-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
268 lines (259 loc) · 9.09 KB
/
serverless.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
# prestonfrazier.net
#
# website-pf Serverless Deployment configuration
service: website-pf
plugins:
- serverless-wsgi
- serverless-python-requirements
custom:
stages:
- prod
# Python Flask Lmabda Config
wsgi:
app: lambda_backend/website_pf/src/app.app
pythonBin: python3
packRequirements: false
pythonRequirements:
usePoetry: true
requirePoetryLockFile: true
pythonBin: python3
layer:
name: website-pf-lambda-layer-${self:provider.stage}
description: Lambda Layer for Website-PF application.
compatibleRuntimes:
- python3.11
# Provider/Service Definition
provider:
name: aws
region: us-east-1
versionFunctions: false
endpointType: REGIONAL
stage: ${opt:stage, 'prod'}
apiGateway:
shouldStartNameWithService: true
apiKeys:
- name: website-pf-${self:provider.stage}-client-key-111112
description: Client key for website-pf api application.
usagePlan:
quota:
limit: 40001
offset: 0
period: DAY
throttle:
burstLimit: 10
rateLimit: 20
# Environment variables
environment:
LOG_LEVEL: INFO
ENVIRONMENT: ${self:provider.stage}
REGION: ${self:provider.region}
VERSION: 0.8.0
WEBSITE_URL: ${ssm:/${self:provider.stage}/${self:service}/acm/url}
S3_WEBSITE_PF_BUCKET: "website-pf-${self:provider.stage}"
DATABASE_URL: ${ssm:/${self:provider.stage}/${self:service}/rds/hostname}
DATABASE_SCHEMA: ${ssm:/${self:provider.stage}/${self:service}/rds/schema}
DATABASE_USERNAME: ${ssm:/${self:provider.stage}/${self:service}/rds/username}
DATABASE_PASSWORD: ${ssm:/${self:provider.stage}/${self:service}/rds/password}
# IAM Role for Lambda configuration
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:*"
Resource:
- "arn:aws:s3:::website-pf-${self:provider.stage}"
- "arn:aws:s3:::website-pf-${self:provider.stage}/*"
- Effect: "Allow"
Action:
- "lambda:InvokeFunction"
Resource:
- "*"
# Application packaging information
package:
individually: true
# Lambda backend configuration
functions:
website_pf:
runtime: python3.11
handler: wsgi_handler.handler
name: website-pf-${self:provider.stage}
description: Website-PF backend for querying and retrieving post information.
memorySize: 256
timeout: 10
layers:
- Ref: PythonRequirementsLambdaLayer
package:
patterns:
- '!./**'
- lambda_backend/website_pf/src/**
- ./wsgi_handler.py
- ./serverless_wsgi.py
- ./.serverless-wsgi
events: ${file(config/events.yml)}
vpc:
securityGroupIds:
- ${ssm:/${self:provider.stage}/${self:service}/vpc/sg/id}
subnetIds:
- ${ssm:/${self:provider.stage}/${self:service}/vpc/subnet/id}
environment:
website_pf_post_loader:
runtime: python3.11
handler: lambda_backend/website_pf_post_loader/src/app.lambda_handler
name: website-pf-post-loader-${self:provider.stage}
description: Website-PF loader for new and existing posts.
memorySize: 256
timeout: 30
layers:
- Ref: PythonRequirementsLambdaLayer
package:
patterns:
- '!./**'
- lambda_backend/website_pf_post_loader/src/**
vpc:
securityGroupIds:
- ${ssm:/${self:provider.stage}/${self:service}/vpc/sg/id}
subnetIds:
- ${ssm:/${self:provider.stage}/${self:service}/vpc/subnet/id}
environment:
FEATURED_POSTS: "about,portfolio"
# CloudFormation resources
resources:
Description: Website-PF CloudFormation Stack using serverless
Resources:
# S3 Bucket for webapp / posts
WebsitePFBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: website-pf-${self:provider.stage}
AccessControl: Private
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html
CorsConfiguration:
CorsRules:
- AllowedMethods:
- GET
AllowedOrigins:
- "*"
AllowedHeaders:
- "*"
# S3 Bucket for webapp / posts Policy
WebAppS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: WebsitePFBucket
PolicyDocument:
Statement:
# Cloudfront & Lambda Bucket Allow
- Sid: OAIReadGetObjects
Effect: Allow
Principal:
AWS:
- { "Fn::Join" : [" ", ["arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity", { Ref: WebAppCloudFrontOriginAccessIdentity } ] ] }
- !Sub "arn:aws:iam::${AWS::AccountId}:role/website-pf-${self:provider.stage}-${self:provider.region}-lambdaRole"
Action:
- s3:GetObject
Resource:
- "arn:aws:s3:::website-pf-${self:provider.stage}/*"
- Sid: WebsitePFBucketVPCBucketAccess
Effect: Allow
Principal: "*"
Action:
- s3:GetObject
Resource:
- "arn:aws:s3:::website-pf-${self:provider.stage}/*"
Condition:
StringEquals:
aws:sourceVpce: ${ssm:/${self:provider.stage}/${self:service}/vpc/id}
- Sid: OAIDenyGetObjects
Effect: Deny
Principal:
AWS:
- { "Fn::Join" : [" ", ["arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity", { Ref: WebAppCloudFrontOriginAccessIdentity } ] ] }
Action:
- s3:GetObject
Resource:
- "arn:aws:s3:::website-pf-${self:provider.stage}/upload/*"
# Cloudfront Origin Access Identity
WebAppCloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: ${self:service}-${self:provider.stage}-access-identity
# Cloudfront cache Policy
WebAppCloudFrontCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: ${self:service}-${self:provider.stage}-cache-policy
DefaultTTL: 86400
MaxTTL: 31536000
MinTTL: 1
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: "none"
HeadersConfig:
HeaderBehavior: "none"
QueryStringsConfig:
QueryStringBehavior: "none"
EnableAcceptEncodingBrotli: true
EnableAcceptEncodingGzip: true
# Cloudfront Origin Request Policy
WebAppCloudFrontOriginRequestPolicy:
Type: AWS::CloudFront::OriginRequestPolicy
Properties:
OriginRequestPolicyConfig:
Name: ${self:service}-${self:provider.stage}-origin-request-policy
CookiesConfig:
CookieBehavior: "none"
HeadersConfig:
HeaderBehavior: "whitelist"
Headers:
- "origin"
- "access-control-request-headers"
- "access-control-request-method"
QueryStringsConfig:
QueryStringBehavior: "none"
# Cloudfront distribution configuration
WebAppCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Comment: Website-PF serverless distribution for webapp in s3 bucket
PriceClass: PriceClass_100
WebACLId: ${ssm:/${self:provider.stage}/waf/cloudfront/arn}
Origins:
- DomainName: website-pf-${self:provider.stage}.s3.amazonaws.com
Id: s3-website-pf-${self:provider.stage}
S3OriginConfig:
OriginAccessIdentity: { "Fn::Join" : ["", ["origin-access-identity/cloudfront/", { Ref: WebAppCloudFrontOriginAccessIdentity } ] ] }
Enabled: 'true'
DefaultRootObject: site/${self:provider.environment.VERSION}/index.html
CustomErrorResponses:
- ErrorCode: 403
ResponseCode: 200
ResponsePagePath: /site/${self:provider.environment.VERSION}/index.html
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /site/${self:provider.environment.VERSION}/index.html
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
TargetOriginId: s3-website-pf-${self:provider.stage}
CachePolicyId: { Ref: WebAppCloudFrontCachePolicy }
OriginRequestPolicyId: { Ref: WebAppCloudFrontOriginRequestPolicy }
ViewerProtocolPolicy: redirect-to-https
ResponseHeadersPolicyId: 5cc3b908-e619-4b99-88e5-2cf7f45965bd
Aliases:
- "prestonfrazier.net"
- "www.prestonfrazier.net"
ViewerCertificate:
AcmCertificateArn: ${ssm:/${self:provider.stage}/${self:service}/acm/arn}
MinimumProtocolVersion: "TLSv1.2_2019"
SslSupportMethod: "sni-only"