-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverless-resources.yml
84 lines (80 loc) · 2.88 KB
/
serverless-resources.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
Resources:
WebAppS3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.statics.bucket}
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: index.html
## Specifying the policies to make sure all files inside the Bucket are available to CloudFront
WebAppS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: WebAppS3Bucket
PolicyDocument:
Statement:
- Sid: PublicReadGetObject
Effect: Allow
Principal: "*"
Action:
- s3:GetObject
Resource: arn:aws:s3:::${self:custom.statics.bucket}/*
## Specifying the CloudFront Distribution to serve the Web App (static assets) and Backend (lambda function)
WebAppCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: 'true'
DefaultRootObject: index.html
Origins:
- Id: WebApp
DomainName: ${self:custom.statics.bucket}.s3.amazonaws.com
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
- Id: Backend
DomainName:
'Fn::Join': [ "", [ { Ref: 'ApiGatewayRestApi' }, '.execute-api.', { Ref: 'AWS::Region' }, '.amazonaws.com' ] ]
OriginPath: /${self:custom.stage}
CustomOriginConfig:
OriginProtocolPolicy: https-only
DefaultCacheBehavior:
AllowedMethods: [ HEAD, GET, OPTIONS ]
TargetOriginId: WebApp
ForwardedValues:
QueryString: false
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
CacheBehaviors:
- AllowedMethods: [ HEAD, DELETE, POST, GET, OPTIONS, PUT, PATCH ]
CachedMethods: [ HEAD, GET, OPTIONS ]
DefaultTTL: 0
Compress: true
ForwardedValues:
Headers: [ 'Accept', 'Referer', 'Authorization', 'Content-Type' ]
QueryString: true
PathPattern: '/backend/*'
TargetOriginId: Backend
ViewerProtocolPolicy: https-only
Aliases:
- ${self:custom.cf.customDomain}
ViewerCertificate:
AcmCertificateArn: ${self:custom.cf.certificateArn}
SslSupportMethod: sni-only
GatewayResponse:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.WWW-Authenticate: "'Basic'"
ResponseType: UNAUTHORIZED
RestApiId:
Ref: 'ApiGatewayRestApi'
StatusCode: '401'
## In order to print out the hosted domain via `serverless info` we need to define the DomainName output for CloudFormation
Outputs:
WebAppCloudFrontDistributionOutput:
Value:
'Fn::GetAtt': [ WebAppCloudFrontDistribution, DomainName ]