-
Notifications
You must be signed in to change notification settings - Fork 8
/
serverless.yml
307 lines (294 loc) · 9.63 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
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
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!
service: serverless-ephemera-local
plugins:
- serverless-s3-sync
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
custom:
s3Sync:
- bucketName: ${self:custom.config.public_bucket_name}
localDir: frontend
acl: public-read
config: ${file(config.yml)}
provider:
name: aws
runtime: nodejs8.10
stage: ${self:custom.config.stage}
region: ${self:custom.config.region}
resourcePolicy:
- Effect: Allow
Principal: "*"
Action: execute-api:Invoke
Resource:
- execute-api:/${self:custom.config.stage}/*/addTextSecret
Condition:
IpAddress:
aws:SourceIp: ${self:custom.config.add_secret_whitelisted_ips}
- Effect: Allow
Principal: "*"
Action: execute-api:Invoke
Resource:
- execute-api:/${self:custom.config.stage}/*/getSecret
Condition:
IpAddress:
aws:SourceIp: ${self:custom.config.get_secret_whitelisted_ips}
environment:
REGION: !Ref AWS::Region
DYNAMODB_TABLE_NAME: ${self:custom.config.dynamodb_table_name}
KMS_KEY_ID: !Ref LambdaEncryptionKey
iamRoleStatements:
- Effect: "Allow"
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:Scan"
- "dynamodb:DeleteItem"
Resource:
- Fn::Join:
- ""
- - "arn:aws:dynamodb:"
- ${self:custom.config.region}
- ":"
- Ref: 'AWS::AccountId'
- ":table/"
- ${self:custom.config.dynamodb_table_name}
- Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:PutObjectAcl"
Resource:
- Fn::Join:
- ""
- - "arn:aws:s3:::"
- ${self:custom.config.public_bucket_name}
- '/*'
# you can define service wide environment variables here
# environment:
# variable1: value1
# you can add packaging information here
package:
include:
- lambda/**
exclude:
- "**"
functions:
EphemeraAddTextSecret:
handler: lambda/ephemera-addtextsecret/ephemera-addtextsecret.handler
events:
- http:
path: addTextSecret
method: post
cors: true
EphemeraGetSecret:
handler: lambda/ephemera-getsecret/ephemera-getsecret.handler
events:
- http:
path: getSecret
method: get
cors: true
EphemeraAgeOffSecret:
handler: lambda/ephemera-ageoffsecret/ephemera-ageoffsecret.handler
environment:
MAX_SECRET_AGE_HOURS: ${self:custom.config.max_secret_age_hours}
events:
- schedule: rate(2 hours)
EphemeraPopulateStaticSiteConfig:
handler: lambda/ephemera-populatestaticsiteconfig/ephemera-populatestaticsiteconfig.handler
environment:
BUCKET_NAME: ${self:custom.config.public_bucket_name}
API_URL: !Join
- ""
-
- https://
- !Ref ApiGatewayRestApi
- ".execute-api."
- !Ref AWS::Region
- ".amazonaws.com/"
- ${self:custom.config.stage}
events:
- s3:
bucket: PublicBucket
event: s3:ObjectRemoved:*
- s3:
bucket: PublicBucket
event: s3:ObjectCreated:*
rules:
- prefix: index.html
resources:
Conditions:
HttpUi: !Equals ["${self:custom.config.use_https_ui}", false]
HttpsUi: !Equals ["${self:custom.config.use_https_ui}", true]
Resources:
# Create CloudFront OAI when using HTTPS, so this can be added to the S3 Bucket Policy
EphemeraCloudfrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Condition: HttpsUi
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "access-identity-${self:custom.config.public_bucket_name}.s3.amazonaws.com"
S3BucketPublicBucket:
Type: AWS::S3::Bucket
Properties:
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
PublicAccessBlockConfiguration:
RestrictPublicBuckets: true
BucketName: ${self:custom.config.public_bucket_name}
S3BucketHttpPublicBucketPolicy:
Type: AWS::S3::BucketPolicy
Condition: HttpUi
Properties:
Bucket:
!Ref S3BucketPublicBucket
PolicyDocument:
Statement:
-
Action:
- "s3:GetObject"
Effect: "Allow"
Resource: "arn:aws:s3:::${self:custom.config.public_bucket_name}/*"
Principal: "*"
Condition:
StringLike:
aws:Referer:
- "http://${self:custom.config.public_bucket_name}/"
-
Action:
- "s3:*"
Effect: "Allow"
Resource: "arn:aws:s3:::${self:custom.config.public_bucket_name}/*"
Principal:
AWS:
- !GetAtt [IamRoleLambdaExecution, Arn]
# Create S3 Bucket Policy which permits access only from AWS Lambda and AWS CloudFront
S3BucketHttpsPublicBucketPolicy:
Type: AWS::S3::BucketPolicy
Condition: HttpsUi
Properties:
Bucket:
!Ref S3BucketPublicBucket
PolicyDocument:
Statement:
-
Action:
- "s3:GetObject"
Effect: "Allow"
Resource: "arn:aws:s3:::${self:custom.config.public_bucket_name}/*"
Principal:
AWS: !Join [" ", ["arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity", !Ref EphemeraCloudfrontOriginAccessIdentity]]
-
Action:
- "s3:*"
Effect: "Allow"
Resource: "arn:aws:s3:::${self:custom.config.public_bucket_name}/*"
Principal:
AWS:
- !GetAtt [IamRoleLambdaExecution, Arn]
# If using HTTPS UI, create a CloudFront distribution to front the S3 Bucket
EphemeraCloudfrontDistribution:
Type: AWS::CloudFront::Distribution
Condition: HttpsUi
Properties:
DistributionConfig:
Origins:
- DomainName: "${self:custom.config.public_bucket_name}.s3.amazonaws.com"
Id: "S3-${self:custom.config.public_bucket_name}"
S3OriginConfig:
OriginAccessIdentity: !Join ["/", ["origin-access-identity/cloudfront", !Ref EphemeraCloudfrontOriginAccessIdentity]]
Enabled: true
DefaultRootObject: index.html
Aliases:
- ${self:custom.config.public_bucket_name}
DefaultCacheBehavior:
TargetOriginId: "S3-${self:custom.config.public_bucket_name}"
ForwardedValues:
QueryString: false
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
ViewerCertificate:
AcmCertificateArn: !Join ["", ["arn:aws:acm:us-east-1:", !Ref AWS::AccountId, ":certificate/", "${self:custom.config.acm_certificate_id}"]]
SslSupportMethod: sni-only
EphemeraPopulateStaticSiteConfigPermissionPublicBucketS3:
Type: "AWS::Lambda::Permission"
Properties:
FunctionName:
"Fn::GetAtt":
- EphemeraPopulateStaticSiteConfigLambdaFunction
- Arn
Principal: "s3.amazonaws.com"
Action: "lambda:InvokeFunction"
SourceAccount:
Ref: AWS::AccountId
SourceArn: "arn:aws:s3:::${self:custom.config.public_bucket_name}"
dynamoDBTable:
Type: "AWS::DynamoDB::Table"
Properties:
SSESpecification:
SSEEnabled: True
AttributeDefinitions:
-
AttributeName: "SecretID"
AttributeType: "S"
KeySchema:
-
AttributeName: "SecretID"
KeyType: "HASH"
TableName: ${self:custom.config.dynamodb_table_name}
BillingMode: PAY_PER_REQUEST
LambdaEncryptionKey:
Type: AWS::KMS::Key
Properties:
Description: "Ephemera ${self:custom.config.stage} key"
KeyPolicy:
Version: "2012-10-17"
Id: "key-default"
Statement:
-
Sid: "Allow use of the key"
Effect: "Allow"
Principal:
AWS: !GetAtt [IamRoleLambdaExecution, Arn]
Action:
- "kms:Encrypt"
- "kms:Decrypt"
- "kms:ReEncrypt*"
- "kms:GenerateDataKey*"
Resource: "*"
-
Sid: "Allow administration of the key"
Effect: "Allow"
Principal:
AWS: '*'
Action:
- "kms:Create*"
- "kms:Describe*"
- "kms:Enable*"
- "kms:List*"
- "kms:Put*"
- "kms:Update*"
- "kms:Revoke*"
- "kms:Disable*"
- "kms:Get*"
- "kms:Delete*"
- "kms:ScheduleKeyDeletion"
- "kms:CancelKeyDeletion"
Resource: "*"
Outputs:
WebsiteURL:
Value:
Fn::GetAtt: S3BucketPublicBucket.WebsiteURL
Description: URL for the website hosted on S3