DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Terraform Best Practices: The 24 Practices You Should Adopt
  • Revolutionizing the Command Line: An Introduction to Gemini CLI
  • Rapid AWS Prototyping With LocalStack: A Developer’s Guide to Building AWS PoCs Locally
  • Establishing a Highly Available Kubernetes Cluster on AWS With Kops

Trending

  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team
  • Content Lakes: Harness Unstructured Data for Enterprise AI Readiness
  • Evaluating SOC Effectiveness Using Detection Coverage and Response Metrics
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. AWS CLI Basics – Part 2

AWS CLI Basics – Part 2

In this post, we will learn a few AWS CLI commands and we will also set up an s3 bucket as a static website that is publicly accessible.

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Nov. 12, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.0K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In a 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 a few AWS CLI commands and we will also set up an s3 bucket as a static website that is publicly accessible.

AWS CLI Syntax

The following picture shows AWS CLI commands Syntax:

AWS CLI commands Syntax


Example: LIST EC2 AMI Images

Let’s try the command to “show amazon EC2 images, that run CentOS”

PowerShell
 
aws --output table ec2 describe-images `
 --filter "Name=description, Values=*cent*" `
          "Name=owner-alias, Values=amazon"


Here is the command syntax break-down:

Command syntax break-down


The execution of the command generates the following output:

Command execution output


You’ll often run describe-based subcommands to get important resource-IDs, that you can then plug into your actual action commands

Top-level commands normally execute AWS services, Sub-commands execute some actions on service resources, options control the command environment and parameters narrow down the scope of your command.

Besides 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.

PowerShell
 
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:
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:
AWS Web console


Example: Terminate EC2 Instance

Here is the command to terminate an EC2 instance using CLI:

PowerShell
 
aws ec2 terminate-instances --instance-ids i-0858df785c04b25a4


Here is the output of executing the command:
Command execution output

AWS CLI Help

You can get some helpful information by using AWS help. A few examples are shown below:

PowerShell
 
aws help
aws s3 help
aws s3 website help


You can press the 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

PowerShell
 
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

PowerShell
 
aws s3 cp .\ s3://maptestdemo --recursive

The above shown command will copy all files from the current directory to the s3 bucket.

Here is the output of the operation:

Operation output


List Content of S3 Bucket

PowerShell
 
aws s3 ls maptestdemo

This command will list the content of the S3 bucket as shown below:
S3 bucket contents


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:

Website files

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 set up an S3 bucket as a public website. Some of these steps we’ve already done in the previous exercise.

Create a bucket

PowerShell
 
aws s3 mb s3://maptestdemo.com

Set Public Read Access for Bucket

Next, we will set public read access to the 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 Set-up Properly

aws s3api get-bucket-website --bucket maptestdemo.com

Here is the output of all above shown operations in regard to the static website:

Output of the static website operation


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]

Summary

In this post, we saw few more AWS CLI commands. We use these commands to create EC2 instances and we also learn how can we get some help with AWS commands. We then create an S3 bucket, copy files from the local directory to it and then configure this s3 bucket as a static website, which is publicly accessible. 

Let me know if you have any questions or comments. Till next time, happy coding!

AWS Command-line interface Command (computing)

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Terraform Best Practices: The 24 Practices You Should Adopt
  • Revolutionizing the Command Line: An Introduction to Gemini CLI
  • Rapid AWS Prototyping With LocalStack: A Developer’s Guide to Building AWS PoCs Locally
  • Establishing a Highly Available Kubernetes Cluster on AWS With Kops

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook