AWS CLI Command examples (Part-2)

AWS CLI Command examples (Part-2)

ยท

2 min read

AWS CLI is an open-source tool that enables users to interact with AWS services using commands or scripts from the user command line shell. It helps the user to manage multiple AWS services and automate them using scripts.

NOTE: AWS CLI versions 1 and 2 use the same aws command name. If you previously installed AWS CLI version 1, Migrate from AWS CLI version 1 to version 2.

Check whether AWS is installed or not on your system

Run command aws --version on your command prompt & it should have the output as below:

$ aws --version
aws-cli/2.10.0 Python/3.11.2 Windows/10 exe/AMD64 prompt/off

The command structure in the AWS CLI

AWS CLI command structure looks like as below

for more details on AWS CLI structure, do refer to the official AWS doc.

AWS CLI command Examples

Let's go through some of the commands for different AWS services.

  • For AWS S3

  • For AWS EC2

  • For AWS IAM

AWS CLI For AWS S3

  • List the S3 bucket aws s3 ls

  • Copies a local file aws s3 cp s3://mybucket/test.txt s3://mybucket/test2.txt

  • Deletes an S3 object aws s3 rm s3://mybucket/test2.txt

AWS CLI For AWS EC2

  • Launch EC2 instance aws ec2 run-instances --image-id ami-xxxxxx --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-xxxxxx --subnet-id subnet-xxxx

  • List all EC2 instances aws ec2 describe-instances

  • Create a security group for EC2 aws ec2 create-security-group --group-name my-sg --description "My security group" --vpc-id vpc-xxxx

  • Create a key pair for EC2 aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem

AWS CLI For AWS IAM

  • create IAM group aws iam create-group --group-name MyIamGroup

  • create IAM user aws iam create-user --user-name MyUser

  • Add the user to the created group aws iam add-user-to-group --user-name MyUser --group-name MyIamGroup

Additionally, there can be queries & filters applied in the AWS CLI command to get specific output like getting the EC2 details that are running and in n. Virginia region.

aws ec2 describe-instances \
--query "Reservations[*].Instances[*]"  --region us-east-1 \
--filters "Name=instance-state-name,Values=running"

The result can be depicted in different formats like JSON or table & others.

aws ec2 describe-instances \
--query "Reservations[*].Instances[*]"  --region us-east-1 \
--filters "Name=instance-state-name,Values=running" --output table

To get more details commands for different AWS services then do check out this official AWS CLI doc.

I hope this blog is helpful for you to understand AWS CLI, Please do comment & like.

ย