AB
A comprehensive guide to AWS CLI - from installation and basic commands to intermediate operations and best practices
AWS CLI (Command Line Interface) is a tool that allows users to interact with AWS services through a set of text-based commands. Think of it as typing instructions into a “command box” to perform tasks on AWS services, rather than clicking through a web interface (AWS Management Console).
Let’s say you want to see all the S3 buckets you own. With AWS CLI, you simply type:
aws s3 ls
This tool is highly flexible and powerful, as it supports nearly all AWS services and allows automation via scripts.
AWS CLI is a game-changer for cloud management. Here’s why you might choose it over the Management Console:
Speed
Instead of clicking through multiple screens to create an S3 bucket, you can use a single command:
aws s3 mb s3://my-new-bucket
my-new-bucket
.Automation
AWS CLI supports scripting. This means you can write scripts to automate repetitive tasks, like starting multiple EC2 instances at specific times.
For instance:
aws ec2 start-instances --instance-ids i-0abc12345678defg i-0hij12345678klmn
Scriptability
CLI integrates seamlessly with shell scripting tools (like Bash or PowerShell). You can create custom workflows, such as daily backups or scheduled cleanups.
Broad Coverage
CLI supports nearly every AWS service, including some not yet available in the AWS Management Console.
Imagine the AWS Management Console as a detailed recipe book. While it’s great for learning, it takes time to flip through pages to find a recipe. AWS CLI is like asking a chef to directly prepare the dish for you when you give a short command!
Before diving into AWS CLI, here’s what you’ll need:
Sign up at AWS to create your account. You can use the free tier for most basic tasks while learning.
Understanding AWS services like S3 (storage), EC2 (virtual servers), and IAM (access management) will make your experience smoother.
This section will guide you through the essential steps to set up and start using AWS CLI effectively.
AWS CLI is available for different operating systems, and the installation steps are straightforward.
aws --version
aws-cli/2.12.2 Python/3.9.10 Windows/10
AWS CLI can be installed using pip
, the Python package manager:
pip install awscli
aws --version
Verifying ensures that AWS CLI is properly installed and available in your system’s path, so you can use its commands directly.
Once AWS CLI is installed, it needs to be linked to your AWS account. This is done using the aws configure
command.
aws configure
us-east-1
, ap-south-1
).json
for clarity).Here’s how the process looks:
$ 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
json
: Machine-readable and visually structured.table
: Easy-to-read, tabular format.text
: Minimal, raw output.Yes, you can run aws configure
again to update your credentials or preferences.
AWS CLI commands follow a consistent structure, which makes them easy to learn and use.
aws <service> <operation> --parameter1 value1 --parameter2 value2
Suppose you want to list all the S3 buckets in your AWS account. The command would be:
aws s3 ls
aws
: Invokes the AWS CLI.s3
: Specifies the AWS service (S3 in this case).ls
: The operation to perform, which lists all buckets.Let’s create an S3 bucket named my-new-bucket
:
aws s3 mb s3://my-new-bucket
mb
: Stands for “Make Bucket.”s3://my-new-bucket
: Specifies the name of the new bucket.my-new-bucket
is created in your default region.AWS CLI commands often require parameters to specify details. For example, launching an EC2 instance involves passing parameters like instance type and key name:
aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name my-key --security-group-ids sg-0123456789abcdef
--image-id
: The Amazon Machine Image (AMI) to use.--count
: Number of instances to launch (here, 1).--instance-type
: Type of instance (e.g., t2.micro
for free tier).--key-name
: Name of the key pair for SSH access.--security-group-ids
: Security group to associate with the instance.Think of AWS CLI commands like filling out a form:
aws s3 ls
to explore and interact with services.AWS CLI offers a vast range of commands to interact with different AWS services. Let’s start with some basic and commonly used commands across various services, with examples and explanations.
aws --version
aws-cli/2.12.2 Python/3.9.10 Windows/10
aws help
aws s3 help
s3
service.aws s3 mb s3://my-first-bucket
my-first-bucket
in your default region.make_bucket: my-first-bucket
aws s3 cp myfile.txt s3://my-first-bucket/
myfile.txt
to the bucket named my-first-bucket
.upload: ./myfile.txt to s3://my-first-bucket/myfile.txt
aws s3 ls s3://my-first-bucket/
my-first-bucket
.2024-12-17 10:32:45 1024 myfile.txt
aws s3 rb s3://my-first-bucket --force
my-first-bucket
along with its contents.remove_bucket: my-first-bucket
aws ec2 describe-instances
aws ec2 start-instances --instance-ids i-1234567890abcdef0
{
"StartingInstances": [
{
"InstanceId": "i-1234567890abcdef0",
"CurrentState": {
"Code": 0,
"Name": "pending"
},
"PreviousState": {
"Code": 80,
"Name": "stopped"
}
}
]
}
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
Name
set to “stopping.”aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name my-key --security-group-ids sg-0123456789abcdef
aws iam list-users
aws iam create-user --user-name new-user
new-user
.{
"User": {
"Path": "/",
"UserName": "new-user",
"UserId": "AIDAIEXAMPLEID",
"Arn": "arn:aws:iam::123456789012:user/new-user",
"CreateDate": "2024-12-17T12:34:56Z"
}
}
aws cloudwatch list-metrics
aws cloudwatch get-metric-data --metric-data-queries file://queries.json --start-time 2024-12-16T00:00:00Z --end-time 2024-12-17T00:00:00Z
queries.json
.aws lambda list-functions
aws lambda invoke --function-name my-function-name response.json
my-function-name
and stores the response in response.json
.response.json
file containing the function’s output.help
option to learn more about available commands and their parameters.After mastering the basic commands, you can move on to intermediate-level commands that involve more complex operations and interactions with AWS services. Below are some commonly used commands across multiple services.
aws s3 sync ./local-folder s3://my-bucket-name
upload: ./file1.txt to s3://my-bucket-name/file1.txt
upload: ./file2.txt to s3://my-bucket-name/file2.txt
aws s3api put-bucket-versioning --bucket my-bucket-name --versioning-configuration Status=Enabled
aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --instance-type "{\"Value\":\"t3.large\"}"
t3.large
. The instance must be stopped before resizing.{
"ResponseMetadata": {
"RequestId": "abc12345-6789-0def-1234-567890abcdef",
"HTTPStatusCode": 200
}
}
aws ec2 create-key-pair --key-name MyKeyPair --query "KeyMaterial" --output text > MyKeyPair.pem
MyKeyPair.pem
.aws dynamodb create-table \
--table-name MyTable \
--attribute-definitions AttributeName=ID,AttributeType=S \
--key-schema AttributeName=ID,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
MyTable
with a primary key ID
of type string.CREATING
.aws dynamodb scan --table-name MyTable
MyTable
.aws iam attach-user-policy --user-name MyUser --policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
MyUser
access to perform all actions on S3.aws iam create-role --role-name MyRole --assume-role-policy-document file://trust-policy.json
MyRole
with the trust policy specified in trust-policy.json
.aws cloudformation create-stack --stack-name MyStack --template-body file://template.json
template.json
.{
"StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/MyStack/abc12345-def6-7890-ghij-klmnopqrstuv"
}
aws cloudformation list-stacks
aws cloudwatch put-metric-alarm \
--alarm-name CPUAlarm \
--metric-name CPUUtilization \
--namespace AWS/EC2 \
--statistic Average \
--period 300 \
--threshold 80 \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions Name=InstanceId,Value=i-1234567890abcdef0 \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:us-east-1:123456789012:MyTopic
{
"ResponseMetadata": {
"RequestId": "abc12345-6789-0def-1234-567890abcdef",
"HTTPStatusCode": 200
}
}
aws logs get-log-events --log-group-name MyLogGroup --log-stream-name MyLogStream
--help
option for additional details and examples.