Using AWS CLI to manage EBS volumes

Intro to AWS CLI

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

To install AWS CLI on MacOS:

$ curl "" -o "AWSCLIV2.pkg"
$ sudo installer -pkg AWSCLIV2.pkg -target /
$ which aws
$ aws --version
aws-cli/2.10.3 Python/3.9.11 Darwin/20.4.0 exe/x86_64 prompt/off

Configure AWS credentials

The AWS CLI stores sensitive credential information that you specify with aws configure in a local file ~/.aws/credentials.

$ aws configure

AWS Access Key ID [****************6N7Q]: [Your AWS access Key ID]

AWS Secret Access Key [****************+Ic+]: [Your AWS Secret Access Key]

Default region name [None]: [Your Region Name]

Default output format [None]:

$ cat ~/.aws/credentials


aws_access_key_id = [Your AWS access Key ID]

aws_secret_access_key = [Your AWS Secret Access Key]

aws_region = [Your Region Name]

AWS CLI Commands

To get commands help:

$ aws eks help
$ aws ec2 help

To list and describe clusters:

$ aws eks list-clusters
$ aws eks describe-cluster --name [your-cluster-name]

To list and describe nodegroups:

$ aws eks list-nodegroups --cluster-name [your-cluster-name]
$ aws eks describe-nodegroup --cluster-name [your-cluster-name] --nodegroup-name [you-nodegroup-name]

To delete multiple EBS volumes(example):

$ aws ec2 describe-volumes --filters "Name=tag:Name,Values=gp3-volume-*"  | egrep "VolumeId" | awk '{print $NF}' | sed 's/\"//g;s/\,//' > vol_ids

$ for id in `cat vol_ids`
    aws ec2 delete-volume --volume-id $id

$ aws ec2 describe-volumes --filters "Name=tag:Name,Values=gp3-volume-*"  | egrep "VolumeId" | awk '{print $NF}'  | wc -l

To create multiple EBS volumes(example):

$ for i in `seq 1 8`
aws ec2 create-volume --volume-type gp3 --size 256 --iops 16000 --throughput 1000 --availability-zone us-east-1a --tag-specifications "ResourceType=volume,Tags=[{Key=Name,Value=gp3-volume-$i}]"

To attach EBS volume to EC2 instance:

$ aws ec2 describe-volumes --filters "Name=tag:Name,Values=gp3-volume-1" --query "Volumes[*].{ID:VolumeId}"
        "ID": "vol-0101002b66d4fc211"

$ aws ec2 attach-volume --volume-id vol-0101002b66d4fc211 --instance-id i-0c2b7553a99a7277b --device /dev/sdf
    "AttachTime": "2023-03-04T01:58:37.155000+00:00",
    "Device": "/dev/sdf",
    "InstanceId": "i-0c2b7553a99a7277b",
    "State": "attaching",
    "VolumeId": "vol-0101002b66d4fc211"

To describe instance:

$ aws ec2 describe-instances --instance-ids i-0c2b7553a99a7277b | egrep "DeviceName|Status|VolumeId"
"DeviceName": "/dev/sdf",
"Status": "attached",
"VolumeId": "vol-0101002b66d4fc211"
"Status": "attached"

To verify the attached volumes in EC2 instance:

[ec2-user@ip-192-168-25-183 ~]$ ls -la /dev/sdf
lrwxrwxrwx 1 root root 7 Mar  4 01:58 /dev/sdf -> nvme1n1

[ec2-user@ip-192-168-25-183 ~]$ lsblk
nvme0n1       259:0    0  256G  0 disk
├─nvme0n1p1   259:1    0  256G  0 part /
└─nvme0n1p128 259:2    0    1M  0 part
nvme1n1       259:3    0  256G  0 disk