-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.yaml
338 lines (309 loc) · 10 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
AWSTemplateFormatVersion: '2010-09-09'
Transform:
- AWS::LanguageExtensions
- AWS::Serverless-2016-10-31
Description: "EC Postcode Lookup app: Lambda, API Gateway"
Globals:
Function:
Timeout: 10
Api:
BinaryMediaTypes:
- "*/*"
Parameters:
FQDN:
Default: FQDN
Description: "The domain name this app is mounted on"
Type: AWS::SSM::Parameter::Value<String>
CertificateArn:
Default: CertificateArn
Description: "The ARN of the cert to use"
Type: AWS::SSM::Parameter::Value<String>
XForwardedForHost:
Default: XForwardedForHost
Description: "The ARN of the cert to use"
Type: AWS::SSM::Parameter::Value<String>
AppAPIKey:
Default: AppAPIKey
Description: "The DC aggregator API key"
Type: AWS::SSM::Parameter::Value<String>
AppSentryDSN:
Default: SENTRY_DSN
Description: "The Sentry DSN"
Type: AWS::SSM::Parameter::Value<String>
DCEnvironment:
Default: DC_ENVIRONMENT
Description: "The DC_ENVIRONMENT environment variable passed to the app."
Type: AWS::SSM::Parameter::Value<String>
Conditions:
UseBasicAuth: !Or
- !Equals [ !Ref DCEnvironment, development ]
- !Equals [ !Ref DCEnvironment, staging ]
Resources:
ECDeployerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
RoleName: ECDeployer
ECPostcodeLookupFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: ECPostcodeLookupFunction
Timeout: 60
CodeUri: postcode_lookup
Handler: app.handler
Runtime: python3.12
MemorySize: 512
Environment:
Variables:
FQDN: !Ref FQDN
API_KEY: !Ref AppAPIKey
SENTRY_DSN: !Ref AppSentryDSN
DC_ENVIRONMENT: !Ref DCEnvironment
Events:
HTTPRequests:
Type: Api
Properties:
RestApiId: !Ref ECPostcodeLookupFunctionApiGateway
Path: /{proxy+}
Method: ANY
HTTPRequestRoots:
Type: Api
Properties:
RestApiId: !Ref ECPostcodeLookupFunctionApiGateway
Path: /
Method: ANY
ECPostcodeLookupFunctionLogGroup:
Type: AWS::Logs::LogGroup
DependsOn: [ ECPostcodeLookupFunction ]
Properties:
LogGroupName: !Sub /aws/lambda/${ECPostcodeLookupFunction}
RetentionInDays: 60
ECPostcodeLookupFunctionApiGateway:
Type: AWS::Serverless::Api
Properties:
AlwaysDeploy: True
StageName: Prod
Cors:
AllowMethods: "'GET'"
AllowOrigin: "'*'"
MaxAge: "'600'"
Auth:
DefaultAuthorizer: !If [ UseBasicAuth, "BasicAuthFunction", !Ref AWS::NoValue]
Authorizers:
BasicAuthFunction:
FunctionArn: !GetAtt BasicAuthFunction.Arn
FunctionPayloadType: REQUEST
Identity:
Headers:
- Authorization
ReauthorizeEvery: 3600
BasicAuthGatewayResponse:
Condition: UseBasicAuth
Type: AWS::ApiGateway::GatewayResponse
Properties:
ResponseParameters:
gatewayresponse.header.www-authenticate: "'Basic realm=\"Restricted\"'"
ResponseType: UNAUTHORIZED
RestApiId: !Ref ECPostcodeLookupFunctionApiGateway
StatusCode: '401'
BasicAuthFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./postcode_lookup/
Handler: lambda_basic_auth.lambda_handler
Runtime: python3.12
FailOver:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${FQDN}-failover"
AccessControl: Private
DeletionPolicy: Delete
FailOverBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
PolicyDocument:
Statement:
- Action:
- 's3:*'
Effect: 'Allow'
Principal:
CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
Resource:
- !Sub 'arn:aws:s3:::${FailOver}/*'
Bucket: !Ref FailOver
StaticPages:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "${FQDN}-static-pages"
AccessControl: Private
DeletionPolicy: Delete
StaticPagesBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
PolicyDocument:
Id: FailOverBucketPolicy
Version: 2012-10-17
Statement:
- Action:
- 's3:*'
Effect: 'Allow'
Principal:
CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
Resource:
- !Sub 'arn:aws:s3:::${StaticPages}/*'
Bucket: !Ref StaticPages
CloudFrontDistribution:
Type: 'AWS::CloudFront::Distribution'
Properties:
DistributionConfig:
Comment: 'Cloudfront Distribution pointing to Lambda origin'
Origins:
- Id: Dynamic
DomainName: !Sub "${ECPostcodeLookupFunctionApiGateway}.execute-api.${AWS::Region}.amazonaws.com"
OriginPath: "/Prod"
CustomOriginConfig:
OriginProtocolPolicy: "https-only"
OriginCustomHeaders:
- HeaderName: X-Forwarded-Host
HeaderValue: !Ref XForwardedForHost
- HeaderName: X-Forwarded-Proto
HeaderValue: https
OriginShield:
Enabled: true
OriginShieldRegion: eu-west-2
- Id: StaticPages
DomainName: !GetAtt StaticPages.RegionalDomainName
ConnectionAttempts: 1
ConnectionTimeout: 2
OriginShield:
Enabled: true
OriginShieldRegion: eu-west-2
OriginPath: ''
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
- Id: Failover
DomainName: !GetAtt FailOver.RegionalDomainName
# Keep the ? to convert the path in to a query param
OriginPath: "/index.html?"
OriginShield:
Enabled: true
OriginShieldRegion: eu-west-2
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
OriginGroups:
Quantity: 2
Items:
- Id: DynamicPagesFailOverOriginGroup
FailoverCriteria:
StatusCodes:
Quantity: 6
Items:
- 404
- 400
- 500
- 502
- 503
- 504
Members:
Quantity: 2
Items:
- OriginId: Dynamic
- OriginId: Failover
- Id: StaticPagesOriginGroup
FailoverCriteria:
StatusCodes:
Quantity: 6
Items:
- 404
- 400
- 500
- 502
- 503
- 504
Members:
Quantity: 2
Items:
- OriginId: StaticPages
- OriginId: Dynamic
Enabled: true
HttpVersion: 'http2'
Aliases:
- !Ref FQDN
PriceClass: "PriceClass_100"
ViewerCertificate:
AcmCertificateArn: !Ref CertificateArn
MinimumProtocolVersion: TLSv1.1_2016
SslSupportMethod: sni-only
DefaultCacheBehavior:
AllowedMethods: [ GET, HEAD, OPTIONS ]
TargetOriginId: DynamicPagesFailOverOriginGroup
DefaultTTL: '60'
ForwardedValues:
QueryString: true
Cookies:
Forward: "all"
Headers:
- Authorization
- Origin
- Referer
ViewerProtocolPolicy: "redirect-to-https"
CacheBehaviors:
- AllowedMethods: [ GET, HEAD, OPTIONS ]
PathPattern: static/*
TargetOriginId: Dynamic
Compress: true
ViewerProtocolPolicy: "redirect-to-https"
ForwardedValues:
QueryString: true
Cookies:
Forward: none
Headers:
- Authorization
- Origin
MinTTL: '50'
- AllowedMethods: [ GET, HEAD, OPTIONS ]
PathPattern: /i-am-a/voter/your-election-information
TargetOriginId: StaticPagesOriginGroup
Compress: true
DefaultTTL: '60'
MaxTTL: '60'
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
ViewerProtocolPolicy: "redirect-to-https"
- AllowedMethods: [ GET, HEAD, OPTIONS ]
PathPattern: /cy/rwyf-yneg-pleidleisiwr/pleidleisiwr/gwybodaeth-etholiad
TargetOriginId: StaticPagesOriginGroup
Compress: true
DefaultTTL: '60'
MaxTTL: '60'
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
ViewerProtocolPolicy: "redirect-to-https"
CfOriginAccessIdentity:
Metadata:
Comment: 'Access S3 bucket content only through CloudFront'
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: 'Access S3 bucket content only through CloudFront'
Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'
DnsRecord:
Type: AWS::Route53::RecordSet
Properties:
AliasTarget:
DNSName: !GetAtt CloudFrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2 # this is an AWS-owned, global singleton required for Aliases to CloudFront
HostedZoneName: !Sub "${FQDN}."
Name: !Sub "${FQDN}."
Type: A
Outputs:
ECPostcodeLookupFqdn:
Description: "API Gateway endpoint FQDN for EC Postcode Lookup function"
Value: !Sub "${ECPostcodeLookupFunctionApiGateway}.execute-api.${AWS::Region}.amazonaws.com"
Export:
Name: !Join [ ":", [ !Ref "AWS::StackName", "ECPostcodeLookupFqdn" ] ]