-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.yaml
299 lines (262 loc) · 8.53 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
AWSTemplateFormatVersion: 2010-09-09
Description: |
This is the AWS CFN template for the IB Group Project,
"Riffr" by Team Papa
######## ### ######## ### ## ##
## ## ## ## ## ## ## ## ### ###
## ## ## ## ## ## ## ## #### ####
######## ## ## ######## ## ## ## ### ##
## ######### ## ## ######### ## ##
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ##
Parameters:
DomainName:
Type: String
Default: riffr.net
Stage:
Description: Development stage
Type: String
Default: dev
AllowedValues: [dev, prod]
Port:
Description: Port for the Backend Server
Type: Number
Default: 10000
KeyName:
Type: AWS::EC2::KeyPair::KeyName
Description: Name of an existing EC2 KeyPair for SSH access to the Backend Server Instance
AmiId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
VPCId:
Type: AWS::EC2::VPC::Id
Description: VPC Id
Default: vpc-cefb23b7
SubnetPublic1Id:
Type: AWS::EC2::Subnet::Id
Description: Public Subnet 1 in default VPC
Default: subnet-c245a089
SubnetPublic2Id:
Type: AWS::EC2::Subnet::Id
Description: Public Subnet 2 in default VPC
Default: subnet-80bdb7e6
CloudfrontHttpsCertificateArn:
Type: String
Description: Certificate Arn for the Cloudfront Distribution
CreateHttpsResources:
Type: String
Default: false
AllowedValues:
- false
- true
Conditions:
CreateHttpsResources: !Equals [!Ref CreateHttpsResources, true]
Resources:
###### ## ####### ## ## ######## ######## ######## ####### ## ## ########
## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## #### ## ##
## ## ## ## ## ## ## ## ###### ######## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## #### ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ##
###### ######## ####### ####### ######## ## ## ## ####### ## ## ##
CloudfrontDistribution:
Condition: CreateHttpsResources
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- !Sub site.${Stage}.${DomainName}
ViewerCertificate:
MinimumProtocolVersion: TLSv1
SslSupportMethod: sni-only
AcmCertificateArn: !Ref CloudfrontHttpsCertificateArn
Enabled: True
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
CachedMethods:
- GET
- HEAD
ForwardedValues:
QueryString: True
TargetOriginId: WebsiteBucket
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
Origins:
- Id: WebsiteBucket
DomainName: !GetAtt WebsiteBucket.DomainName
CustomOriginConfig:
OriginProtocolPolicy: match-viewer
###### #######
## ## ## ##
## ##
###### #######
## ##
## ## ## ##
###### #######
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: PublicRead
BucketName: !Sub ${AWS::AccountId}-${AWS::StackName}-${Stage}-website-bucket
WebsiteConfiguration:
IndexDocument: "index.html"
ErrorDocument: "index.html"
WebsiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebsiteBucket
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: AllowPublicReadAccess
Effect: Allow
Principal: "*"
Action:
- s3:GetObject
Resource:
- !Sub ${WebsiteBucket.Arn}/*
######## ####### ## ## ######## ######## ######## #######
## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ##
######## ## ## ## ## ## ###### ####### #######
## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ##
## ## ####### ####### ## ######## ###### #######
HostedZone:
Type: AWS::Route53::HostedZone
Properties:
Name: !Sub ${Stage}.${DomainName}
CloudfrontAliasRecord:
Condition: CreateHttpsResources
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZone
Name: !Sub site.${Stage}.${DomainName}
Type: A
AliasTarget:
DNSName: !GetAtt CloudfrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2 #https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget-1.html#cfn-route53-aliastarget-hostedzoneid
ALBAliasRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: !Ref HostedZone
Name: !Sub alb.${Stage}.${DomainName}
Type: A
AliasTarget:
DNSName: !GetAtt ALB.DNSName
HostedZoneId: !GetAtt ALB.CanonicalHostedZoneID
ALBHttpsCertificate:
Condition: CreateHttpsResources
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Sub alb.${Stage}.${DomainName}
DomainValidationOptions:
- DomainName: !Sub alb.${Stage}.${DomainName}
HostedZoneId: !Ref HostedZone
ValidationMethod: DNS
### ## ########
## ## ## ## ##
## ## ## ## ##
## ## ## ########
######### ## ## ##
## ## ## ## ##
## ## ######## ########
ALB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Subnets:
- !Ref SubnetPublic1Id
- !Ref SubnetPublic2Id
SecurityGroups:
- !Ref ALBSecurityGroup
Type: application
Scheme: internet-facing
ALBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPCId
GroupDescription: Access to the ALB that sits in front of EC2
SecurityGroupIngress:
- IpProtocol: "-1"
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-alb-sg
ALBListenerHttp:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref ALB
Port: 80
Protocol: HTTP
DefaultActions:
- Type: forward
TargetGroupArn: !Ref EC2TargetGroup
ALBListenerHttps:
Condition: CreateHttpsResources
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref ALB
Port: 443
Protocol: HTTPS
Certificates:
- CertificateArn: !Ref ALBHttpsCertificate
DefaultActions:
- Type: forward
TargetGroupArn: !Ref EC2TargetGroup
EC2TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: !Sub ${AWS::StackName}-alb-ec2-tg
VpcId: !Ref VPCId
Port: !Ref Port
Protocol: HTTP
Targets:
- Id: !Ref EC2Instance
Port: !Ref Port
HealthCheckIntervalSeconds: 60
HealthCheckTimeoutSeconds: 10
HealthyThresholdCount: 2
HealthCheckProtocol: HTTP
HealthCheckPath: /socket.io/?EIO=4&transport=polling
Matcher:
HttpCode: "200"
######## ###### #######
## ## ## ## ##
## ## ##
###### ## #######
## ## ##
## ## ## ##
######## ###### #########
EC2Instance:
Type: AWS::EC2::Instance
Properties:
KeyName: !Ref KeyName
ImageId: !Ref AmiId
InstanceType: t2.micro
NetworkInterfaces:
- DeviceIndex: '0'
AssociatePublicIpAddress: True
GroupSet:
- !Ref EC2SecurityGroup
SubnetId:
!Ref SubnetPublic1Id
EC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPCId
GroupDescription: Accesss to the EC2 instances
SecurityGroupIngress:
- IpProtocol: "-1"
SourceSecurityGroupId: !Ref ALBSecurityGroup
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Outputs:
WebsiteUrl:
Value: !GetAtt WebsiteBucket.WebsiteURL
Description: URL for the frontend website on S3