forked from snyk/snyk-iac-cloudformation
-
Notifications
You must be signed in to change notification settings - Fork 3
/
elastic-beanstalk.yml
447 lines (374 loc) · 12.6 KB
/
elastic-beanstalk.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
---
AWSTemplateFormatVersion: '2010-09-09'
Description: Elastic Beanstalk
# App stack creation prerequisites: first create a VPC stack, then a DB stack.
Parameters:
ApplicationName:
Description: Name of your application
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: "^[a-zA-Z][-a-zA-Z0-9]*$"
StackType:
Description: node, rails, python, python3 or spring
Type: String
MinLength: 1
MaxLength: 255
AllowedValues:
- node
- rails
- spring
- python
- python3
ConstraintDescription: Specify node, rails, python, python3 or spring
EnvironmentName:
Description: Environment name, either dev or prod.
Type: String
MinLength: 1
MaxLength: 255
AllowedValues:
- dev
- prod
ConstraintDescription: Specify either dev or prod
NetworkStackName:
Description: Name of an active CloudFormation stack of networking resources
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: "^[a-zA-Z][-a-zA-Z0-9]*$"
DatabaseStackName:
Description: Name of an active CloudFormation stack of database resources
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: "^[a-zA-Z][-a-zA-Z0-9]*$"
DatabaseName:
Description: Database name (schema).
Type: String
MinLength: 1
MaxLength: 255
AllowedPattern: "^[a-zA-Z][-a-zA-Z0-9]*$"
DatabasePassword:
NoEcho: true
Type: String
Description: Database admin account password
MinLength: 6
MaxLength: 41
AllowedPattern: "[a-zA-Z0-9]*"
ConstraintDescription: Password must contain only alphanumeric characters
AppS3Bucket:
Description: S3 Bucket containing your application package.
Type: String
MinLength: 1
MaxLength: 255
AppS3Key:
Description: S3 Bucket key for your application package
Type: String
MinLength: 1
MaxLength: 255
EC2KeyPairName:
Description: EC2 key pair name for SSH access
Type: AWS::EC2::KeyPair::KeyName
DevInstanceType:
Description: The instance type for the dev environment
Type: String
MinLength: 1
MaxLength: 255
Default: t2.micro
ProdInstanceType:
Description: The instance type for the prod environment
Type: String
MinLength: 1
MaxLength: 255
Default: t2.large
SSLCertificateArn:
Description: The SSL/TLS certificate ARN
Type: String
MinLength: 0
MaxLength: 2048
Default: ""
AutoScalingMinInstanceCount:
Description: Minimum number of EC2 instances for Auto Scaling
Type: Number
MinValue: 1
MaxValue: 20
Default: 2
ConstraintDescription: Specify a number between 1 - 20
AutoScalingMaxInstanceCount:
Description: Maximum number of EC2 instances for Auto Scaling
Type: Number
MinValue: 1
MaxValue: 20
Default: 6
ConstraintDescription: Specify a number between 1 - 20
Conditions:
CreateProdEnv: !Equals [ !Ref EnvironmentName, prod ]
TlsEnabled: !Not [ !Equals [ !Ref SSLCertificateArn, "" ] ]
Mappings:
# Maps stack type parameter to solution stack name string
StackMap:
node:
stackName: 64bit Amazon Linux 2018.03 v4.5.3 running Node.js
rails:
stackName: 64bit Amazon Linux 2018.03 v2.8.3 running Ruby 2.4 (Puma)
spring:
stackName: 64bit Amazon Linux 2018.03 v3.0.3 running Tomcat 8 Java 8
python:
stackName: 64bit Amazon Linux 2018.03 v2.7.3 running Python 2.7
python3:
stackName: 64bit Amazon Linux 2018.03 v2.7.3 running Python 3.6
Resources:
ElasticBeanstalkServiceRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "elasticbeanstalk.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkEnhancedHealth
- arn:aws:iam::aws:policy/service-role/AWSElasticBeanstalkService
Application:
Type: AWS::ElasticBeanstalk::Application
Properties:
ApplicationName: !Ref ApplicationName
ApplicationVersion:
Type: AWS::ElasticBeanstalk::ApplicationVersion
Properties:
ApplicationName: !Ref Application
SourceBundle:
S3Bucket: !Ref AppS3Bucket
S3Key: !Ref AppS3Key
Environment:
Type: AWS::ElasticBeanstalk::Environment
Properties:
EnvironmentName: !Sub "${ApplicationName}-${EnvironmentName}"
ApplicationName: !Ref Application
TemplateName: !Ref ConfigurationTemplate
VersionLabel: !Ref ApplicationVersion
DependsOn:
- ConfigurationTemplate
- ApplicationVersion
# The configuration template contains environment parameters such as those
# that relate to the autoscaling group (e.g. size, triggers), placement of
# resources in the VPC, load balancer setup, and environment variables
ConfigurationTemplate:
Type: AWS::ElasticBeanstalk::ConfigurationTemplate
Properties:
ApplicationName: !Ref Application
SolutionStackName: !FindInMap [ StackMap, !Ref StackType, stackName ]
OptionSettings:
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: LoadBalanced
- Namespace: aws:elasticbeanstalk:environment
OptionName: LoadBalancerType
Value: application
- Namespace: aws:elasticbeanstalk:environment
OptionName: ServiceRole
Value: !Ref ElasticBeanstalkServiceRole
# AUTOSCALING OPTIONS
- Namespace: aws:autoscaling:asg
OptionName: MinSize
Value: !Ref AutoScalingMinInstanceCount
- Namespace: aws:autoscaling:asg
OptionName: MaxSize
Value: !Ref AutoScalingMaxInstanceCount
- Namespace: aws:autoscaling:launchconfiguration
OptionName: SecurityGroups
Value:
Fn::ImportValue: !Sub "${NetworkStackName}-AppSecurityGroupID"
- Namespace: aws:autoscaling:launchconfiguration
OptionName: SSHSourceRestriction
Value:
"Fn::Join":
- ','
- - 'tcp, 22, 22'
- !ImportValue
"Fn::Sub": "${NetworkStackName}-BastionGroupID"
- Namespace: aws:autoscaling:launchconfiguration
OptionName: InstanceType
Value: !If [ CreateProdEnv, !Ref ProdInstanceType, !Ref DevInstanceType ]
- Namespace: aws:autoscaling:launchconfiguration
OptionName: IamInstanceProfile
Value: !Ref AppInstanceProfile
- Namespace: aws:autoscaling:launchconfiguration
OptionName: EC2KeyName
Value: !Ref EC2KeyPairName
- Namespace: aws:autoscaling:updatepolicy:rollingupdate
OptionName: RollingUpdateEnabled
Value: true
- Namespace: aws:autoscaling:updatepolicy:rollingupdate
OptionName: RollingUpdateType
Value: Health
- Namespace: aws:autoscaling:trigger
OptionName: MeasureName
Value: CPUUtilization
- Namespace: aws:autoscaling:trigger
OptionName: Unit
Value: Percent
- Namespace: aws:autoscaling:trigger
OptionName: UpperThreshold
Value: 80
- Namespace: aws:autoscaling:trigger
OptionName: LowerThreshold
Value: 40
# VPC OPTIONS (PLACEMENT OF RESOURCES IN SUBNETS)
- Namespace: aws:ec2:vpc
OptionName: VPCId
Value:
Fn::ImportValue: !Sub "${NetworkStackName}-VpcID"
- Namespace: aws:ec2:vpc
OptionName: Subnets
Value:
"Fn::Join":
- ','
- - !ImportValue
"Fn::Sub": "${NetworkStackName}-PrivateSubnet1ID"
- !ImportValue
"Fn::Sub": "${NetworkStackName}-PrivateSubnet2ID"
- Namespace: aws:ec2:vpc
OptionName: ELBSubnets
Value:
"Fn::Join":
- ','
- - !ImportValue
"Fn::Sub": "${NetworkStackName}-PublicSubnet1ID"
- !ImportValue
"Fn::Sub": "${NetworkStackName}-PublicSubnet2ID"
- Namespace: aws:elbv2:listener:default
OptionName: ListenerEnabled
Value: !If [ TlsEnabled, false, true ]
- Namespace: aws:elbv2:loadbalancer
OptionName: SecurityGroups
Value:
Fn::ImportValue: !Sub "${NetworkStackName}-ELBSecurityGroupID"
- Namespace: aws:elbv2:loadbalancer
OptionName: ManagedSecurityGroup
Value:
Fn::ImportValue: !Sub "${NetworkStackName}-ELBSecurityGroupID"
- Namespace: aws:elbv2:listenerrule:default
OptionName: PathPatterns
Value: "/*"
- Namespace: !Sub
- "aws:elbv2:listener:${ListenPort}"
- ListenPort:
"Fn::ImportValue": !Sub "${NetworkStackName}-ELBIngressPort"
OptionName: Protocol
Value: !If [ TlsEnabled, HTTPS, HTTP ]
- Namespace: !Sub
- "aws:elbv2:listener:${ListenPort}"
- ListenPort:
"Fn::ImportValue": !Sub "${NetworkStackName}-ELBIngressPort"
OptionName: Rules
Value: default
- Namespace: !Sub
- "aws:elbv2:listener:${ListenPort}"
- ListenPort:
"Fn::ImportValue": !Sub "${NetworkStackName}-ELBIngressPort"
OptionName: SSLCertificateArns
Value: !Ref SSLCertificateArn
# CLOUDWATCH LOGS
- Namespace: aws:elasticbeanstalk:cloudwatch:logs
OptionName: StreamLogs
Value: true
- Namespace: aws:elasticbeanstalk:cloudwatch:logs
OptionName: DeleteOnTerminate
Value: true
# ENVIRONMENT VARIABLES - COMMON TO ALL STACKS
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: AWS_REGION
Value: !Ref AWS::Region
# ENVIRONMENT VARIABLES - NODE, RAILS - Move to parameter store
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: DB_PASSWORD
Value: !Ref DatabasePassword
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: DB_USER
Value:
Fn::ImportValue: !Sub ${DatabaseStackName}-DatabaseUser
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: DB_NAME
Value: !Ref DatabaseName
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: DB_HOST
Value:
Fn::ImportValue: !Sub "${DatabaseStackName}-DatabaseURL"
# ENVIRONMENT VARIABLES - SPRING
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: spring.datasource.password
Value: !Ref DatabasePassword
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: spring.datasource.username
Value:
Fn::ImportValue: !Sub "${DatabaseStackName}-DatabaseUser"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: spring.datasource.url
Value:
"Fn::Join":
- ''
- - 'jdbc:mysql://'
- !ImportValue
"Fn::Sub": "${DatabaseStackName}-DatabaseURL"
- ':3306/'
- Ref: DatabaseName
# IAM resources
AppRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
AppPolicies:
Type: AWS::IAM::Policy
Properties:
PolicyName: App
Roles:
- !Ref AppRole
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Action: "*"
Resource: "*"
AppInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref AppRole
Outputs:
Name:
Description: Elastic Beanstalk Stack Name
Value: !Ref AWS::StackName
Export:
Name: !Sub ${AWS::StackName}-Name
EnvironmentURL:
Description: Environment URL
Value: !GetAtt Environment.EndpointURL
Export:
Name: !Sub "${AWS::StackName}-EnvironmentURL"
EnvironmentName:
Description: Environment Name
Value: !Sub "${ApplicationName}-${EnvironmentName}"
Export:
Name: !Sub "${AWS::StackName}-EnvironmentName"
TypeOfStack:
Description: Stack type
Value: !Ref StackType
Export:
Name: !Sub "${AWS::StackName}-TypeOfStack"