AWS CLI commands

I will use a bucket name (qnatime, or a variation thereof) for the AWS CLI commands within this chapter.

S3 bucket names are unique across all regions. Please use a different domain name than the one I use in the recipes.
  1. Create an S3 bucket, as follows:
aws s3api create-bucket 
--bucket qnatime
--create-bucket-configuration LocationConstraint=ap-south-1
--region ap-south-1
--profile admin

We need to specify the LocationConstraint explicitly, in addition to the --region option for non-US regions. This command will provide you with the location of the S3 bucket:

From this location and the knowledge of the region, you can derive the URL for our static websitehttp://qnatime.s3-website.ap-south-1.amazonaws.com. However, the website link will not work now, as we have not configured the bucket as a website.

We are also using the aws cli s3api command, instead of the aws cli s3 command that we were using hitherto. Some of the actions that we will perform will require more control, as provided by the s3api sub-command over the high-level s3 sub-command. 

  1. Create an index document and an error document.

An S3 static website requires you to provide two HTML files: an index document and an error document. The index document is the website landing page, and the error document is displayed in the case of an error. 

Create a simple index.html file, with only an <h1> tag inside the body:

<body>
<h1> Welcome to Q & A Time! </h1>
</body>

Similarly, you can also create a simple error.html file, with a different text within the <h1> tag: 

<body>
<h1>
Error page for Q & A Time! </h1>
<body>

Refer to the code files for the complete index.html and error.html files.

  1. Upload the index and error documents, as follows:
aws s3 cp resources/index.html s3://qnatime/index.html 
--profile admin
aws s3 cp resources/error.html s3://qnatime/error.html
--profile admin
  1. Create a website configuration JSON file specifying the index and error filenames, as follows:
{
"IndexDocument": {
"Suffix": "index.html"
},
"ErrorDocument": {
"Key": "error.html"
}
}
  1. Create a static website specifying the website configuration JSON file, as follows:
aws s3api put-bucket-website 
--bucket qnatime.com
--website-configuration file://resources/s3-website-configuration.json
--profile admin
  1. Create a bucket policy with read permission for everyone.

By default, an S3 bucket and its objects do not provide read access to the public. However, for an S3 bucket to act as a website, all of the files need to be made accessible to the public. This can be done by using the following bucket policy:

{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"PublicReadGetObjectAccess",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::qnatime/*"]
}
]
}

Execute the bucket policy, as follows:

aws s3api put-bucket-policy 
--bucket qnatime
--policy file://resources/s3-website-policy.json
--profile admin
  1. Execute the bucket website URL; the result will look like 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.212.212