Skip to content

Instantly share code, notes, and snippets.

@jtomchak
Created August 16, 2022 20:56
Show Gist options
  • Save jtomchak/2342169a0d21f29f161fc83f693a9897 to your computer and use it in GitHub Desktop.
Save jtomchak/2342169a0d21f29f161fc83f693a9897 to your computer and use it in GitHub Desktop.
service: sls-react-cloudfront
provider:
name: aws
runtime: nodejs16.x
region: us-east-1
plugins:
- serverless-s3-sync
custom:
bucketName: sls-react-aug-2022
s3Sync:
- bucketName: ${self:custom.bucketName}
localDir: my-app/build/
resources:
Resources:
ReactAppBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.bucketName}
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: index.html
S3AccessPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: ReactAppBucket
PolicyDocument:
Statement:
- Sid: PublicReadGetObject
Effect: Allow
Principal: "*"
Action:
- s3:GetObject
Resource: arn:aws:s3:::${self:custom.bucketName}/*
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: ${self:custom.bucketName}.s3.amazonaws.com
Id: ReactApp
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
Enabled: "true"
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
DefaultCacheBehavior:
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
TargetOriginId: ReactApp
ForwardedValues:
QueryString: "false"
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
ViewerCertificate:
CloudFrontDefaultCertificate: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment