-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserverless.yml
164 lines (153 loc) · 4.57 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
service: missionlabs-url-shortener
frameworkVersion: "2"
plugins:
- serverless-iam-roles-per-function
- serverless-plugin-typescript
provider:
name: aws
runtime: nodejs14.x
profile: ${opt:profile, default}
region: ${opt:region, eu-west-2}
stage: dev
environment:
LINK_TABLE: !Ref LinkTable
apiGateway:
apiKeys:
- key-${self:custom.stage}
custom:
stage: ${opt:stage, self:provider.stage}
r53HostedZone: ${opt:r53hosedzoneid}
domainName: ${opt:domainname}
acmCertArn: ${opt:acmcertarn}
createDNSRecord: ${opt:creatednsrecord}
functions:
shorten-url:
handler: src/handlers/shorten-url/index.handler
events:
- http:
path: api/shorten
method: POST
private: true
environment:
OWNER_DOMAIN: ${self:custom.domainName}
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:PutItem
Resource:
- !GetAtt LinkTable.Arn
get-url:
handler: src/handlers/get-url/index.handler
events:
- http:
path: /{id}
method: GET
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:GetItem
Resource:
- !GetAtt LinkTable.Arn
resources:
Conditions:
CreateRoute53Record: !Equals ["${self:custom.createDNSRecord}", "true"]
Resources:
LinkTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: missionlabs-url-shortener-links-${self:custom.stage}
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
AttributeDefinitions:
- AttributeName: id
AttributeType: S
TimeToLiveSpecification:
AttributeName: expire
Enabled: true
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
ViewerCertificate:
AcmCertificateArn: ${self:custom.acmCertArn}
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2019
Origins:
- DomainName:
Fn::Join:
- "."
- - Ref: ApiGatewayRestApi
- execute-api
- Ref: AWS::Region
- amazonaws.com
OriginPath: /${self:custom.stage}
Id: ${self:service}-${self:provider.region}-api-gw
CustomOriginConfig:
HTTPSPort: 443
OriginProtocolPolicy: https-only
HttpVersion: "http2"
Aliases:
- ${self:custom.domainName}
DefaultCacheBehavior:
CachePolicyId: !Ref CachePolicy
OriginRequestPolicyId: !Ref OriginRequestPolicy
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
TargetOriginId: ${self:service}-${self:provider.region}-api-gw
ViewerProtocolPolicy: redirect-to-https
OriginRequestPolicy:
Type: AWS::CloudFront::OriginRequestPolicy
Properties:
OriginRequestPolicyConfig:
Name: !Sub "OriginRequestPolicy-${AWS::StackName}"
CookiesConfig:
CookieBehavior: none
HeadersConfig:
HeaderBehavior: whitelist
Headers:
- x-api-key
QueryStringsConfig:
QueryStringBehavior: none
CachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Comment: CachePolicy
Name: !Sub "CachePolicy-${AWS::StackName}"
DefaultTTL: 86400
MaxTTL: 31536000
MinTTL: 1
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingGzip: true
HeadersConfig:
HeaderBehavior: whitelist
Headers:
- x-api-key
QueryStringsConfig:
QueryStringBehavior: none
DNSRecord:
Type: AWS::Route53::RecordSetGroup
Condition: CreateRoute53Record
Properties:
HostedZoneId: ${self:custom.r53HostedZone}
RecordSets:
- Name: ${self:custom.domainName}
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2 #Cloudfront Zone ID always same
DNSName: !GetAtt CloudFrontDistribution.DomainName
Outputs:
CloudFrontDomain:
Description: The generated Cloudfront Domain
Value: !GetAtt CloudFrontDistribution.DomainName