-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_generate_s3_presigned_url.yml
277 lines (252 loc) · 8.07 KB
/
auto_generate_s3_presigned_url.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
AWSTemplateFormatVersion: 2010-09-09
Description: Auto generate S3 pre-signed URL.
Parameters:
IAMUserName:
Type: String
Default: AutoGenerateS3PreSignedUrl
ConstraintDescription: '[A-Za-z0-9\+=,\.@_\-]+'
Description: Enter IAM user name.
BucketName:
Type: String
ConstraintDescription: '[A-Za-z0-9\-]+'
Description: Enter S3 bucket name.
SnsDisplayName:
Type: String
Default: PreSignURL
MinLength: 1
MaxLength: 10
Description: Enter SNS display name. (It's set e-mail subject.)
Mail:
Type: String
Description: Enter notify mail address.
NameOfSecretsManager:
Type: String
Default: AutoGenerateS3PreSignedUrl/accessKey
ConstraintDescription: '[A-Za-z0-9_\-\/]+'
MinLength: 1
Description: Enter name of Secrets Manager for access key.
Resources:
S3Bucket:
Type: 'AWS::S3::Bucket'
DependsOn: LambdaPermission
Properties:
BucketName: !Ref BucketName
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: True
IgnorePublicAcls: True
BlockPublicPolicy: True
RestrictPublicBuckets: True
NotificationConfiguration:
LambdaConfigurations:
-
Function: !GetAtt Lambda.Arn
Event: "s3:ObjectCreated:*"
LambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: 'lambda:InvokeFunction'
FunctionName: !GetAtt Lambda.Arn
Principal: 's3.amazonaws.com'
SourceArn: !Sub 'arn:aws:s3:::${BucketName}'
SnsTopic:
Type: 'AWS::SNS::Topic'
Properties:
DisplayName: !Ref SnsDisplayName
KmsMasterKeyId: alias/aws/sns
TopicName: AutoGenerateS3PreSignedUrl
SnsSubscription:
Type: 'AWS::SNS::Subscription'
Properties:
Endpoint: !Ref Mail
Protocol: email
TopicArn: !Ref SnsTopic
IamUser:
Type: 'AWS::IAM::User'
Properties:
UserName: !Ref IAMUserName
UserAccessKey:
Type: 'AWS::IAM::AccessKey'
Properties:
UserName: !Ref IamUser
SecretsManager:
Type: 'AWS::SecretsManager::Secret'
Properties:
Name: !Ref NameOfSecretsManager
KmsKeyId: alias/aws/secretsmanager
SecretString: !Sub |
{
"access_key_id": "${UserAccessKey}",
"secret_access_key": "${UserAccessKey.SecretAccessKey}"
}
PolicyForUser:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: AutoGenerateS3PreSignedUrl_IAMUser
PolicyDocument: {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": [
!GetAtt S3Bucket.Arn,
!Sub '${S3Bucket.Arn}/*'
]
},
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::*/*"
}
]
}
Users:
- !Ref IamUser
RoleForLambda:
Type: 'AWS::IAM::Role'
Properties:
RoleName: AutoGenerateS3PreSignedUrl_LambdaRole
AssumeRolePolicyDocument: {
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [ "lambda.amazonaws.com" ]
},
"Action": [ "sts:AssumeRole" ]
}
]
}
Path: /
Policies:
-
PolicyName: sns
PolicyDocument: {
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": !Ref SnsTopic
}
]
}
-
PolicyName: cloudwatchlogs
PolicyDocument: {
"Version" : "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
-
PolicyName: secretsmanager
PolicyDocument: {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["secretsmanager:GetSecretValue"],
"Resource": !Ref SecretsManager
}
]
}
Lambda:
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: AutoGenerateS3PreSignedUrl
Runtime: python3.7
Handler: 'index.lambda_handler'
Role: !GetAtt RoleForLambda.Arn
MemorySize: 128
Timeout: 60
Environment:
Variables:
'SNS_TOPIC_ARN': !Ref SnsTopic
'EXPIRE_DAYS': '7'
'SECRET_NAME': !Ref NameOfSecretsManager
Code:
ZipFile: !Sub |
from __future__ import print_function
import json
import os
import base64
import boto3
import urllib.parse
from botocore.exceptions import ClientError
sns = boto3.client('sns')
secret_name = os.environ['SECRET_NAME']
expires_days = int(os.environ['EXPIRE_DAYS']) if 'EXPIRE_DAYS' in os.environ else 7
topic_arn = os.environ['SNS_TOPIC_ARN']
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
print("Bucket : {0}, Key : {1}".format(bucket, key))
if key[-1:] == '/':
print("Created directory. Skip generate pre-signed url.")
return {
'statusCode': 209,
'key': key,
'body': 'Created directory. Skip generate pre-signed url.'
}
secret = get_secret()
s3 = boto3.client('s3',
aws_access_key_id = secret['access_key_id'],
aws_secret_access_key = secret['secret_access_key'])
url = get_presigned_url(s3, bucket, key, expires_days)
print("Generated URL : {}".format(url))
subject = "PreSigned URL for {0}".format(key)
message = "Key : {0}\nPre-Signed URL : {1}".format(key, url)
result = send_mail(subject, message)
print(json.dumps(result))
return {
'statusCode': 200,
'body': json.dumps(result)
}
def get_secret():
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager'
)
secret = {}
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
raise e
else:
secret_string = get_secret_value_response['SecretString']
secret = json.loads(secret_string)
return secret
def get_presigned_url(s3, bucket, key, expires_days=7):
url = s3.generate_presigned_url(
ClientMethod = 'get_object',
Params = { 'Bucket': bucket, 'Key': key },
ExpiresIn = expires_days * 24 * 60 * 60,
HttpMethod = 'GET')
return url
def send_mail(subject, message):
request = {
'TopicArn': topic_arn,
'Subject': subject,
'Message': message
}
response = sns.publish(**request)
return response
# Outputs:
# DebugAccessKeyId:
# Value: !Ref UserAccessKey
# DebugSecretAccessKey:
# Value: !GetAtt UserAccessKey.SecretAccessKey