-
Notifications
You must be signed in to change notification settings - Fork 4
/
stack-lambda.yaml
287 lines (265 loc) · 8.44 KB
/
stack-lambda.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
AWSTemplateFormatVersion: 2010-09-09
Description: Web service to host a Datasette instance on AWS.
Parameters:
Bucket:
Type: String
CORS:
Type: String
Description: Whether to start the Datasette service with the --cors flag.
AllowedPattern: ^True$|^False$
DbFiles:
Type: String
Description: The names of the DB files to serve, separated by @ symbols.
Domain:
Type: String
Default: ''
Description: The custom CNAME for the CloudFront distribution, e.g. foo.example.com.
HostedZoneId:
Type: String
Default: ''
Description: The Route 53 Zone ID for apex domain containing the CNAME, e.g. Z39FM...
Prefix:
Type: String
Default: ''
Description: The path on which the Datasette service is hosted.
ValidationDomain:
Type: String
Default: ''
Description: The apex domain containing the CNAME, e.g. example.com.
Conditions:
CreateCNAME: !Not [!Equals [ !Ref HostedZoneId, '' ]]
Resources:
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: !Ref Bucket
S3Key: package.zip
Runtime: python3.8
Description: !Sub ${AWS::StackName} Datasette
FunctionName: !Sub ${AWS::StackName}-${AWS::AccountId}
Handler: index.handler
MemorySize: 1536
Role: !GetAtt LambdaIAMRole.Arn
Timeout: 15
Layers:
- arn:aws:lambda:us-east-1:562933535245:layer:datasette-0_39:11
Environment:
Variables:
Bucket: !Ref Bucket
CORS: !Ref CORS
DbFiles: !Ref DbFiles
Prefix: !Ref Prefix
LambdaIAMRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${AWS::StackName}-${AWS::AccountId}:*
- Action:
- s3:GetObject
- s3:HeadObject
Effect: Allow
Resource:
- !Sub arn:aws:s3:::${Bucket}/*
PolicyName: lambda
LambdaLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub /aws/lambda/${AWS::StackName}-${AWS::AccountId}
RetentionInDays: 14
ApiGateway:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub ${AWS::StackName} Datasette Gateway
EndpointConfiguration:
Types:
- REGIONAL
RootMethod:
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: NONE
HttpMethod: ANY
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
PassthroughBehavior: WHEN_NO_MATCH
IntegrationResponses:
- StatusCode: '200'
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaArn}/invocations
- LambdaArn: !GetAtt LambdaFunction.Arn
ResourceId: !GetAtt ApiGateway.RootResourceId
RestApiId: !Ref ApiGateway
ProxyMethod:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: ANY
AuthorizationType: NONE
RequestParameters:
method.request.path.proxy: true
Integration:
IntegrationHttpMethod: POST
Type: AWS_PROXY
PassthroughBehavior: WHEN_NO_MATCH
IntegrationResponses:
- StatusCode: '200'
RequestParameters:
integration.request.path.proxy: 'method.request.path.proxy'
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaArn}/invocations
- LambdaArn: !GetAtt LambdaFunction.Arn
ResourceId: !Ref Resource
RestApiId: !Ref ApiGateway
Resource:
Type: 'AWS::ApiGateway::Resource'
Properties:
ParentId: !GetAtt ApiGateway.RootResourceId
RestApiId: !Ref ApiGateway
PathPart: '{proxy+}'
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- RootMethod
- ProxyMethod
Properties:
RestApiId: !Ref ApiGateway
ApiStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref ApiGatewayDeployment
MethodSettings:
- HttpMethod: '*'
LoggingLevel: INFO
ResourcePath: '/'
DataTraceEnabled: true
RestApiId: !Ref ApiGateway
StageName: datasette
LambdaApiGatewayInvoke:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt LambdaFunction.Arn
Principal: apigateway.amazonaws.com
SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases: !If [ CreateCNAME, [ !Ref Domain ], !Ref "AWS::NoValue" ]
ViewerCertificate:
!If
- CreateCNAME
-
AcmCertificateArn: !Ref SSLCertificate
MinimumProtocolVersion: TLSv1
SslSupportMethod: sni-only
- !Ref "AWS::NoValue"
Origins:
- DomainName: !Sub ${ApiGateway}.execute-api.${AWS::Region}.amazonaws.com
Id: datasette
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginPath: /datasette
Enabled: true
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
TargetOriginId: datasette
ForwardedValues:
QueryString: true
Headers:
- Referer
- CloudFront-Viewer-Country
- CloudFront-Is-Desktop-Viewer
- CloudFront-Is-Mobile-Viewer
- CloudFront-Is-SmartTV-Viewer
- CloudFront-Is-Tablet-Viewer
- X-Forwarded-Host
- User-Agent
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN: !Ref CFAddHostLambdaFunctionVersion
PriceClass: PriceClass_100
# Add X-Forwarded-Host header
# See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#example-viewer-request
# for description of input record structure.
CFAddHostLambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const { headers } = request;
const hostname = headers.host[0].value;
headers['x-forwarded-host'] = [{
key:'X-Forwarded-Host',
value: hostname
}];
callback(null, request);
};
Runtime: nodejs10.x
Description: !Sub ${AWS::StackName} CloudFront X-Forwarded-Host
FunctionName: !Sub ${AWS::StackName}-cf-x-forwarded-host-${AWS::AccountId}
Handler: index.handler
MemorySize: 128
Role: !GetAtt LambdaIAMRole.Arn
Timeout: 5
CFAddHostLambdaFunctionVersion:
Type: AWS::Lambda::Version
Properties:
FunctionName: !Ref CFAddHostLambdaFunction
SSLCertificate:
Type: AWS::CertificateManager::Certificate
Condition: CreateCNAME
Properties:
DomainName: !Sub ${Domain}
ValidationMethod: EMAIL
DomainValidationOptions:
- DomainName: !Sub ${Domain}
ValidationDomain: !Sub ${ValidationDomain}
CNAMERecord:
Type: AWS::Route53::RecordSet
Condition: CreateCNAME
Properties:
HostedZoneId: !Sub ${HostedZoneId}
Name: !Sub ${Domain}
ResourceRecords:
- !GetAtt CloudFrontDistribution.DomainName
TTL: '60'
Type: CNAME
Outputs:
ApiUrl:
Value: !Sub
- https://${ApiId}.execute-api.${AWS::Region}.amazonaws.com/${StageName}
- ApiId: !Ref ApiGateway
StageName: !Ref ApiStage
CloudFrontUrl:
Value: !Sub
- https://${DomainName}
- DomainName: !GetAtt CloudFrontDistribution.DomainName