-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserverless.yml
More file actions
224 lines (209 loc) · 7.01 KB
/
serverless.yml
File metadata and controls
224 lines (209 loc) · 7.01 KB
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
service: static-app
frameworkVersion: '2'
provider: # Basic configuration with IAM Role for Lambda
name: aws
runtime: nodejs12.x
stage: dev
lambdaHashingVersion: '20201221'
iam: # For Lambda to write to DynamoDB
role:
statements:
- Effect: "Allow"
Action:
- dynamodb:PutItem
Resource:
Fn::GetAtt:
- ContactTable
- Arn
- Effect: "Allow"
Action:
- sns:Publish
Resource: !Ref ContactReceivedTopic
environment: # For Lambda to get the DynamoDB table
TABLE_NAME: ${self:service}-${self:provider.stage}-${self:custom.table-name}
TOPIC_NAME: !Ref ContactReceivedTopic
custom: # Sevice parameters
bucket-name: ${file(./config.${opt:stage, 'dev'}.json):BUCKET_NAME} # S3 Bucket Name
table-name: ${file(./config.${opt:stage, 'dev'}.json):TABLE_NAME} # DynamoDB Table Name
contact-received-topic: ${file(./config.${opt:stage, 'dev'}.json):CONTACT_RECEIVED_TOPIC} # SNS Topic Name
forward-email: ${file(./config.${opt:stage, 'dev'}.json):FORWARD_EMAIL} # SNS Topic Email Subscription
acm-cert-arn: ${file(./config.${opt:stage, 'dev'}.json):ACM_CERT_ARN} # ACM Certificate ID
hosted-zone-id: ${file(./config.${opt:stage, 'dev'}.json):HOSTED_ZONE_ID} # If you want to register with Route53
package: # Remove files that are not needed in Lambda package
individually: true
excludeDevDependencies: false
exclude:
- app/**
- .gitignore
- '*.md'
- node_modules/**
- assets/**
functions: # Lambda functions
contact:
handler: functions/contact.handler
events:
- http:
path: contact
method: POST
cors: true
resources: # AWS Resources
Resources:
S3SiteBucket: # Host website content in this bucket
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.bucket-name}
WebsiteConfiguration:
IndexDocument: index.html
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- '*'
AllowedMethods:
- GET
AllowedOrigins:
- '*'
ExposedHeaders:
- Date
Id: myCORSRuleId1
MaxAge: 3600
ContactTable: # For holding the contact form detail
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub ${self:service}-${self:provider.stage}-${self:custom.table-name}
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: email
AttributeType: S
KeySchema:
- AttributeName: email
KeyType: HASH
OriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Sub 'OriginAccessIdentity for ${S3SiteBucket}'
BucketPolicy: # Allow site access from CloudFront only
DependsOn: OriginAccessIdentity
Type: AWS::S3::BucketPolicy
Properties:
PolicyDocument:
Id: OriginBucketPolicy
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
CanonicalUser: !GetAtt OriginAccessIdentity.S3CanonicalUserId
Action: 's3:GetObject'
Resource: !Sub 'arn:aws:s3:::${S3SiteBucket}/*'
Bucket: !Ref S3SiteBucket
CloudFrontDistro: # To serve the website via CDN
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: ${self:custom.bucket-name}.s3.amazonaws.com
Id: myS3Origin
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${OriginAccessIdentity}'
- DomainName: !Sub '${ApiGatewayRestApi}.execute-api.${AWS::Region}.amazonaws.com'
Id: ApiGatewayOrigin
CustomOriginConfig:
OriginProtocolPolicy: https-only
Enabled: 'true'
Comment: 'My Static Website Distro'
DefaultRootObject: index.html
Aliases:
- ${self:custom.bucket-name}
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- DELETE
- OPTIONS
- PATCH
- POST
- PUT
CachedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
DefaultTTL: 300
ForwardedValues:
Headers:
- Accept
- Referer
- Authorization
- Content-Type
QueryString: true
MaxTTL: 300
TargetOriginId: myS3Origin
ViewerProtocolPolicy: https-only
CacheBehaviors:
- TargetOriginId: ApiGatewayOrigin
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
PathPattern: /dev/*
ViewerProtocolPolicy: https-only
ForwardedValues:
QueryString: false
- TargetOriginId: myS3Origin
PathPattern: '*'
ViewerProtocolPolicy: redirect-to-https
ForwardedValues:
QueryString: false
PriceClass: 'PriceClass_100'
ViewerCertificate:
AcmCertificateArn: ${self:custom.acm-cert-arn}
MinimumProtocolVersion: TLSv1.2_2018
SslSupportMethod: sni-only
ContactReceivedTopic: # drop message when a new contact message is received
Type: AWS::SNS::Topic
Properties:
DisplayName: 'Contact received email'
TopicName: ${self:service}-${self:provider.stage}-${self:custom.contact-received-topic}
Subscription:
- Endpoint: ${self:custom.forward-email}
Protocol: email
WebsiteDNSName: # Registger Domain with Route53
DependsOn: CloudFrontDistro
Type: 'AWS::Route53::RecordSetGroup'
Properties:
HostedZoneId: ${self:custom.hosted-zone-id}
RecordSets:
- Name: ${self:custom.bucket-name}
Type: A
AliasTarget:
HostedZoneId: 'Z2FDTNDATAQYW2'
DNSName: !GetAtt
- CloudFrontDistro
- DomainName
Outputs:
WebsiteURL:
Value: !GetAtt
- S3SiteBucket
- WebsiteURL
Description: URL for website hosted on S3
S3BucketSecureURL:
Value: !Join
- ''
- - 'https://'
- !GetAtt
- S3SiteBucket
- DomainName
Description: Name of S3 bucket to hold website content
CloudFrontDistro:
Value: !Join
- ''
- - 'https://'
- !GetAtt
- CloudFrontDistro
- DomainName
ContactsTable:
Value: !Ref 'ContactTable'