-
Notifications
You must be signed in to change notification settings - Fork 16
/
template.yml
354 lines (309 loc) · 9.91 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
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
AWSTemplateFormatVersion: 2010-09-09
Parameters:
EnvironmentName:
Type: String
VpcId:
Type: String
PrivateSubnetIds:
Type: String
PublicSubnetIds:
Type: String
Mappings:
Beanstalk2Route53HostedZoneId:
us-west-2:
HostedZoneId: Z33MTJ483KN6FU
us-east-1:
HostedZoneId: Z3DZXE0Q79N41H
us-west-1:
HostedZoneId: Z1M58G0W56PQJA
eu-west-1:
HostedZoneId: Z3NF1Z3NOM5OY2
ap-northeast-1:
HostedZoneId: Z2YN17T5R711GT
ap-southeast-1:
HostedZoneId: Z1WI8VXHPB1R38
sa-east-1:
HostedZoneId: Z2ES78Y61JGQKS
Env2Domain:
prod:
SSLCertificateId: ApiThenewbostonComCertificateArn
dev:
SSLCertificateId: ApiDevThenewbostonComCertificateArn
Conditions:
IsProd: !Equals [!Ref EnvironmentName, "prod"]
Resources:
WebsiteApiRecordSet:
Type: AWS::Route53::RecordSet
Properties:
Name:
!If [
IsProd,
"api.thenewboston.com",
!Sub "api-${EnvironmentName}.thenewboston.com",
]
Type: A
HostedZoneId: ZDHCZ05S94NJT # thenewboston.com
AliasTarget:
DNSName: !GetAtt WebsiteApiEnvironment.EndpointURL
HostedZoneId:
Fn::FindInMap:
[Beanstalk2Route53HostedZoneId, !Ref AWS::Region, HostedZoneId]
WebsiteApiServiceRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
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
WebsiteApiRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
WebsiteApiPolicies:
Type: AWS::IAM::Policy
Properties:
PolicyName: App
Roles:
- !Ref WebsiteApiRole
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "*"
Resource: "*"
WebsiteApiInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref WebsiteApiRole
WebsiteApiSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub "${EnvironmentName}-website-api-http-ingress-sg"
GroupDescription: !Sub "${EnvironmentName} WebsiteApi Security group to HTTP to allow connect"
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
PGSQLSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: !Sub "${EnvironmentName}-website-api-postgresql-ingress-sg"
GroupDescription: !Sub "${EnvironmentName} WebsiteApi Security group to RDS to allow connect"
VpcId: !Ref VpcId
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 5432
ToPort: 5432
CidrIp: 0.0.0.0/0
WebsiteApiPGInstance:
Type: AWS::RDS::DBInstance
DeletionPolicy: Snapshot
UpdateReplacePolicy: Snapshot
Properties:
AllocatedStorage: "20"
AllowMajorVersionUpgrade: false
AutoMinorVersionUpgrade: true
BackupRetentionPeriod: 10
CopyTagsToSnapshot: true
DBInstanceClass: "db.t2.micro"
DBName: !Sub "${EnvironmentName}WebsiteApi"
DBSubnetGroupName: !Ref WebsiteApiDBSubnetGroup
EnableIAMDatabaseAuthentication: true
Engine: postgres
EngineVersion: "12.4"
MasterUsername: root
MasterUserPassword: "MasterUserPassword"
StorageType: gp2
MultiAZ: false
VPCSecurityGroups:
- !Ref PGSQLSecurityGroup
Tags:
- Key: Name
Value: !Sub ${EnvironmentName} PGInstance
WebsiteApiDBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: !Sub ${EnvironmentName} WebsiteApiDBSubnetGroup
DBSubnetGroupName: !Sub ${EnvironmentName} WebsiteApiDBSubnetGroup
SubnetIds:
Fn::Split:
- ","
- Ref: PrivateSubnetIds
Tags:
- Key: Name
Value: !Sub ${EnvironmentName} WebsiteApiDBSubnetGroup
WebsiteApiApp:
Type: AWS::ElasticBeanstalk::Application
Properties:
Description: WebSiteApi Beanstalk App
WebsiteApiAppVersion:
Type: AWS::ElasticBeanstalk::ApplicationVersion
Properties:
ApplicationName: !Ref WebsiteApiApp
Description: WebSiteApi Beanstalk App Version
SourceBundle:
S3Bucket: _ARTIFACT_BUCKET_
S3Key: website-api/code._COMMIT_.zip
WebsiteApiConfigurationTemplate:
Type: AWS::ElasticBeanstalk::ConfigurationTemplate
Properties:
ApplicationName: !Ref WebsiteApiApp
Description: WebSiteApi Beanstalk App Configuration Template
SolutionStackName: 64bit Amazon Linux 2 v3.1.2 running Python 3.7
OptionSettings:
- Namespace: aws:elasticbeanstalk:environment
OptionName: EnvironmentType
Value: LoadBalanced
- Namespace: aws:elasticbeanstalk:environment
OptionName: ServiceRole
Value: !Ref WebsiteApiServiceRole
- Namespace: aws:autoscaling:launchconfiguration
OptionName: IamInstanceProfile
Value: !Ref WebsiteApiInstanceProfile
- Namespace: aws:autoscaling:launchconfiguration
OptionName: SecurityGroups
Value: !Ref WebsiteApiSecurityGroup
- Namespace: aws:autoscaling:asg
OptionName: MinSize
Value: "1"
- Namespace: aws:autoscaling:asg
OptionName: MaxSize
Value: "2"
- Namespace: aws:elasticbeanstalk:cloudwatch:logs
OptionName: StreamLogs
Value: "true"
- Namespace: aws:elasticbeanstalk:cloudwatch:logs
OptionName: DeleteOnTerminate
Value: "true"
- Namespace: aws:ec2:vpc
OptionName: VPCId
Value: !Ref VpcId
- Namespace: aws:ec2:vpc
OptionName: Subnets
Value: !Ref PrivateSubnetIds
- Namespace: aws:ec2:vpc
OptionName: ELBSubnets
Value: !Ref PublicSubnetIds
- Namespace: aws:elb:listener:443
OptionName: SSLCertificateId
Value:
Fn::ImportValue: !Sub
- ${FoundInMap}
- {
FoundInMap:
!FindInMap [
Env2Domain,
!Ref EnvironmentName,
SSLCertificateId,
],
}
- Namespace: aws:elb:listener:443
OptionName: ListenerProtocol
Value: HTTPS
- Namespace: aws:elb:listener:443
OptionName: InstancePort
Value: 80
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: DJANGO_APPLICATION_ENVIRONMENT
Value: "production"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: POSTGRES_DB
Value: !Sub "${EnvironmentName}WebsiteApi"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: POSTGRES_USER
Value: "root"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: POSTGRES_PASSWORD
Value: "MasterUserPassword"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: POSTGRES_HOST
Value: !GetAtt WebsiteApiPGInstance.Endpoint.Address
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: SENTRY_DSN
Value:
Fn::Join:
- ""
- - "{{resolve:ssm:"
- !Sub "${EnvironmentName}SentryDsn"
- ":1}}"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: ENVIRONMENT
Value:
Fn::Join:
- ""
- - "{{resolve:ssm:"
- !Sub "${EnvironmentName}Env"
- ":1}}"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: SMTP_USER
Value:
Fn::Join:
- ""
- - "{{resolve:ssm:"
- !Sub "${EnvironmentName}SmtpUser"
- ":1}}"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: SMTP_PASSWORD
Value:
Fn::Join:
- ""
- - "{{resolve:ssm:"
- !Sub "${EnvironmentName}SmtpPassword"
- ":1}}"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: SMTP_SERVER
Value:
Fn::Join:
- ""
- - "{{resolve:ssm:"
- !Sub "SmtpServer"
- ":1}}"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: DEFAULT_FROM_EMAIL
Value:
Fn::Join:
- ""
- - "{{resolve:ssm:"
- !Sub "EmailFrom"
- ":1}}"
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: SECRET_KEY
Value:
Fn::Join:
- ""
- - "{{resolve:ssm:"
- !Sub "${EnvironmentName}Secret"
- ":1}}"
WebsiteApiEnvironment:
Type: AWS::ElasticBeanstalk::Environment
Properties:
Description: WebSiteApi Beanstalk App Environment
ApplicationName: !Ref WebsiteApiApp
TemplateName: !Ref WebsiteApiConfigurationTemplate
VersionLabel: !Ref WebsiteApiAppVersion