Unlike with the CLI commands, there is no shorthand way to create a CloudFront distribution with a CloudFormation template without specifying the distribution config parameters. I will, however, only add the essential parameters in this recipe:
- Start the template with the template version and a description (optional).
- Create a resource of the type AWS::CloudFront::Distribution:
Resources:
MyCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: quizzer.cloud.s3.amazonaws.com
Id: myS3Origin
S3OriginConfig:
OriginAccessIdentity: ''
Enabled: 'true'
Comment: 'CloudFront Distribution for S3 Bucket'
DefaultRootObject: index.html
DefaultCacheBehavior:
TargetOriginId: myS3Origin
ForwardedValues:
QueryString: 'false'
Cookies:
Forward: none
ViewerProtocolPolicy: allow-all
- Add an Outputs section to return the CloudFront distribution ID and the CloudFront domain name:
Outputs:
CloudFrontDistributionId:
Value: !Ref MyCloudFrontDistribution
Description: 'CloudFront distribution id'
CloudFrontDomain:
Value: !GetAtt MyCloudFrontDistribution.DomainName
Description: 'CloudFront distribution domain name'
- Execute the stack, using the create-stack command.
It will take some time for the distribution to be created. You can check the status by using the describe-stacks command. Once it has completed, you will get a response with the Outputs section, as follows:
- Execute the CloudFront domain name in a browser, and verify whether the S3 static website has loaded: