-
Notifications
You must be signed in to change notification settings - Fork 0
/
items_template.yaml
252 lines (234 loc) · 6.88 KB
/
items_template.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
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
Parameters:
Stage:
Type: String
Description: The name for a project pipeline stage, for which resources are provisioned and deployed.
Default: dev
FruitID:
Type: String
Description: An ID to show how to use different variables for different envs.
Default: apples
ServiceName:
Type: String
Default: templates
AllowedValues:
- templates
Description: The service name.
ServiceFeature:
Type: String
Default: items
AllowedValues:
- items
Description: The service feature name.
Resources:
ItemsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "${ServiceName}-${ServiceFeature}-${Stage}"
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ItemsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "${ServiceName}-${ServiceFeature}-${Stage}"
Timeout: 60
Runtime: python3.7
CodeUri: items/
Handler: app.lambda_handler
Environment:
Variables:
TABLE_NAME: !Sub "${ServiceName}-${ServiceFeature}-${Stage}"
SECRET_NAME: !Sub "${ServiceName}/${ServiceFeature}/${Stage}"
KMS_ALIAS: !Sub "alias/${ServiceName}-${ServiceFeature}-${Stage}"
FRUIT_ID: !Sub "${FruitID}"
Events:
PostResoureApiEndpoint:
Type: Api
Properties:
Path: /items/
Method: POST
RestApiId: !Ref ItemsApi
Role: !GetAtt ItemsFunctionRole.Arn
ItemsFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: ['sts:AssumeRole']
Effect: Allow
Principal:
Service: [lambda.amazonaws.com]
Version: 2012-10-17
Policies:
- PolicyName: ItemsFunctionPermissions
PolicyDocument: {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"kms:Decrypt",
"kms:Encrypt",
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret",
"dynamodb:PutItem",
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:Scan",
"dynamodb:Query",
"dynamodb:UpdateItem",
"dynamodb:GetRecords",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
ItemsRule:
Type: AWS::Events::Rule
Properties:
Description: "Run a rule once a week."
ScheduleExpression: "rate(7 days)"
State: "ENABLED"
Targets:
-
Arn:
Fn::GetAtt:
- "ItemsFunction"
- "Arn"
Id: "TargetFunctionV1"
PermissionForEventsToInvokeLambda:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: "ItemsFunction"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn:
Fn::GetAtt:
- "ItemsRule"
- "Arn"
ItemsApi:
Type: AWS::Serverless::Api
Properties:
Name: !Sub "${ServiceName}-${ServiceFeature}-${Stage}"
StageName: !Sub "${Stage}"
DefinitionBody:
'Fn::Transform':
Name: 'AWS::Include'
Parameters:
Location: !Join [ '', [ 's3://', !Ref ServiceName, '-', !Ref ServiceFeature, '-', !Ref Stage, '/items_open_api.yaml' ] ]
UsagePlanBasic:
Type: AWS::ApiGateway::UsagePlan
Properties:
ApiStages:
- ApiId: !Ref 'ItemsApi'
Stage: !Ref Stage
Description: Service access plan
Quota:
Limit: 3600
Period: DAY
UsagePlanName: Basic_Plan
BasicApiKey:
Type: 'AWS::ApiGateway::ApiKey'
Properties:
Name: ItemsAPIKey
Description: Key for accessing services.
Enabled: 'true'
StageKeys:
- RestApiId: !Ref 'ItemsApi'
StageName: !Ref Stage
BasicUsagePlanKey:
Type: 'AWS::ApiGateway::UsagePlanKey'
Properties:
KeyId: !Ref BasicApiKey
KeyType: API_KEY
UsagePlanId: !Ref UsagePlanBasic
BasicKMSKey:
Type: AWS::KMS::Key
Properties:
Description: "A basic CMK"
KeyPolicy:
Version: "2012-10-17"
Id: "key-basic-1"
Statement:
-
Sid: "Enable IAM User Permissions"
Effect: "Allow"
Principal:
AWS: !Join
- ''
- - 'arn:aws:iam::'
- !Ref 'AWS::AccountId'
- ':role/service-role/codebuild-templates-service-role'
Action: "kms:*"
Resource: "*"
-
Sid: "Enable IAM User Permissions"
Effect: "Allow"
Principal:
AWS: !Join
- ''
- - 'arn:aws:iam::'
- !Ref 'AWS::AccountId'
- ':root'
Action: "kms:*"
Resource: "*"
-
Sid: "Enable IAM User Permissions"
Effect: "Allow"
Principal:
AWS: !Join
- ''
- - 'arn:aws:iam::'
- !Ref 'AWS::AccountId'
- ':user/michelle.brenner'
Action: "kms:*"
Resource: "*"
-
Sid: "Allow use of key"
Effect: "Allow"
Principal:
AWS: !GetAtt ItemsFunctionRole.Arn
Action:
- "kms:DescribeKey"
- "kms:Encrypt"
- "kms:Decrypt"
- "kms:ReEncrypt*"
- "kms:GenerateDataKey"
- "kms:GenerateDataKeyWithoutPlaintext"
Resource: "*"
BasicKMSKeyAlias:
Type: AWS::KMS::Alias
Properties:
AliasName: !Sub "alias/${ServiceName}-${ServiceFeature}-${Stage}"
TargetKeyId:
Ref: BasicKMSKey
ItemSecrets:
Type: 'AWS::SecretsManager::Secret'
Properties:
Name: !Sub "${ServiceName}/${ServiceFeature}/${Stage}"
Description: "Creating a secret to store all secrets."
SecretString: '{"a_secret":"UPDATE_ME"}'
Outputs:
ItemsApi:
Description: "Items API Gateway endpoint URL "
Value: !Sub "https://${ItemsApi}.execute-api.${AWS::Region}.amazonaws.com/${Stage}/items"
ItemsFunction:
Description: "Items Lambda Function ARN"
Value: !GetAtt ItemsFunction.Arn
ItemsFunctionRole:
Description: "Implicit IAM Role created for Items function"
Value: !GetAtt ItemsFunctionRole.Arn