-
Notifications
You must be signed in to change notification settings - Fork 3
/
serverless.yml
199 lines (182 loc) · 6.12 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
service: ${self:custom.serviceName}
custom:
setupFile: ./setup.${self:provider.stage}.json
serviceName: ${file(${self:custom.setupFile}):serviceName}
rulesBucket: ${self:custom.serviceName}-${file(${self:custom.setupFile}):rulesBucket}
rulesFile: ${file(${self:custom.setupFile}):rulesFile}
regexpFile: ${file(${self:custom.setupFile}):regexpFile}
regexpFileTTL: ${file(${self:custom.setupFile}):regexpFileTTL}
minTTL: ${file(${self:custom.setupFile}):cloudfront-minTTL}
maxTTL: ${file(${self:custom.setupFile}):cloudfront-maxTTL}
defaultTTL: ${file(${self:custom.setupFile}):cloudfront-defaultTTL}
apiKey: ${file(${self:custom.setupFile}):api-key}
fakeOrigin: ${file(${self:custom.setupFile}):fakeOrigin}
distributionId: ${file(${self:custom.setupFile}):distributionId}
#R53HostedZone: ${file(${self:custom.setupFile}):R53HostedZone}
s3Sync:
- bucketName: ${self:custom.rulesBucket}
localDir: ${file(${self:custom.setupFile}):rulesBucket}
deleteRemoved: true
#cfdDomain:
# domainNames:
# - "*.labs.xxxx.es"
# cloudFront: CloudFrontDistribution
# retries: 15
# minimumProtocolVersion: TLSv1.2_2018
plugins:
- '@silvermine/serverless-plugin-cloudfront-lambda-edge'
- serverless-s3-sync
# - serverless-cloudfront-distribution-certificate
package:
patterns:
- '!node_modules/**'
provider:
name: aws
runtime: nodejs18.x
stage: ${opt:stage, 'demo'}
region: ${env:region, 'us-east-1'}
memorySize: 128
timeout: 5
stackTags:
name: ${self:service}
iam:
role:
statements:
- Effect: 'Allow'
Action:
- 's3:*'
Resource: "*"
- Effect: 'Allow'
Action: sts:AssumeRole
Resource: "*"
- Effect: 'Allow'
Action: cloudfront:CreateInvalidation
Resource: "*"
functions:
invalidate:
handler: functions/cf-invalidate/index.handler
lambdaAtEdge:
distribution: 'CloudFrontDistribution'
eventType: 'origin-request'
pathPattern: '/invalidate*'
http-redirect:
handler: functions/query/index.handler
lambdaAtEdge:
distribution: 'CloudFrontDistribution'
eventType: 'origin-request'
resources:
Resources:
RulesData:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.rulesBucket}
IamRoleLambdaExecution:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
# RouteDNSName:
# Type: AWS::Route53::RecordSetGroup
# Properties:
# HostedZoneId: ${self:custom.R53HostedZone}
# RecordSets:
# - Name: 'drivemonitor.itlabs.uoc.es'
# Type: CNAME
# AliasTarget:
# HostedZoneId: ${self:custom.R53HostedZone}
# DNSName: !GetAtt [CloudFrontDistribution, DomainName]
# EvaluateTargetHealth: false
# - Name: 'redirect.itlabs.uoc.es'
# Type: CNAME
# AliasTarget:
# HostedZoneId: ${self:custom.R53HostedZone}
# DNSName: !GetAtt [CloudFrontDistribution, DomainName]
# EvaluateTargetHealth: false
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
#Aliases:
# - 'redirect.labs.xxxx.es'
# - 'domain.labs.xxxx.es'
Comment: ${self:custom.serviceName}
CacheBehaviors:
- TargetOriginId: ${self:custom.serviceName}
ViewerProtocolPolicy: 'redirect-to-https'
DefaultTTL: 0
MaxTTL: 0
MinTTL: 0
ForwardedValues:
QueryString: false
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- POST
- PATCH
- DELETE
PathPattern: '/sign*'
- TargetOriginId: ${self:custom.serviceName}
ViewerProtocolPolicy: 'redirect-to-https'
DefaultTTL: 0
MaxTTL: 0
MinTTL: 0
ForwardedValues:
QueryString: false
Headers:
- x-api-key
- invalidatePath
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- POST
- PATCH
- DELETE
PathPattern: '/invalidate*'
DefaultCacheBehavior:
TargetOriginId: ${self:custom.serviceName}
ViewerProtocolPolicy: 'redirect-to-https'
DefaultTTL: ${self:custom.defaultTTL}
MaxTTL: ${self:custom.maxTTL}
MinTTL: ${self:custom.minTTL}
ForwardedValues:
QueryString: false
Headers:
- Host
AllowedMethods:
- GET
- HEAD
- OPTIONS
- PUT
- POST
- PATCH
- DELETE
Enabled: true
Origins:
- Id: ${self:custom.serviceName}
DomainName: ${self:custom.fakeOrigin}
CustomOriginConfig:
OriginProtocolPolicy: 'https-only'
OriginCustomHeaders:
- HeaderName: 'bucket'
HeaderValue: ${self:custom.rulesBucket}
- HeaderName: 'rules'
HeaderValue: ${self:custom.rulesFile}
- HeaderName: 'regexp'
HeaderValue: ${self:custom.regexpFile}
- HeaderName: 'role'
HeaderValue: { "Fn::GetAtt" : [ "IamRoleLambdaExecution", "Arn" ] }
- HeaderName: 'api-key'
HeaderValue: ${self:custom.apiKey}
- HeaderName: 'cloudfrontid'
HeaderValue: ${self:custom.distributionId}
- HeaderName: 'regexpTTL'
HeaderValue: ${self:custom.regexpFileTTL}