-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yaml
356 lines (321 loc) · 11.8 KB
/
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
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
# CloudFormation / SAM template to create a serverless application.
# Two architectures are included, both using the same DynamoDB table and static frontend:
# 1. Lambdas ('serverless' functions) -> implemented below
# 2. Fargate ('serverless' containers) -> Todo, pretty involved
#
# Created: 2019-12-19
# Author: turiphro
#
# Install: pip3 install awscli boto3 aws-sam-cli
# Create with:
# sam validate
# sam build
#
# # 1) test locally, behind full API
# # note: can keep the API running, just run 'sam build' after changes
# sam local start-api # -> set URL in index.html or call manually:
# http GET http://127.0.0.1:3000/blog/foo
# # test locally, raw call w/h API
# sam local invoke GetBlog --event <(echo '{"id": "foo"}') | jq .
#
# # 2) test locally, Docker container
# export TABLE_NAME=$SOME_DYNAMODB_TABLE
# docker build -t blog .
# docker run -it -p 5000:80 -v $PWD/src:/app -v ~/.aws:/root/.aws -e TABLE_NAME blog
#
# # deploy:
# sam deploy --guided
#
# # update urls in index.html, open locally
#
# This example is mostly from scratch, but with some help from:
# - https://aws.amazon.com/blogs/compute/introducing-simplified-serverless-application-deplyoment-and-management/
# - https://github.com/nathanpeck/aws-cloudformation-fargate
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Api:
Cors:
AllowOrigin: "'*'"
AllowHeaders: "'Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization'"
Parameters:
DockerImage:
Type: String
Description: Docker image location (push first, then pass in)
Default: turiphro/fargate-blog
Outputs:
FunctionsURL:
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod"
Description: URL for API gateway (Lambda functions) endpoint
ContainersURL:
Value: !Join ['', ['http://', !GetAtt FargateLoadBalancer.DNSName]]
Description: URL for load balancer (Fargate containers) endpoint
Resources:
## Shared table
BlogTable:
Type: AWS::Serverless::SimpleTable
Name: ServerlessBlog
Properties:
PrimaryKey:
Name: id
Type: String
## 1. Serverless functions route
GetBlogs:
Type: AWS::Serverless::Function
Properties:
Handler: src.functions.get_all
Runtime: python3.7
Policies:
- AmazonDynamoDBReadOnlyAccess
- AmazonAPIGatewayPushToCloudWatchLogs
Environment:
Variables:
TABLE_NAME: !Ref BlogTable
Events:
GetResource:
Type: Api
Properties:
Path: /blog
Method: get
GetBlog:
Type: AWS::Serverless::Function
Properties:
Handler: src.functions.get
Runtime: python3.7
Policies:
- AmazonDynamoDBReadOnlyAccess
- AmazonAPIGatewayPushToCloudWatchLogs
Environment:
Variables:
TABLE_NAME: !Ref BlogTable
Events:
GetResource:
Type: Api
Properties:
Path: /blog/{id}
Method: get
PostBlog:
Type: AWS::Serverless::Function
Properties:
Handler: src.functions.post
Runtime: python3.7
Policies:
- AmazonDynamoDBFullAccess # note: needs write access
- AmazonAPIGatewayPushToCloudWatchLogs
Environment:
Variables:
TABLE_NAME: !Ref BlogTable
Events:
GetResource:
Type: Api
Properties:
Path: /blog
Method: post
DeleteBlog:
Type: AWS::Serverless::Function
Properties:
Handler: src.functions.delete
Runtime: python3.7
Policies:
- AmazonDynamoDBFullAccess
- AmazonAPIGatewayPushToCloudWatchLogs
Environment:
Variables:
TABLE_NAME: !Ref BlogTable
Events:
GetResource:
Type: Api
Properties:
Path: /blog/{id}
Method: delete
## 2. Fargate route
# Fargate CloudFormation templates are very (!) involved.
# Fargate services need a VPC, Cluster, LoadBalancer, TaskDefinition, and manually
# building & pushing containers to a (private?) AWS container repository.
# Definitely want to start from a template.
# Skipping for now.
#
# Examples:
# - https://github.com/aws-samples/startup-kit-templates/blob/master/vpc-bastion-fargate.cfn.yml
# - https://github.com/nathanpeck/aws-cloudformation-fargate
FargateCluster:
Type: AWS::ECS::Cluster
FargateTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
NetworkMode: awsvpc # only supported mode for Fargate
Memory: 512
Cpu: 256
RequiresCompatibilities:
- FARGATE
TaskRoleArn: !Ref FargateTaskRole
ContainerDefinitions:
- Name: blog-container
Image: !Ref DockerImage
PortMappings:
- ContainerPort: 80
Environment:
- Name: "TABLE_NAME"
Value: !Ref BlogTable
- Name: "WERKZEUG_DEBUG_PIN"
Value: "off"
FargateService:
Type: AWS::ECS::Service
DependsOn:
- FargateLoadBalancerListener
Properties:
ServiceName: blog-service
Cluster: !Ref FargateCluster
TaskDefinition: !Ref FargateTaskDefinition
LaunchType: FARGATE
DesiredCount: 3
NetworkConfiguration:
AwsvpcConfiguration:
AssignPublicIp: ENABLED
Subnets:
# In production, this should be multiple private subnets
- !Ref FargateSubnet1
- !Ref FargateSubnet2
SecurityGroups:
- !Ref FargateSecurityGroup
LoadBalancers:
- ContainerName: blog-container
ContainerPort: 80
TargetGroupArn: !Ref FargateTargetGroup
# In production, a AWS::ApplicationAutoScaling::ScalingPolicy should be added (now 3 instances).
FargateTaskRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: [ecs-tasks.amazonaws.com]
Action: ['sts:AssumeRole']
Policies:
- PolicyName: ECSDynamodbPolicy
PolicyDocument:
Statement:
- Effect: Allow
Resource: "*"
Action:
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:DeleteItem
- dynamodb:UpdateItem
- dynamodb:Query
- dynamodb:Scan
- dynamodb:BatchGetItem
- dynamodb:BatchWriteItem
# Load balancer in front of the containers (inside the VPC below)
FargateLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internet-facing
LoadBalancerAttributes:
- Key: idle_timeout.timeout_seconds
Value: '30'
Subnets:
- !Ref FargateSubnet1
- !Ref FargateSubnet2
SecurityGroups:
- !Ref FargateLoadBalancerSecurityGroup
FargateLoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: "Fargate LB public firewall"
GroupDescription: "inbound from anywhere"
VpcId: !Ref FargateVPC
SecurityGroupIngress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
FargateTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
TargetType: ip
Protocol: HTTP
Port: 80
VpcId: !Ref FargateVPC
FargateLoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn:
- FargateLoadBalancer
Properties:
LoadBalancerArn: !Ref FargateLoadBalancer
Port: 80
Protocol: HTTP # In production, this should be HTTPS with a Certificate
DefaultActions:
- Type: forward
TargetGroupArn: !Ref FargateTargetGroup
# A whole lot of VPC config is needed,
# including access inbound (requests) and outbound (fetch Docker container)
FargateVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
FargateInternetGateway:
Type: AWS::EC2::InternetGateway
FargateInternetAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref FargateVPC
InternetGatewayId: !Ref FargateInternetGateway
# In production, we should add more subnets in multiple
# availability zones (availability), and move the containers
# to private subnets (security)
FargateSubnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref FargateVPC
CidrBlock: 10.0.16.0/20
MapPublicIpOnLaunch: true
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: {Ref: 'AWS::Region'}
FargateSubnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref FargateVPC
CidrBlock: 10.0.32.0/20
MapPublicIpOnLaunch: true
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: {Ref: 'AWS::Region'}
FargateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref FargateVPC
FargateInternetRoute:
Type: AWS::EC2::Route
DependsOn: FargateInternetAttachment
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref FargateInternetGateway
RouteTableId: !Ref FargateRouteTable
FargateRouteTableAssociation1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref FargateRouteTable
SubnetId: !Ref FargateSubnet1
FargateRouteTableAssociation2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref FargateRouteTable
SubnetId: !Ref FargateSubnet2
FargateSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: "Fargate firewall"
GroupDescription: "inbound to 80, outbound to anything"
VpcId: !Ref FargateVPC
SecurityGroupIngress: # In production, this should only accept traffic from the load balancer
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0