-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.yml
236 lines (220 loc) · 6.78 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
Transform: AWS::Serverless-2016-10-31
Parameters:
IsDev:
Type: String
Default: "true"
AllowedValues:
- "true"
- "false"
DomainName:
Type: String
AcmCertificateArn:
Type: String
Conditions:
IsDev: !Equals [!Ref IsDev, "true"]
IsProd: !Equals [!Ref IsDev, "false"]
Mappings:
Region:
us-east-1:
Ami: ami-03a45a5ac837f33b7
Subnets:
- subnet-c98797ae
- subnet-ecf3edc2
- subnet-2e8d2e63
- subnet-bf6f73e3
- subnet-f31334cd
- subnet-06c11408
Vpc: vpc-c9cee2b3
Resources:
WebsiteCloudFront:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
Origins:
- Id: WebsiteOrigin
DomainName: !GetAtt WebsiteLoadBalancer.DNSName
CustomOriginConfig:
OriginProtocolPolicy: !If [IsDev, http-only, https-only]
DefaultCacheBehavior:
TargetOriginId: WebsiteOrigin
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: !Ref CloudFrontCachePolicy
OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3
HttpVersion: http2
Aliases:
- !If [IsDev, !Ref AWS::NoValue, !Ref DomainName]
ViewerCertificate:
!If
- IsDev
- !Ref AWS::NoValue
- AcmCertificateArn: !Ref AcmCertificateArn
MinimumProtocolVersion: TLSv1.2_2021
SslSupportMethod: sni-only
Comment: !Ref AWS::StackName
CloudFrontCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
DefaultTTL: !If [IsDev, 0, 86400]
MaxTTL: !If [IsDev, 1, 31536000]
MinTTL: 0
Name: !Ref AWS::StackName
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingBrotli: !If [IsDev, false, true]
EnableAcceptEncodingGzip: !If [IsDev, false, true]
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: none
WebsiteBucket:
Type: AWS::S3::Bucket
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
WebsiteLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: !Ref AWS::StackName
Subnets: !FindInMap
- Region
- !Ref AWS::Region
- Subnets
SecurityGroups:
- !GetAtt WebsiteLoadBalancerSecurityGroup.GroupId
WebsiteLoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Join ["", ["load-balancer-", !Ref AWS::StackName]]
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !If [IsDev, 80, 443]
ToPort: !If [IsDev, 80, 443]
SourcePrefixListId: pl-3b927c52
WebsiteLoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref WebsiteLoadBalancer
Protocol: !If [IsDev, HTTP, HTTPS]
Port: !If [IsDev, 80, 443]
DefaultActions:
- Type: forward
TargetGroupArn: !Ref WebsiteLoadBalancerTargetGroup
Certificates:
!If
- IsDev
- !Ref AWS::NoValue
- - CertificateArn: !Ref AcmCertificateArn
WebsiteLoadBalancerTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: !Ref AWS::StackName
Protocol: HTTP
Port: 80
VpcId: !FindInMap
- Region
- !Ref AWS::Region
- Vpc
WebsiteAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
MinSize: 1
MaxSize: 1
LaunchTemplate:
LaunchTemplateId: !Ref WebsiteLaunchTemplate
Version: !GetAtt WebsiteLaunchTemplate.LatestVersionNumber
TargetGroupARNs:
- !Ref WebsiteLoadBalancerTargetGroup
AvailabilityZones:
Fn::GetAZs:
!Ref AWS::Region
WebsiteLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: !Ref AWS::StackName
LaunchTemplateData:
InstanceType: t4g.nano
IamInstanceProfile:
Arn: !GetAtt WebsiteInstanceProfile.Arn
ImageId: !FindInMap
- Region
- !Ref AWS::Region
- Ami
SecurityGroups:
- !Ref WebsiteInstanceSecurityGroup
UserData:
Fn::Base64:
Fn::Sub:
|
#!/bin/bash
export HOME=/home/ec2-user
yum update -y
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
nvm install 16
aws s3 sync s3://${WebsiteBucket} ~/website --delete
PORT=80 node ~/website/build
WebsiteInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Roles:
- !Ref WebsiteInstanceRole
WebsiteInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: !Join ["", ["WebsiteInstancePolicy", "-", !Ref AWS::StackName]]
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource: !Join ["", [!GetAtt WebsiteBucket.Arn, "/*"]]
- Effect: Allow
Action:
- s3:ListBucket
Resource: !GetAtt WebsiteBucket.Arn
WebsiteInstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Ref AWS::StackName
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !GetAtt WebsiteLoadBalancerSecurityGroup.GroupId
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
WebsiteWWWRedirectBucket:
Type: AWS::S3::Bucket
Condition: IsProd
Properties:
BucketName: !Join ["", ["www.", !Ref DomainName]]
WebsiteConfiguration:
RedirectAllRequestsTo:
HostName: !Ref DomainName
Protocol: https
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true