Introduction
In previous post on this topic, we learned some basics about AWS CLI and how to install it on your machine. In this post, we will learn few AWS CLI commands and we will also setup an s3 bucket as a static website which is publicly accessible.
AWS CLI Syntax
Following picture shows AWS CLI commands Syntax:
Example: LIST EC2 AMI Images
Let’s try command to “show amazon EC2 images, that run CentOS”
aws --output table ec2 describe-images `
--filter "Name=description, Values=*cent*" `
"Name=owner-alias, Values=amazon"
Here is the command syntax break-down:
The execution of the command generates following output:
You’ll often run describe-based sub commands to get important resource-IDs, that you can then plug-in to your actual action commands
Top-level commands normally execute AWS services, Sub-commands execute some actions on a service resources, options controls the command environment and parameters narrow down the scope of your command.
Beside describe and run, other sub-commands begin with verbs like:
- create
- delete
- disable
- modify
- request
- stop
- terminate
options example:
- — region
- — output
- — profile
- — dry-run
Example: Launch an EC2 Instance
The following command shows, how to launch an ec2 instance. If we use –dry-run flag, it will just execute the command, verify syntax but will not create the instance.
aws ec2 run-instances --dry-run `
--image-id ami-05f7491af5eef733a `
--count 1 `
--key-name euKey `
--security-group-ids sg-0b5ee4097807... `
--subnet-id subnet-0126882843... `
--instance-type t2.micro
Here is the output of executing the command without using –dry-run flag:
and if we visit the AWS Web console, we can see that EC2 instance is created:
Example: Terminate EC2 Instance
Here is the command to terminate an EC2 instance using CLI:
aws ec2 terminate-instances --instance-ids i-0858df785c04b25a4
here is the output of executing the command:
AWS CLI Help
You can get some helpful information by using aws help. Few examples are shown below:
aws help
aws s3 help
aws s3 website help
you can press Q key to exit from the help window.
Demo – S3 Bucket
Let’s try some AWS CLI commands to create and manage S3 Bucket:
Create a bucket:
aws s3 mb s3://maptestdemo
This command will create a new S3 bucket (note that bucket name should be globally unique)
Copy files to S3 Bucket:
aws s3 cp .\ s3://maptestdemo --recursive
The above shown command will copy all files from current directory to the s3 bucket.
Here is the output of the operation:
List content of S3 Bucket
aws s3 ls maptestdemo
This command will list content of s3 bucket as shown below:
Demo – Setup an S3 bucket as public website
I have a simple website folder on my local machine which contains some files as shown below:
We want to copy these files to an S3 bucket and then configure this bucket to be served as a static website.
You can use your own website files if you like and it will work the same way for those as well.
Here are the steps we will perform to setup an S3 bucket as a public website. Some of these steps we’ve already done in previous exercise.
Create a bucket
aws s3 mb s3://maptestdemo.com
Set Public read access for bucket
Next, we will set public read access to bucket. Notice that we used s3api for this purpose here.
aws s3api put-bucket-acl --bucket maptestdemo.com --acl public-read
Copy files from local directory to s3 bucket
aws s3 sync . s3://maptestdemo.com --acl public-read
Here dot (.) used for current local directory. –acl public-read flag gives copied files permission as the bucket.
Setup Index and Error documents
aws s3 website s3://maptestdemo.com/ --index-document index.html --error-document error.html
Here we are targeting index and error documents.
Verify if Website is setup properly
aws s3api get-bucket-website --bucket maptestdemo.com
Here is the output of all above shown operations in regard to static website:
Next, how do we know, what is the website URL? well, one option is that we can look it up into AWS web console or we can also know this by understanding how AWS forms it.
It typically follows the following pattern:
[bucketname].[s3-website]-[defaultregion].[amazonaws.com]
maptestdemo.com.s3-website-eu-central-1.amazonaws.com
Summary
In this post we saw few more AWS CLI Commands. We use these commands to create EC2 instance and we also learn how can we get some help about aws commands. We then create an S3 bucket, copy files from local directory to it and then configure this s3 bucket as a static website, which is publicly accessible. Let me know if you have some questions or comments. Till next time, happy coding.
My Recent Books
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.
1 thought on “AWS CLI Basics – Part 2”
Comments are closed.