AWS CLI Cheat Sheet

The AWS Command Line Interface (CLI) is a tool provided by Amazon Web Services (AWS) that allows you to interact with various AWS services through a command-line interface. The AWS CLI enables you to manage AWS resources and automate common administrative tasks using scripts, allowing you to integrate AWS functionality into your own applications and workflows easily.

With the AWS CLI, you can create and manage AWS resources, configure security settings, monitor resource usage, and access resource logs. The AWS CLI supports many AWS services, including Amazon S3, Amazon EC2, Amazon RDS, AWS Lambda, Amazon DynamoDB, and others.

The AWS CLI is available for Windows, macOS, and Linux operating systems and can be installed using a package manager or by downloading and running an installer. Once installed, you can configure the AWS CLI with your AWS access keys and region to start interacting with AWS services from the command line.

Configure your Linux Environment to access your AWS account

Using the command, you can set your AWS access key ID and secret access key as environment variables in your Bash shell. Here’s an example:

export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key

Replace your_access_key_id and your_secret_access_key with your actual values.

You can also add these lines to your ~/.bashrc or ~/.bash_profile file so that they are automatically loaded each time you open a new terminal session.

Note: Storing your AWS access keys as environment variables can be risky, as anyone accessing your computer or account can also access your keys. It’s generally recommended to use the AWS Command Line Interface (CLI) tool or an SDK to manage your AWS credentials securely.

To test what account you are using you can type:

aws sts get-caller-identity

The output will look something like this:

    "UserId": "ABCDEFGHIJKLMNOPQ:example-user",
    "Account": "123456789012",
    "Arn": "arn:aws:iam::123456789012:user/example-user"

Copy Files to S3

The AWS Command Line Interface (CLI) can copy files from your local Linux server to an Amazon S3 bucket. Here’s an example command:

Copy a local file to S3 Bucket

aws s3 cp /path/to/local/file s3://bucket-name/path/to/s3/file

Replace /path/to/local/file with the path to the file on your local Linux server that you want to copy to the S3 bucket, and s3://bucket-name/path/to/s3/file with the S3 bucket name and path where you want to store the file. For example:

aws s3 cp /home/user/test.txt s3://my-bucket/test.txt

This command will copy the file /home/user/test.txt from the local Linux server to the S3 bucket my-bucket with the same name.

Copy multiple files to S3 recursively.

You can use the option if you want to copy multiple files or an entire directory recursively. For example:

aws s3 cp /path/to/local/directory s3://bucket-name/path/to/s3/directory --recursive

This command will copy all files and subdirectories under /path/to/local/directory to the S3 bucket bucket-name/path/to/s3/directory.

Copy all objects from one S3 bucket to another

aws s3 cp s3://source-bucket s3://destination-bucket --recursive

Replace source-bucket and destination-bucket with the names of the S3 buckets you want to copy objects from and to.

Note: You need to have AWS access keys configured on your Linux server with appropriate permissions to access the S3 bucket. You can configure your access keys using the aws configure command.

Elastic Block Storage (EBS)

Here are some useful AWS CLI commands to manage EBS volumes:

Create an EBS volume:

aws ec2 create-volume --availability-zone us-west-2a --size 50 --volume-type gp2

Replace us-west-2a with the availability zone where you want to create the volume, 50 with the size of the volume in GiB, and gp2 with the desired volume type.

Attach an EBS volume to an EC2 instance:

aws ec2 attach-volume --volume-id vol-0123456789abcdef --instance-id i-0123456789abcdef --device /dev/sdf

Replace vol-0123456789abcdef with the ID of the EBS volume you want to attach, i-0123456789abcdef with the ID of the EC2 instance to which you want to attach the volume, and /dev/sdf with the device name, you want to assign to the volume.

Detach an EBS volume from an EC2 instance:

aws ec2 detach-volume --volume-id vol-0123456789abcdef

Replace vol-0123456789abcdef with the ID of the EBS volume, you want to detach.

Delete an EBS volume:

aws ec2 delete-volume --volume-id vol-0123456789abcdef

Replace vol-0123456789abcdef with the ID of the EBS volume, you want to delete.

Create a snapshot of an EBS volume:

aws ec2 create-snapshot --volume-id vol-0123456789abcdef --description "My snapshot"

Replace vol-0123456789abcdef with the ID of the EBS volume, you want to create a snapshot of, and "My snapshot" with a description of the snapshot.

Delete an EBS snapshot:

aws ec2 delete-snapshot --snapshot-id snap-0123456789abcdef

Replace snap-0123456789abcdef with the ID of the EBS snapshot, you want to delete.

These are just a few examples of the many AWS CLI commands available to manage EBS volumes. Be sure to consult the AWS CLI documentation for more information on managing your EBS volumes.

Show all Instances that use gp2 storage.

Gp3 storage is a lot cheaper than gp2, and it performs better. There is no reason your environment should be using gp2 ahead of gp3

aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==Name].Value | [0], InstanceId, BlockDeviceMappings[?Ebs.VolumeType==gp2].Ebs.VolumeId]'

A more advanced query is below. You will need to edit the command to match your local tags

aws ec2 describe-volumes --filters "Name=volume-type,Values=gp2" --query 'Volumes[*].{ID:VolumeId,Size:Size,State:State,Instance:Attachments[0].InstanceId,"Created_by":Attachments[0].Tags[?Key=created-by]|[0].Value,"Stack Name":Tags[?Key===ws:cloudformation:stack-name=]|[0].Value,"Logical ID":Tags[?Key===aws:cloudformation:logical-id]|[0].Value,"Name":Tags[?Key==Name]|[0].Value,"Environment":Tags[?Key==Environment]|[0].Value,"Last Attachment Time":Attachments[0].AttachTime,"Last Detachment Time":Attachments[0].DetachTime,"Encryption":Encrypted,"KMS Key ID":KmsKeyId,"IOPS":Iops,"Throughput":Throughput}'

Elsewhere On TurboGeek:  DevOps Guide to Important Terraform Concepts


Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

1 Response

  1. 06/12/2023

    […] Workspaces is essentially a jump box that sits inside AWS and has access to all your AWS resources. It can be a Linux or Windows Desktop. (Simply look for workspaces […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate ยป