The CloudFormation template

I will use a bucket named quizzercloud (or one of its variations) for all of the CloudFormation templates within this chapter:

  1. Start the template with the template version and a description (optional).
  2. Define a parameter for the bucket name:
Parameters:
BucketName:
Description: Bucket name for your website
Type: String
  1. Define a resource for the bucket:
Resources:
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
AccessControl: PublicRead
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
  1. Define a bucket access policy that allows for everyone to access the bucket's contents:
WebsitePublicAccessPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref MyBucket
PolicyDocument:
Statement:
-
Action:
- "s3:GetObject"
Effect: "Allow"
Resource:
Fn::Join:
- ""
-
- "arn:aws:s3:::"
- !Ref MyBucket
- "/*"
Principal: "*"
  1. Add an Outputs section to return the URL of the S3 website (optional):
Outputs:
S3WebsiteURL:
Value: !Sub
- http://${Bucket}.s3-website.${AWS::Region}.amazonaws.com
- Bucket: !Ref MyBucket
Description: URL for S3 static website
  1. Execute the CloudFormation template by passing the values for the parameters:
aws cloudformation create-stack 
--stack-name s3websitestack
--template-body file://resources/s3-static-website-cf-template.yml
--parameters ParameterKey=BucketName,ParameterValue=quizzercloud
--region ap-south-1
--profile admin
  1. Check the creation status by using the aws cloudformation describe-stacks command. If it is successful, you should get a response with an Outputs section, as follows:

  1. Once the stack creation has completed, you will need to upload the index.html and error.html files into the root bucket. Refer to the AWS CLI commands section or the code files for the command.
  2. Finally, execute the S3 static website URL in a browser, as shown in the following screenshot:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.176.166