Introduction to AWS Command Line Interface and some necessary commands
Tags: aws, ec2, ami, script
Published: 2023-07-06 16:02:42

The AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

**AWS CLI install and update instructions**

_Linux:_

```
sudo apt-get update
sudo apt install unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
```

_Windows_

Download and run the AWS CLI MSI installer for Windows (64-bit):
https://awscli.amazonaws.com/AWSCLIV2.msi

To confirm the installation, open the Start menu, search for cmd to open a command prompt window, and at the command prompt use the aws --version command.

**aws configure**
Run this command to quickly set and view your credentials, Region, and output format. The following example shows sample values.

To get the access key information, go to IAM service, then select the user -> create access key


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qqr97xbdzh5iqdq7gfv.png)

Then select CLI

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9c4663yszrn6e4f7h6c.png)

And finally, you get the access key information.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx07lfb94v6pfsgcbyol.png)



```
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: json
```


**AWS CLI command:**

_EC2:_

```
# list all instances (running, and not running)
# http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html
aws ec2 describe-instances

# list all instances running
aws ec2 describe-instances --filters Name=instance-state-name,Values=running


# Create Image
aws ec2 create-image --instance-id i-097110090aa174e39 --name "Image_Name" --no-reboot

# Move the image into the S3 bucket.
aws ec2 create-store-image-task --image-id ami-0012a3609a0032d94 --bucket bucket_name

# Restore the image from the S3 bucket.
aws ec2 create-restore-image-task --object-key ami-059ab5909e3c2dbd6.bin --bucket bucket_name --name "image_name"

# To see the store task 
aws ec2 describe-store-image-tasks

# list all private AMI's, ImageId and Name tags
aws ec2 describe-images --filter "Name=is-public,Values=false" \
    --query 'Images[].[ImageId, Name]' \
    --output text | sort -k2

# delete an AMI, by ImageId
aws ec2 deregister-image --image-id ami-00000000

```

I think you will find more interesting thing here.
**Create Image from the Autoscaling group:**

```
# Set variables
read -p "Enter Instance Name : " EC2_INSTANCE_NAME

# Set image description and tag with timestamp 
TIME=$(TZ='Asia/Dhaka' date '+%d-%m-%y'-'%H-%M')

AMI_NAME=$EC2_INSTANCE_NAME'-'$TIME
IMAGE_DESCRIPTION=$EC2_INSTANCE_NAME'-'$TIME
IMAGE_TAG=$EC2_INSTANCE_NAME'-'$TIME 
#Query the running Instance ID
INSTANCE_ID=$(aws ec2 describe-instances --filters  "Name=tag:Name,Values=$EC2_INSTANCE_NAME" "Name=instance-state-name,Values=running" --query 'Reservations[*].Instances[*].[InstanceId]' --output text |head -n 1)

# Create the AMI
AMI_ID=$(aws ec2 create-image --instance-id "$INSTANCE_ID" --name "$AMI_NAME" --description "$IMAGE_DESCRIPTION" --tag-specifications 'ResourceType=image,Tags=[{Key=Name,Value='$IMAGE_TAG'}]' --no-reboot --output text)

# Capture the status of the image
state1=$(aws ec2 describe-images --image-ids $AMI_ID --query 'Images[*].State' --output text)
echo 'Image on '$state1' state and Image ID is '$AMI_ID''
echo 'Please wait until the image is available....'

# Wait till the image available 
aws ec2 wait image-available --image-ids $AMI_ID

# finally, capture the update state of the image
state2=$(aws ec2 describe-images --image-ids $AMI_ID --query 'Images[*].State' --output text)
echo Image is $state2
```

Hope it will help you. 
You can explore more..
https://docs.aws.amazon.com/cli/latest/index.html