env.dev

AWS CLI Cheat Sheet — EC2, S3 & IAM Quick Reference

AWS CLI quick reference: EC2, S3, IAM, Lambda, and CloudFormation common commands with examples.

Last updated:

A quick reference for the AWS CLI (v2). Covers EC2, S3, IAM, Lambda, CloudFormation, and common flags for day-to-day cloud operations.

EC2

CommandDescription
aws ec2 describe-instancesList all EC2 instances
aws ec2 run-instances --image-id ami-xxx --instance-type t3.microLaunch an instance
aws ec2 start-instances --instance-ids i-xxxStart a stopped instance
aws ec2 stop-instances --instance-ids i-xxxStop a running instance
aws ec2 terminate-instances --instance-ids i-xxxTerminate an instance
aws ec2 describe-security-groupsList security groups
aws ec2 create-key-pair --key-name mykeyCreate an SSH key pair
aws ec2 describe-vpcsList VPCs

S3

CommandDescription
aws s3 lsList all buckets
aws s3 ls s3://bucket/prefix/List objects in bucket prefix
aws s3 cp file.txt s3://bucket/Upload file to S3
aws s3 cp s3://bucket/file.txt .Download file from S3
aws s3 sync ./dir s3://bucket/dirSync local directory to S3
aws s3 mb s3://new-bucketCreate a new bucket
aws s3 rb s3://bucket --forceDelete bucket and all objects
aws s3 presign s3://bucket/key --expires-in 3600Generate pre-signed URL (1hr)

IAM

CommandDescription
aws iam create-user --user-name devCreate IAM user
aws iam list-usersList all IAM users
aws iam create-access-key --user-name devCreate access key for user
aws iam attach-user-policy --user-name dev --policy-arn arn:...Attach policy to user
aws iam list-attached-user-policies --user-name devList user policies
aws iam create-role --role-name myrole --assume-role-policy-document file://trust.jsonCreate IAM role
aws sts get-caller-identityShow current IAM identity
aws iam delete-user --user-name devDelete IAM user

Lambda

CommandDescription
aws lambda list-functionsList all Lambda functions
aws lambda invoke --function-name myfn out.jsonInvoke a function
aws lambda create-function --function-name myfn --runtime nodejs20.x --handler index.handler --zip-file fileb://fn.zip --role arn:...Create a function
aws lambda update-function-code --function-name myfn --zip-file fileb://fn.zipUpdate function code
aws lambda get-function --function-name myfnGet function configuration
aws lambda delete-function --function-name myfnDelete a function
aws logs tail /aws/lambda/myfn --followTail function logs

CloudFormation

CommandDescription
aws cloudformation deploy --template-file tpl.yaml --stack-name mystackDeploy or update a stack
aws cloudformation describe-stacks --stack-name mystackDescribe a stack
aws cloudformation list-stacksList all stacks
aws cloudformation describe-stack-events --stack-name mystackShow stack events
aws cloudformation delete-stack --stack-name mystackDelete a stack
aws cloudformation validate-template --template-body file://tpl.yamlValidate a template

Common Flags

FlagDescription
--region us-east-1Override default AWS region
--profile stagingUse a named CLI profile
--output json | table | textSet output format
--query "Reservations[].Instances[]"JMESPath query to filter output
--dry-runValidate permissions without executing
--no-paginateDisable automatic pagination
--cli-auto-promptEnable interactive auto-prompt

Configuration

CommandDescription
aws configureSet up credentials interactively
aws configure listShow current configuration
aws configure set region us-west-2Set a specific config value
aws configure list-profilesList all configured profiles
export AWS_PROFILE=stagingSwitch profile via environment
export AWS_DEFAULT_REGION=eu-west-1Set region via environment

Frequently Asked Questions

How do I configure the AWS CLI?

Run aws configure to set your access key, secret key, default region, and output format. Credentials are stored in ~/.aws/credentials. Use AWS_PROFILE to switch between multiple accounts.

How do I sync files to S3?

Use aws s3 sync ./local-dir s3://bucket-name/ to upload. Add --delete to remove files from S3 that are not in the local directory. Use --exclude and --include for filtering.

How do I assume an IAM role?

Use aws sts assume-role --role-arn arn:aws:iam::123456:role/RoleName --role-session-name session1. Export the returned credentials as environment variables.