This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yml
238 lines (222 loc) · 6.79 KB
/
template.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
# This is the SAM template that represents the architecture of your serverless application
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html
# The AWSTemplateFormatVersion identifies the capabilities of the template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/format-version-structure.html
AWSTemplateFormatVersion: 2010-09-09
Description: >-
aws-sam-typescript-layers-example
# Transform section specifies one or more macros that AWS CloudFormation uses to process your template
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/transform-section-structure.html
Transform:
- AWS::Serverless-2016-10-31
Globals:
Function:
Environment:
Variables:
REGION: ${AWS::Region}
RESULTS_BUCKET: !Ref TestResultBucket
Timeout: 100
MemorySize: 2048
# Resources declares the AWS resources that you want to include in the stack
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
Resources:
ApiResource:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
BinaryMediaTypes:
Auth:
DefaultAuthorizer: NONE
# Each Lambda function is defined by properties:
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
# This is a Lambda function config associated with the source code: get-all-items.js
cypressLambdaFunction:
Type: AWS::Serverless::Function
Metadata:
DockerTag: cypress-lambda
DockerContext: ./
Dockerfile: docker-src/lambda.Dockerfile
Properties:
PackageType: Image
Description: A simple example includes a HTTP get method to get all items from a DynamoDB table.
Policies:
- Statement:
- Sid: TestBucketFullAccess
Effect: Allow
Action:
- s3:*
Resource:
- !GetAtt TestResultBucket.Arn
- !Join
- ''
- - !GetAtt TestResultBucket.Arn
- '/*'
Events:
Api:
Type: Api
Properties:
RestApiId: !Ref ApiResource
Path: /lambda
Method: ANY
RunTestFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/functions/runtest
Handler: index.handler
Runtime: nodejs14.x
Description: A function that ideally would run a test
Environment:
Variables:
ECS_CLUSTER: !Ref ECSCluster
ECS_TASKDEF: !Ref ECSBaseTaskDef
Policies:
- Statement:
- Sid: ECSRunAccess
Effect: Allow
Action:
- ecs:RunTask
Condition:
ArnEquals:
ecs:cluster: !GetAtt ECSCluster.Arn
Resource:
- !Ref ECSBaseTaskDef
- Sid: ECSPassRole
Effect: Allow
Action:
- iam:GetRole
- iam:PassRole
Resource:
- !GetAtt ECSExecutionRole.Arn
- !GetAtt ECSTaskRole.Arn
- Sid: EC2Describe
Effect: Allow
Action:
- ec2:DescribeSubnets
- ec2:DescribeSecurityGroups
- ec2:DescribeVpcs
Resource:
- '*'
Events:
Api:
Type: Api
Properties:
RestApiId: !Ref ApiResource
Path: /
Method: ANY
RunCompleteFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/functions/runcomplete
Handler: index.handler
Runtime: nodejs14.x
Description: A function that responds to ECS task ending
Policies:
- Statement:
- Sid: TestBucketFullAccess
Effect: Allow
Action:
- s3:*
Resource:
- !GetAtt TestResultBucket.Arn
- !Join
- ''
- - !GetAtt TestResultBucket.Arn
- '/*'
Events:
CloudWatchEvent:
Type: CloudWatchEvent
Properties:
RestApiId: !Ref ApiResource
Path: /
Method: ANY
TestResultBucket:
Type: AWS::S3::Bucket
ECSRepository:
Type: AWS::ECR::Repository
ECSBaseTaskDef:
Type: AWS::ECS::TaskDefinition
Properties:
RequiresCompatibilities:
- FARGATE
NetworkMode: awsvpc
Cpu: 1024
Memory: 2048
ExecutionRoleArn: !Ref ECSExecutionRole
TaskRoleArn: !Ref ECSTaskRole
ContainerDefinitions:
- Name: cypress
Image: !Join
- ''
- - !GetAtt ECSRepository.RepositoryUri
- ':latest'
Essential: true
LogConfiguration:
LogDriver: awslogs
Options:
awslogs-region: !Ref AWS::Region
awslogs-group: !Ref ECSLogGroup
awslogs-stream-prefix: ecs
ECSExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy'
# PolicyDocuments at the IAM role require JSON strings
ECSExecutionRolePolicies:
Type: AWS::IAM::Policy
DependsOn:
- TestResultBucket
Properties:
PolicyName: TestBucketFullAccess
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- s3:*
Resource:
- !GetAtt TestResultBucket.Arn
- !Join
- ''
- - !GetAtt TestResultBucket.Arn
- '/*'
Roles:
- !Ref ECSExecutionRole
ECSTaskRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: 'sts:AssumeRole'
ECSLogGroup:
Type: AWS::Logs::LogGroup
ECSCluster:
Type: AWS::ECS::Cluster
Properties:
CapacityProviders:
- FARGATE
ClusterSettings:
- Name: containerInsights
Value: enabled
Outputs:
WebEndpoint:
Description: API Gateway endpoint URL for Prod stage
Value: !Sub "https://${ApiResource}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
Repository:
Description: ECS Repository URL
Value: !GetAtt ECSRepository.RepositoryUri
Task:
Description: ECS Task Definition
Value: !Ref ECSBaseTaskDef
Cluster:
Description: ECS Cluster
Value: !Ref ECSCluster