Skip to content

Commit 32c4457

Browse files
mw-vmaffetGitHub Enterprise
authored and
GitHub Enterprise
committed
Release Storage Location template v1.0.0 (#8)
1 parent 8cf1d6f commit 32c4457

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed

aws/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Each template configures a specific chunk of infrastructure. MathWorks® refe
1010
| ------------- | ----------- |
1111
| [security-group](security-group) | Creates a security group to control the inbound and outbound traffic for the resources deployed in AWS. |
1212
| [log-location](log-location) | Creates a CloudWatch log group to store log events from AWS services. |
13+
| [storage-location](storage-location) | Creates an Amazon S3™ Bucket to store objects in AWS. |
1314

1415
## Usage
1516

@@ -75,4 +76,4 @@ The [GetAtt](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intr
7576
For support, visit [MathWorks Technical Support](https://www.mathworks.com/support/contact_us.html).
7677

7778
---
78-
Copyright 2023 The MathWorks, Inc.
79+
Copyright 2024 The MathWorks, Inc.

aws/storage-location/v1/.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v1.0.0
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
AWSTemplateFormatVersion: '2010-09-09'
3+
4+
Transform: AWS::LanguageExtensions
5+
6+
Description: >
7+
MathWorks Reference Architectures Template Storage Location: Creates an Amazon S3 Bucket to store objects in AWS. version: v1.0.0
8+
9+
Metadata:
10+
AWS::CloudFormation::Interface:
11+
ParameterGroups:
12+
- Label:
13+
default: Bucket Options
14+
Parameters:
15+
- BucketName
16+
- DeletionPolicy
17+
- Versioning
18+
19+
ParameterLabels:
20+
BucketName:
21+
default: Bucket Name
22+
DeletionPolicy:
23+
default: Deletion Policy
24+
25+
Parameters:
26+
BucketName:
27+
Type: String
28+
Default: ''
29+
Description: A name for the bucket. If you don't specify a name, AWS CloudFormation generates a unique ID and uses that ID for the bucket name.
30+
AllowedPattern: ^([a-z0-9][a-z0-9.-]{1,61}[a-z0-9])?$
31+
ConstraintDescription: Bucket names must be between 3 and 63 characters long, consist only of lowercase letters, numbers, dots (.), and hyphens (-), and must begin and end with a letter or number.
32+
DeletionPolicy:
33+
Type: String
34+
AllowedValues: [Delete, Retain]
35+
Default: Retain
36+
Description: Specify what to do with the bucket when its stack is deleted.
37+
Versioning:
38+
Type: String
39+
AllowedValues: ['Yes', 'No']
40+
Default: 'No'
41+
Description: Version the objects in the bucket.
42+
43+
Conditions:
44+
GenerateName: !Equals [ !Ref BucketName, '' ]
45+
DeleteBucket: !Equals [ !Ref DeletionPolicy, Delete ]
46+
EnableVersioning: !Equals [ !Ref Versioning, 'Yes' ]
47+
48+
Resources:
49+
Bucket:
50+
Type: AWS::S3::Bucket
51+
DeletionPolicy: !Ref DeletionPolicy
52+
Properties:
53+
BucketName: !If [ GenerateName, !Ref AWS::NoValue, !Ref BucketName ]
54+
VersioningConfiguration: !If [ EnableVersioning, Status: Enabled, !Ref AWS::NoValue ]
55+
56+
EmptyBucketLambda:
57+
Type: AWS::Lambda::Function
58+
Condition: DeleteBucket
59+
Properties:
60+
Code:
61+
ZipFile: |
62+
import boto3
63+
import cfnresponse
64+
65+
def lambda_handler(event, context):
66+
67+
status = cfnresponse.SUCCESS
68+
data = {'Message': '', 'Errors': ''}
69+
70+
try:
71+
if event['RequestType'] == 'Delete':
72+
bucket_name = event['ResourceProperties']['BucketName']
73+
74+
s3 = boto3.resource('s3')
75+
bucket = s3.Bucket(bucket_name)
76+
77+
versioning = bucket.Versioning()
78+
if versioning.status:
79+
versioning.suspend()
80+
81+
response = bucket.object_versions.delete()
82+
if any('Errors' in r for r in response):
83+
status = cfnresponse.FAILED
84+
data['Errors'] += ';'.join(str(r['Errors']) for r in response)
85+
else:
86+
data['Message'] += 'Versions deleted successfully.'
87+
88+
else:
89+
data['Message'] += 'Bucket is not versioned.'
90+
91+
response = bucket.objects.delete()
92+
if any('Errors' in r for r in response):
93+
status = cfnresponse.FAILED
94+
data['Errors'] += ';'.join(str(r['Errors']) for r in response)
95+
else:
96+
data['Message'] += 'Objects deleted successfully.'
97+
98+
else:
99+
data['Message'] += 'Lambda created successfully.'
100+
101+
except Exception as e:
102+
status = cfnresponse.FAILED
103+
data['Exception'] = str(e)
104+
105+
cfnresponse.send(event, context, status, data)
106+
107+
Handler: index.lambda_handler
108+
Runtime: python3.12
109+
Timeout: 900
110+
Role: !GetAtt EmptyBucketRole.Arn
111+
112+
EmptyBucketRole:
113+
Type: AWS::IAM::Role
114+
Condition: DeleteBucket
115+
Properties:
116+
AssumeRolePolicyDocument:
117+
Version: '2012-10-17'
118+
Statement:
119+
- Effect: Allow
120+
Principal:
121+
Service:
122+
- lambda.amazonaws.com
123+
Action:
124+
- sts:AssumeRole
125+
Path: /
126+
ManagedPolicyArns:
127+
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
128+
Policies:
129+
- PolicyName: empty-bucket-core
130+
PolicyDocument:
131+
Version: '2012-10-17'
132+
Statement:
133+
- Sid: AllowRead
134+
Effect: Allow
135+
Action:
136+
- s3:ListBucket
137+
- s3:ListBucketVersions
138+
- s3:GetBucketVersioning
139+
Resource: !GetAtt Bucket.Arn
140+
- Sid: AllowEdit
141+
Effect: Allow
142+
Action:
143+
- s3:PutBucketVersioning
144+
Resource: !GetAtt Bucket.Arn
145+
- Sid: AllowDelete
146+
Effect: Allow
147+
Action:
148+
- s3:DeleteObject
149+
- s3:DeleteObjectVersion
150+
Resource: !Sub ${Bucket.Arn}/*
151+
152+
EmptyBucketTrigger:
153+
Type: Custom::LambdaTrigger
154+
Condition: DeleteBucket
155+
Properties:
156+
ServiceToken: !GetAtt EmptyBucketLambda.Arn
157+
BucketName: !Ref Bucket
158+
159+
Outputs:
160+
BucketName:
161+
Value: !Ref Bucket
162+
BucketArn:
163+
Value: !GetAtt Bucket.Arn

0 commit comments

Comments
 (0)