-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
132 lines (122 loc) · 3.85 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
service: s3-hosting
custom:
domain: ${param:domain, 'example.com'}
provider:
name: aws
runtime: nodejs20.x
region: ${opt:region, 'us-east-1'}
stage: ${opt:stage, 'live'}
deploymentBucket:
name: ${aws:accountId}-serverless-deploys
blockPublicAccess: true
resources:
Resources:
S3HostingBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.domain}
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
PublicAccessBlockConfiguration:
BlockPublicAcls: false
OwnershipControls:
Rules:
- ObjectOwnership: ObjectWriter
S3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: S3HostingBucket
PolicyDocument:
Statement:
- Effect: Allow
Principal: "*"
Action:
- "s3:GetObject"
Resource:
Fn::Join: [
"", [
"arn:aws:s3:::",
{
"Ref": "S3HostingBucket"
},
"/*"
]
]
Certificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: ${self:custom.domain}
DomainValidationOptions:
- DomainName: ${self:custom.domain}
ValidationDomain: ${self:custom.domain}
ValidationMethod: DNS
CDN:
Type: AWS::CloudFront::Distribution
DependsOn:
- S3HostingBucket
- Certificate
Properties:
DistributionConfig:
Comment: ${self:custom.domain} website
Origins:
- DomainName: !GetAtt S3HostingBucket.DomainName
Id: !Join ['', [!Select [0, !Split ['.', '${self:custom.domain}']], '-origin']]
S3OriginConfig:
OriginAccessIdentity: ""
DefaultCacheBehavior:
TargetOriginId: !Join ['', [!Select [0, !Split ['.', '${self:custom.domain}']], '-origin']]
ViewerProtocolPolicy: redirect-to-https
AllowedMethods:
- GET
- HEAD
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6
Compress: true
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt CDNFunction.FunctionMetadata.FunctionARN
Enabled: true
DefaultRootObject: index.html
HttpVersion: http2
Aliases:
- ${self:custom.domain}
ViewerCertificate:
AcmCertificateArn: !Ref Certificate
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1.2_2021
CDNFunction:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
FunctionCode: |
function handler(event) {
var request = event.request;
var uri = request.uri;
// Check whether the URI is missing a file name.
if (uri.endsWith('/')) {
request.uri += 'index.html';
}
// Check whether the URI is missing a file extension.
else if (!uri.includes('.')) {
request.uri += '/index.html';
}
return request;
}
FunctionConfig:
Comment: Redirect-Default-Index-Request
Runtime: cloudfront-js-1.0
Name: !Join ['', [!Select [0, !Split ['.', '${self:custom.domain}']], '-index-redirect']]
Route53Record:
Type: AWS::Route53::RecordSet
DependsOn:
- CDN
Properties:
HostedZoneName: ${self:custom.domain}.
Name: ${self:custom.domain}.
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2
DNSName: !GetAtt
- CDN
- DomainName