-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-cloudformation-stack.yaml
341 lines (309 loc) · 11 KB
/
aws-cloudformation-stack.yaml
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# References:
# Policy elements reference: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html
# Policy language grammar: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_grammar.html
# CFN template snippets: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/CHAP_TemplateQuickRef.html
# IAM template snippets: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/quickref-iam.html
Resources:
# This IAM User can only have programmatic access. Credentials can be generated in AWS console.
# This user can invoke the lambda function only using temporary credentials, by assuming its execution role.
stsUser:
Type: AWS::IAM::User
Properties:
UserName: !Sub '${AWS::StackName}-stsUser'
s3Cmk:
Type: AWS::KMS::Key
Properties:
Enabled: true
PendingWindowInDays: 7
KeyPolicy:
Id: key-consolepolicy-3
Version: '2012-10-17'
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: 'kms:*'
Resource: '*'
- Sid: Allow access for Key Administrators
Effect: Allow
Principal:
AWS: !Sub 'arn:aws:iam::${AWS::AccountId}:user/cloud_user'
Action:
- 'kms:Create*'
- 'kms:Describe*'
- 'kms:Enable*'
- 'kms:List*'
- 'kms:Put*'
- 'kms:Update*'
- 'kms:Revoke*'
- 'kms:Disable*'
- 'kms:Get*'
- 'kms:Delete*'
- 'kms:TagResource'
- 'kms:UntagResource'
- 'kms:ScheduleKeyDeletion'
- 'kms:CancelKeyDeletion'
Resource: '*'
- Sid: Allow use of the key
Effect: Allow
Principal:
AWS:
- !GetAtt execGetUserRole.Arn
- !GetAtt execPutUserRole.Arn
Action:
- 'kms:Encrypt'
- 'kms:Decrypt'
- 'kms:ReEncrypt*'
- 'kms:GenerateDataKey*'
- 'kms:DescribeKey'
Resource: '*'
- Sid: Allow attachment of persistent resources
Effect: Allow
Principal:
AWS:
- !GetAtt execGetUserRole.Arn
- !GetAtt execPutUserRole.Arn
Action:
- 'kms:CreateGrant'
- 'kms:ListGrants'
- 'kms:RevokeGrant'
Resource: '*'
Condition:
Bool:
'kms:GrantIsForAWSResource': 'true'
s3CmkAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub 'alias/${AWS::StackName}-cmk-s3'
TargetKeyId: !GetAtt s3Cmk.KeyId
usersBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub '${AWS::AccountId}-users-collection'
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: aws:kms
KMSMasterKeyID: !Ref s3Cmk
BucketKeyEnabled: true
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
ssmParameter:
Type: AWS::SSM::Parameter
Properties:
Name: !Sub '${AWS::StackName}-parameters'
Type: String
Value: !Sub
- '{"users-s3bucket":"${bucketName}"}'
- { bucketName: !Ref usersBucket }
getUserLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-getUser'
Role: !GetAtt execGetUserRole.Arn
Runtime: nodejs12.x
Handler: index.handler
Code:
ZipFile: |
const AWS = require("aws-sdk");
const SSM = new AWS.SSM();
const S3 = new AWS.S3();
const envParamStore = process.env.PARAM_STORE ? process.env.PARAM_STORE : "apigeeDemo-parameters";
const envUserBucketParam = process.env.PARAM_USER_BUCKET ? process.env.PARAM_USER_BUCKET : "users-s3bucket";
exports.handler = async(event) => {
console.log(`xCorrelationId: ${event.metadata.xCorrelationId}`);
try {
const ssmParameter = { Name: envParamStore };
const parameter = await SSM.getParameter(ssmParameter).promise();
const usersBucket = JSON.parse(parameter.Parameter.Value)[envUserBucketParam];
const userS3Object = {
Bucket: usersBucket,
Key: event.data.username
};
const userObject = await S3.getObject(userS3Object).promise();
return {
statusCode: 200,
body: JSON.parse(userObject.Body.toString())
};
}
catch (err) {
console.log(JSON.stringify(err));
return {
statusCode: 500,
body: `Error retrieving user: "${event.data.username}"`
};
}
};
Environment:
Variables:
PARAM_STORE: !Ref ssmParameter
PARAM_USER_BUCKET: users-s3bucket
putUserLambda:
Type: AWS::Lambda::Function
Properties:
FunctionName: !Sub '${AWS::StackName}-putUser'
Role: !GetAtt execPutUserRole.Arn
Runtime: nodejs12.x
Handler: index.handler
Code:
ZipFile: |
const AWS = require("aws-sdk");
const SSM = new AWS.SSM();
const S3 = new AWS.S3();
const envParamStore = process.env.PARAM_STORE ? process.env.PARAM_STORE : "apigeeDemo-parameters";
const envUserBucketParam = process.env.PARAM_USER_BUCKET ? process.env.PARAM_USER_BUCKET : "users-s3bucket";
exports.handler = async(event) => {
console.log(`xCorrelationId: ${event.metadata.xCorrelationId}`);
try {
const ssmParameter = { Name: envParamStore };
const parameter = await SSM.getParameter(ssmParameter).promise();
const usersBucket = JSON.parse(parameter.Parameter.Value)[envUserBucketParam];
const userS3object = {
Bucket: usersBucket,
Key: event.data.username,
Body: JSON.stringify(event.data),
ContentType: "application/json",
};
await S3.putObject(userS3object).promise();
return {
statusCode: 200,
body: `User "${event.data.username}" creation successful`
};
}
catch (err) {
console.log(JSON.stringify(err));
return {
statusCode: 500,
body: `User "${event.data.username}" creation failed`
};
}
};
Environment:
Variables:
PARAM_STORE: !Ref ssmParameter
PARAM_USER_BUCKET: users-s3bucket
execGetUserRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub '${AWS::StackName}-execGetUserRole'
# Trust-relationship should include AWS services & IAM users expected to invoke the lambda function
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
AWS: !GetAtt stsUser.Arn
# AWS service roles simplify permissions management
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
execPutUserRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Sub '${AWS::StackName}-execPutUserRole'
# Trust-relationship should include AWS services & IAM users expected to invoke the lambda function
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: sts:AssumeRole
Principal:
Service: lambda.amazonaws.com
AWS: !GetAtt stsUser.Arn
# AWS service roles simplify permissions management
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AmazonS3FullAccess
# Function resource refers to the Role resource as its execution role.
# So following as an inline-policy in Role resource would lead to circular reference error.
# This managed-policy is required for IAM users to invoke the lambda function.
# If only AWS services invoke the lambda function, AWS service roles (ManagedPolicyArns) will suffice.
allowInvokeGetUser:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub '${AWS::StackName}-allowInvokeGetUser'
Roles:
- !Ref execGetUserRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource:
- !GetAtt getUserLambda.Arn
allowInvokePutUser:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub '${AWS::StackName}-allowInvokePutUser'
Roles:
- !Ref execPutUserRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- lambda:InvokeFunction
Resource:
- !GetAtt putUserLambda.Arn
# Role resource refers to the User resource in its trust-relationship policy.
# So following as an inline-policy in User resource would lead to circular reference error.
allowStsUserAssumeExecRoles:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub '${AWS::StackName}-allowStsUserAssumeExecRoles'
Users:
- !Ref stsUser
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sts:AssumeRole
Resource:
- !GetAtt execGetUserRole.Arn
- !GetAtt execPutUserRole.Arn
allowGetSsmParametes:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub '${AWS::StackName}-allowReadSsmParametes'
Roles:
- !Ref execGetUserRole
- !Ref execPutUserRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:GetParameter
Resource:
- !Sub
- 'arn:aws:ssm:us-east-1:${AWS::AccountId}:parameter/${ssmParameter}'
- { ssmParameter: !Ref ssmParameter }
Outputs:
apigeeUser:
Value: !Ref stsUser
Export:
Name: apigeeUser
getUserLambda:
Value: !Ref getUserLambda
Export:
Name: getUserLambda
execGetUserRoleArn:
Value: !GetAtt execGetUserRole.Arn
Export:
Name: getUserExecRoleArn
putUserLambda:
Value: !Ref putUserLambda
Export:
Name: putUserLambda
execPutUserRoleArn:
Value: !GetAtt execPutUserRole.Arn
Export:
Name: putUserExecRoleArn