Skip to content

AWS CLI

Command line interface for Amazon Web Services.

Configuration

COMMANDDESCRIPTION
aws configureInteractive configuration
aws configure --profile profilenameConfigure named profile
aws configure listList current configuration
aws configure set region us-east-1Set default region

EC2 (Elastic Compute Cloud)

COMMANDDESCRIPTION
aws ec2 describe-instancesList all EC2 instances
aws ec2 run-instances --image-id ami-xxx --count 1Launch EC2 instance
aws ec2 start-instances --instance-ids i-xxxStart instance
aws ec2 stop-instances --instance-ids i-xxxStop instance
aws ec2 terminate-instances --instance-ids i-xxxTerminate instance
aws ec2 describe-instances --instance-id i-xxxGet instance details
aws ec2 create-key-pair --key-name mykeyCreate SSH key pair
aws ec2 create-security-group --name mysgCreate security group

S3 (Simple Storage Service)

COMMANDDESCRIPTION
aws s3 lsList all buckets
aws s3 ls s3://bucketnameList objects in bucket
aws s3 mb s3://bucketnameCreate bucket
aws s3 rb s3://bucketnameDelete bucket
aws s3 cp file.txt s3://bucketname/Upload file
aws s3 cp s3://bucketname/file.txt .Download file
aws s3 sync ./dir s3://bucketname/dirSync directory
aws s3 rm s3://bucketname/file.txtDelete object
aws s3api put-bucket-versioning --bucket name --versioning-configuration Status=EnabledEnable versioning

IAM (Identity and Access Management)

COMMANDDESCRIPTION
aws iam list-usersList IAM users
aws iam create-user --user-name usernameCreate user
aws iam delete-user --user-name usernameDelete user
aws iam list-rolesList IAM roles
aws iam get-role --role-name rolenameGet role details
aws iam create-access-key --user-name usernameCreate access key
aws iam list-access-keys --user-name usernameList access keys
aws iam attach-user-policy --user-name user --policy-arn arnAttach policy to user

Lambda

COMMANDDESCRIPTION
aws lambda list-functionsList Lambda functions
aws lambda invoke response.json --function-name myfuncInvoke function
aws lambda get-function --function-name myfuncGet function details
aws lambda update-function-code --function-name myfunc --zip-file fileb://deployment.zipUpdate function code
aws logs tail /aws/lambda/myfunc --followTail Lambda logs

RDS (Relational Database Service)

COMMANDDESCRIPTION
aws rds describe-db-instancesList DB instances
aws rds create-db-instance --db-instance-identifier mydb --db-instance-class db.t3.microCreate DB instance
aws rds start-db-instance --db-instance-identifier mydbStart DB instance
aws rds stop-db-instance --db-instance-identifier mydbStop DB instance
aws rds delete-db-instance --db-instance-identifier mydb --skip-final-snapshotDelete DB instance

CloudWatch

COMMANDDESCRIPTION
aws cloudwatch list-metricsList all metrics
aws cloudwatch get-metric-statistics --namespace AWS/EC2 --metric-name CPUUtilizationGet metric statistics
aws logs describe-log-groupsList log groups
aws logs tail /aws/lambda/myfunc --followTail log group
aws logs filter-log-events --log-group-name /aws/lambda/myfuncGet log events

CloudFormation

COMMANDDESCRIPTION
aws cloudformation list-stacksList all stacks
aws cloudformation create-stack --stack-name mystack --template-body file://template.yamlCreate stack
aws cloudformation update-stack --stack-name mystack --template-body file://template.yamlUpdate stack
aws cloudformation delete-stack --stack-name mystackDelete stack
aws cloudformation describe-stack-events --stack-name mystackGet stack events
aws cloudformation describe-stack-resources --stack-name mystackList stack resources

VPC (Virtual Private Cloud)

COMMANDDESCRIPTION
aws ec2 describe-vpcsList VPCs
aws ec2 create-vpc --cidr-block 10.0.0.0/16Create VPC
aws ec2 describe-subnetsList subnets
aws ec2 create-subnet --vpc-id vpc-xxx --cidr-block 10.0.1.0/24Create subnet
aws ec2 describe-route-tablesList route tables
aws ec2 create-internet-gatewayCreate internet gateway

ECR (Elastic Container Registry)

COMMANDDESCRIPTION
aws ecr describe-repositoriesList ECR repositories
aws ecr create-repository --repository-name myrepoCreate repository
`aws ecr get-login-password --region us-east-1docker login --username AWS --password-stdin acct-id.dkr.ecr.us-east-1.amazonaws.com`
docker push acct-id.dkr.ecr.us-east-1.amazonaws.com/myrepo:tagPush image

ECS (Elastic Container Service)

COMMANDDESCRIPTION
aws ecs list-clustersList ECS clusters
aws ecs create-cluster --cluster-name myclusterCreate cluster
aws ecs list-tasks --cluster myclusterList tasks in cluster
aws ecs list-services --cluster myclusterList services
aws ecs update-service --cluster mycluster --service myservice --desired-count 3Scale service

Common Queries

Filter EC2 instances by tag

bash
aws ec2 describe-instances \
  --filters "Name=tag:Environment,Values=Production"

Get public IP of instance

bash
aws ec2 describe-instances \
  --instance-ids i-xxx \
  --query "Reservations[0].Instances[0].PublicIpAddress" \
  --output text

List S3 objects with size

bash
aws s3api list-objects-v2 \
  --bucket mybucket \
  --query 'Contents[].{Key:Key,Size:Size}'

Get Lambda function size

bash
aws lambda get-function \
  --function-name myfunc \
  --query 'Code.Location'

List IAM policies attached to user

bash
aws iam list-attached-user-policies \
  --user-name username

Best Practices

  • Use IAM roles instead of access keys when possible
  • Enable MFA for root account and IAM users
  • Use AWS CLI profiles for multiple accounts
  • Set default region to avoid specifying it in every command
  • Use --query to filter output and get specific data
  • Enable versioning on important S3 buckets
  • Use S3 lifecycle policies to manage object retention
  • Tag resources for cost allocation and organization
  • Use AWS CloudTrail to audit API calls
  • Enable encryption for sensitive data

TIP

Use the --dry-run flag to test commands without making actual changes.

Released under MIT License.