-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathrdws-test.stack.yml
More file actions
386 lines (373 loc) · 13.2 KB
/
rdws-test.stack.yml
File metadata and controls
386 lines (373 loc) · 13.2 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template that represents a request driven web service on AWS App Runner.
Metadata:
Version: v1.29.0
Parameters:
AppName:
Type: String
EnvName:
Type: String
WorkloadName:
Type: String
ContainerImage:
Type: String
ContainerPort:
Type: Number
ImageRepositoryType:
Type: String
InstanceCPU:
Type: String
InstanceMemory:
Type: String
HealthCheckPath:
Type: String
Default: ''
HealthCheckInterval:
Type: String
Default: ''
HealthCheckTimeout:
Type: String
Default: ''
HealthCheckHealthyThreshold:
Type: String
Default: ''
HealthCheckUnhealthyThreshold:
Type: String
Default: ''
AddonsTemplateURL:
Description: 'URL of the addons nested stack template within the S3 bucket.'
Type: String
Default: ''
ArtifactKeyARN:
Type: String
Description: 'KMS Key used for encrypting artifacts'
Conditions:
# App Runner will not accept an AccessRole for ImageRepositoryTypes other than ECR.
# In this scenario the request will fail.
NeedsAccessRole:
!Equals [!Ref ImageRepositoryType, 'ECR']
HasHealthCheckPath:
!Not [!Equals [!Ref HealthCheckPath, '']]
HasHealthCheckInterval:
!Not [!Equals [!Ref HealthCheckInterval, '']]
HasHealthCheckTimeout:
!Not [!Equals [!Ref HealthCheckTimeout, '']]
HasHealthCheckHealthyThreshold:
!Not [!Equals [!Ref HealthCheckHealthyThreshold, '']]
HasHealthCheckUnhealthyThreshold:
!Not [!Equals [!Ref HealthCheckUnhealthyThreshold, '']]
HasAddons: # If a bucket URL is specified, that means the template exists.
!Not [!Equals [!Ref AddonsTemplateURL, '']]
Resources:
AccessRole:
Metadata:
'aws:copilot:description': 'An IAM Role for App Runner to use on your behalf to pull your image from ECR'
Type: AWS::IAM::Role
Condition: NeedsAccessRole
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- build.apprunner.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSAppRunnerServicePolicyForECRAccess
InstanceRole:
Metadata:
'aws:copilot:description': 'An IAM role to control permissions for the containers in your service'
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- tasks.apprunner.amazonaws.com
Action: 'sts:AssumeRole'
Policies:
- PolicyName: 'DenyIAM'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Deny'
Action: 'iam:*'
Resource: '*'
- PolicyName: 'AccessCopilotTaggedSecrets'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'ssm:GetParameters'
Resource:
- !Sub 'arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*'
Condition:
StringEquals:
'ssm:ResourceTag/copilot-application': !Sub '${AppName}'
'ssm:ResourceTag/copilot-environment': !Sub '${EnvName}'
- Effect: 'Allow'
Action:
- 'secretsmanager:GetSecretValue'
Resource:
- !Sub 'arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:*'
Condition:
StringEquals:
'secretsmanager:ResourceTag/copilot-application': !Sub '${AppName}'
'secretsmanager:ResourceTag/copilot-environment': !Sub '${EnvName}'
- Effect: 'Allow'
Action:
- 'kms:Decrypt'
Resource:
- !Ref ArtifactKeyARN
- Sid: DecryptTaggedKMSKey
Effect: 'Allow'
Action:
- 'kms:Decrypt'
Resource:
- !Sub 'arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/*'
Condition:
StringEquals:
'aws:ResourceTag/copilot-application': !Sub '${AppName}'
'aws:ResourceTag/copilot-environment': !Sub '${EnvName}'
- PolicyName: 'Publish2SNS'
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action: 'sns:Publish'
Resource:
- !Ref customersSNSTopic
- !Ref mytopicfifoSNSTopic
Service:
Metadata:
'aws:copilot:description': 'An App Runner service to run and manage your containers'
Type: AWS::AppRunner::Service
Properties:
ServiceName: !Sub '${AppName}-${EnvName}-${WorkloadName}'
SourceConfiguration:
AuthenticationConfiguration: !If
- NeedsAccessRole
- AccessRoleArn: !GetAtt AccessRole.Arn
- !Ref AWS::NoValue
AutoDeploymentsEnabled: false
ImageRepository:
ImageIdentifier: !Ref ContainerImage
ImageRepositoryType: !Ref ImageRepositoryType
ImageConfiguration:
Port: !Ref ContainerPort
RuntimeEnvironmentSecrets:
- Name: MY_FROM_CFN_SECRET
Value:
Fn::ImportValue: 'my-other-stack-exported-secret'
- Name: MY_SECRETS_MANAGER_SECRET
Value: !Sub 'arn:${AWS::Partition}:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:mysecretsmanagername'
- Name: my-ssm-secret
Value: arn:aws:ssm:us-east-1:123456789123:parameter/jan11ssm
RuntimeEnvironmentVariables:
- Name: COPILOT_APPLICATION_NAME
Value: !Ref AppName
- Name: COPILOT_ENVIRONMENT_NAME
Value: !Ref EnvName
- Name: COPILOT_SERVICE_NAME
Value: !Ref WorkloadName
- Name: COPILOT_SERVICE_DISCOVERY_ENDPOINT
Value:
- Name: COPILOT_SNS_TOPIC_ARNS
Value: '{"customers":"arn:aws:sns:us-west-2:123456789123:my-app-test-frontend-customers","mytopic.fifo":"arn:aws:sns:us-west-2:123456789123:my-app-test-frontend-mytopic.fifo"}'
- Name: DB_NAME
Value:
Fn::ImportValue: "test-UserDB"
- Name: LOG_LEVEL
Value: "debug"
StartCommand: crazy diamond
InstanceConfiguration:
Cpu: !Ref InstanceCPU
Memory: !Ref InstanceMemory
InstanceRoleArn: !GetAtt InstanceRole.Arn
NetworkConfiguration:
EgressConfiguration:
EgressType: VPC
VpcConnectorArn: !Ref VpcConnector
AutoScalingConfigurationArn: 'arn:${AWS::Partition}:apprunner:${AWS::Region}:${AWS::AccountId}:autoscalingconfiguration/high-availability/3'
Tags:
- Key: copilot-application
Value: !Ref AppName
- Key: copilot-environment
Value: !Ref EnvName
- Key: copilot-service
Value: !Ref WorkloadName
AddonsStack:
Metadata:
'aws:copilot:description': 'An Addons CloudFormation Stack for your additional AWS resources'
Type: AWS::CloudFormation::Stack # Needed for #1848
DependsOn: EnvControllerAction
Condition: HasAddons
Properties:
Parameters:
App: !Ref AppName
Env: !Ref EnvName
Name: !Ref WorkloadName
TemplateURL:
!Ref AddonsTemplateURL
customersSNSTopic:
Metadata:
'aws:copilot:description': 'A SNS topic to broadcast customers events'
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub '${AWS::StackName}-customers'
KmsMasterKeyId: 'alias/aws/sns'
customersSNSTopicPolicy:
Type: AWS::SNS::TopicPolicy
DependsOn: customersSNSTopic
Properties:
Topics:
- !Ref customersSNSTopic
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:root'
Action:
- sns:Subscribe
Resource: !Ref customersSNSTopic
Condition:
StringEquals:
"sns:Protocol": "sqs"
mytopicfifoSNSTopic:
Metadata:
'aws:copilot:description': 'A SNS FIFO topic to broadcast mytopic.fifo events'
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub '${AWS::StackName}-mytopic.fifo'
FifoTopic: true
KmsMasterKeyId: 'alias/aws/sns'
mytopicfifoSNSTopicPolicy:
Type: AWS::SNS::TopicPolicy
DependsOn: mytopicfifoSNSTopic
Properties:
Topics:
- !Ref mytopicfifoSNSTopic
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
AWS: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:root'
Action:
- sns:Subscribe
Resource: !Ref mytopicfifoSNSTopic
Condition:
StringEquals:
"sns:Protocol": "sqs"
EnvControllerAction:
Metadata:
'aws:copilot:description': "Update your environment's shared resources"
Type: Custom::EnvControllerFunction
Properties:
ServiceToken: !GetAtt EnvControllerFunction.Arn
Workload: !Ref WorkloadName
EnvStack: !Sub '${AppName}-${EnvName}'
Parameters: [NATWorkloads,]
EnvVersion: v1.42.0
EnvControllerFunction:
Type: AWS::Lambda::Function
Properties:
Handler: "index.handler"
Timeout: 900
MemorySize: 512
Role: !GetAtt 'EnvControllerRole.Arn'
Runtime: nodejs22.x
EnvControllerRole:
Metadata:
'aws:copilot:description': "An IAM role to update your environment stack"
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
-
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: "EnvControllerStackUpdate"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- cloudformation:DescribeStacks
- cloudformation:UpdateStack
Resource: !Sub 'arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/${AppName}-${EnvName}/*'
Condition:
StringEquals:
'cloudformation:ResourceTag/copilot-application': !Sub '${AppName}'
'cloudformation:ResourceTag/copilot-environment': !Sub '${EnvName}'
- PolicyName: "EnvControllerRolePass"
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- iam:PassRole
Resource: !Sub 'arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${AppName}-${EnvName}-CFNExecutionRole'
Condition:
StringEquals:
'iam:ResourceTag/copilot-application': !Sub '${AppName}'
'iam:ResourceTag/copilot-environment': !Sub '${EnvName}'
ManagedPolicyArns:
- !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
ServiceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Metadata:
'aws:copilot:description': 'A security group for your addons/ resources to allow ingress from your service'
Properties:
GroupDescription: !Join [ '', [ !Ref AppName, '-', !Ref EnvName, !Ref WorkloadName ] ]
VpcId:
Fn::ImportValue:
!Sub '${AppName}-${EnvName}-VpcId'
Tags:
- Key: Name
Value: !Sub 'copilot-${AppName}-${EnvName}-${WorkloadName}'
EnvironmentSecurityGroupIngressFromServiceSecurityGroup:
Type: AWS::EC2::SecurityGroupIngress
Metadata:
'aws:copilot:description': 'Allow ingress from the app runner service to services in your environment'
Properties:
GroupId:
Fn::ImportValue:
!Sub '${AppName}-${EnvName}-EnvironmentSecurityGroup'
IpProtocol: -1
SourceSecurityGroupId: !Ref ServiceSecurityGroup
VpcConnector:
Type: AWS::AppRunner::VpcConnector
DependsOn: EnvControllerAction # Ensure NAT gateways are created before connecting to a VPC.
Metadata:
'aws:copilot:description': 'A VPC connector to link your service to subnets in your environment'
Properties:
Subnets:
Fn::Split:
- ","
- Fn::ImportValue:
!Sub '${AppName}-${EnvName}-PrivateSubnets'
SecurityGroups:
- !Ref ServiceSecurityGroup
- Fn::ImportValue: !Sub "${AppName}-${EnvName}-EnvironmentSecurityGroup"
Tags:
- Key: copilot-application
Value: !Ref AppName
- Key: copilot-environment
Value: !Ref EnvName
- Key: copilot-service
Value: !Ref WorkloadName