Skip to content

Instantly share code, notes, and snippets.

@j-un
Created November 15, 2018 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-un/f3bc69f652ca546d28139215359b27c5 to your computer and use it in GitHub Desktop.
Save j-un/f3bc69f652ca546d28139215359b27c5 to your computer and use it in GitHub Desktop.
CloudFormation template snippet - CloudFront Distribution with a S3 Static Website Hosting
AWSTemplateFormatVersion: 2010-09-09
Description: CloudFront Distribution with a S3 Static Website Hosting
Parameters:
CustomDomain:
Type: String
Description: Domain name of your website.
CertificateARN:
Type: String
Description: SSL Certificate ARN. SSL Certificate must be in us-east-1 region.
Resources:
ContentsBucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
Properties:
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: index.html
ContentsBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
Bucket: !Ref ContentsBucket
PolicyDocument:
Statement:
- Effect: Allow
Principal: '*'
Action:
- 's3:GetObject'
Resource: !Sub 'arn:aws:s3:::${ContentsBucket}/*'
Condition:
StringEquals:
'aws:UserAgent': Amazon CloudFront
ContentsDistribution:
Type: 'AWS::CloudFront::Distribution'
Properties:
DistributionConfig:
Aliases:
- !Ref CustomDomain
Origins:
- DomainName: !Sub '${ContentsBucket}.s3-website-${AWS::Region}.amazonaws.com'
Id: !Sub '${ContentsBucket}'
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: http-only
Enabled: true
DefaultRootObject: index.html
Comment: !Sub '${AWS::StackName} distribution'
DefaultCacheBehavior:
TargetOriginId: !Sub '${ContentsBucket}'
ForwardedValues:
QueryString: false
ViewerProtocolPolicy: redirect-to-https
ViewerCertificate:
SslSupportMethod: sni-only
MinimumProtocolVersion: TLSv1
AcmCertificateArn: !Ref CertificateARN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment