AWS CLI Cheatsheet
Real commands and --query patterns for EC2, Lambda, networking, and Route 53 โ the services this covers best. IAM and S3 CLI usage arenโt covered here (see note at the bottom). For the full explanations, see the AWS guides.
EC2
# Launch an instanceaws ec2 run-instances \ --image-id ami-0c02fb55956c7d316 \ --instance-type t3.medium \ --key-name my-keypair \ --security-group-ids sg-0abc123def456 \ --subnet-id subnet-0a1b2c3d \ --iam-instance-profile Name=AppServerProfile \ --user-data file://startup.sh \ --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'# Burstable instance in unlimited (no CPU-credit throttling) modeaws ec2 run-instances \ --instance-type t3.medium \ --image-id ami-0c02fb55956c7d316 \ --credit-specification '{"CpuCredits":"unlimited"}'# Reserved Instance offerings lookupaws ec2 describe-reserved-instances-offerings \ --instance-type m5.large \ --product-description "Linux/UNIX" \ --offering-class standard \ --query 'ReservedInstancesOfferings[?Duration==`31536000`].[OfferingType,RecurringCharges]' \ --output table# Spot Fleet across instance types and AZsaws ec2 create-fleet \ --type maintain \ --target-capacity-specification TotalTargetCapacity=10,DefaultTargetCapacityType=spot \ --launch-template-configs '[ {"LaunchTemplateSpecification": {"LaunchTemplateName": "my-template", "Version": "$Latest"}, "Overrides": [ {"InstanceType": "m5.large", "SubnetId": "subnet-1a"}, {"InstanceType": "m5a.large", "SubnetId": "subnet-1a"} ]} ]' \ --spot-options AllocationStrategy=diversifiedElastic IP (best real --query examples in the corpus)
# Allocate and read back the allocation ID / public IP with jqEIP_INFO=$(aws ec2 allocate-address --domain vpc --output json)EIP_ALLOC=$(echo $EIP_INFO | jq -r '.AllocationId')EIP_ADDR=$(echo $EIP_INFO | jq -r '.PublicIp')
# Associate with a running instanceaws ec2 associate-address --instance-id i-0abc123 --allocation-id $EIP_ALLOC
# Verify the associationaws ec2 describe-addresses \ --allocation-ids $EIP_ALLOC \ --query 'Addresses[0].[PublicIp,InstanceId,AssociationId]' \ --output table# DisassociateASSOC_ID=$(aws ec2 describe-addresses --allocation-ids $EIP_ALLOC \ --query 'Addresses[0].AssociationId' --output text)aws ec2 disassociate-address --association-id $ASSOC_ID# Find unassociated EIPs (the ones costing you money) and releaseaws ec2 describe-addresses \ --query 'Addresses[?AssociationId==`null`].[AllocationId,PublicIp]' \ --output tableaws ec2 release-address --allocation-id $EIP_ALLOC# Count allocations in a region โ JMESPath length() functionaws ec2 describe-addresses --query 'length(Addresses)' --output textLambda
# Create from a zip packagezip function.zip lambda_function.pyaws lambda create-function \ --function-name process-orders \ --runtime python3.12 \ --role arn:aws:iam::123456789012:role/lambda-exec-role \ --handler lambda_function.lambda_handler \ --zip-file fileb://function.zip \ --memory-size 256 \ --timeout 30 \ --environment Variables='{TABLE_NAME=orders,REGION=us-east-1}'# Deploy as a container image (supports up to 10 GB)aws lambda create-function \ --function-name ml-inference \ --package-type Image \ --code ImageUri=123456789012.dkr.ecr.us-east-1.amazonaws.com/ml-model:latest \ --role arn:aws:iam::123456789012:role/lambda-exec-role \ --memory-size 3008 \ --timeout 60# Allow another service (e.g. S3) to invoke the functionaws lambda add-permission \ --function-name doc-processor \ --statement-id s3-trigger \ --action lambda:InvokeFunction \ --principal s3.amazonaws.com \ --source-arn arn:aws:s3:::my-upload-bucket# Attach a layer, or update VPC configaws lambda update-function-configuration \ --function-name my-function \ --layers arn:aws:lambda:us-east-1:123456789:layer:pandas-layer:1
aws lambda update-function-configuration \ --function-name api-handler \ --vpc-config SubnetIds=subnet-0a1b2c,subnet-0d4e5f,SecurityGroupIds=sg-lambda-outboundLambda concurrency
# Reserved concurrency: set / check / removeaws lambda put-function-concurrency --function-name payment-processor --reserved-concurrent-executions 100aws lambda get-function-concurrency --function-name payment-processoraws lambda delete-function-concurrency --function-name payment-processor# Provisioned concurrency on a published versionVERSION=$(aws lambda publish-version --function-name api-handler --query 'Version' --output text)aws lambda put-provisioned-concurrency-config \ --function-name api-handler --qualifier $VERSION --provisioned-concurrent-executions 10# ...or on an aliasaws lambda create-alias --function-name api-handler --name prod --function-version $VERSIONaws lambda put-provisioned-concurrency-config \ --function-name api-handler --qualifier prod --provisioned-concurrent-executions 10# Monitor concurrent executionsaws cloudwatch get-metric-statistics \ --namespace AWS/Lambda --metric-name ConcurrentExecutions --statistics Maximum \ --period 60 --start-time 2024-01-15T00:00:00Z --end-time 2024-01-15T01:00:00ZDynamoDB Streams โ Lambda
aws dynamodb update-table --table-name orders \ --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES
STREAM_ARN=$(aws dynamodb describe-table --table-name orders \ --query 'Table.LatestStreamArn' --output text)
aws lambda create-event-source-mapping \ --function-name audit-logger --event-source-arn $STREAM_ARN \ --starting-position LATEST --batch-size 100Networking, Route 53, Auto Scaling
# Route 53: create a hosted zone and read its IDZONE_ID=$(aws route53 create-hosted-zone \ --name example.com --caller-reference "$(date +%s)" \ --query 'HostedZone.Id' --output text)
aws route53 change-resource-record-sets \ --hosted-zone-id $ZONE_ID \ --change-batch file://record-changes.json# Auto Scaling: launch template + group + scaling policyaws ec2 create-launch-template --launch-template-name web-lt --launch-template-data file://lt.jsonaws autoscaling create-auto-scaling-group \ --auto-scaling-group-name web-asg \ --launch-template LaunchTemplateName=web-lt,Version='$Latest' \ --min-size 2 --max-size 10 --desired-capacity 3 \ --vpc-zone-identifier "subnet-1a,subnet-1b"aws autoscaling put-scaling-policy \ --auto-scaling-group-name web-asg --policy-name scale-up --scaling-adjustment 2 --adjustment-type ChangeInCapacity# EKS: create cluster and node groupaws eks create-cluster --name my-cluster --role-arn arn:aws:iam::123456789012:role/eks-role --resources-vpc-config subnetIds=subnet-1a,subnet-1baws eks update-kubeconfig --name my-clusteraws eks create-nodegroup --cluster-name my-cluster --nodegroup-name workers --subnets subnet-1a subnet-1b --node-role arn:aws:iam::123456789012:role/node-roleFlags & โquery notes
| Flag | Purpose |
|---|---|
--query '<jmespath>' | Filter/reshape the response before it prints |
--output table | text | json | Output format โ text is ideal for capturing a single value into a shell variable |
--region <region> | Override the default region for one call |
JMESPath patterns actually used above
| Pattern | Example |
|---|---|
| Field path | Table.LatestStreamArn |
| Array projection (pick columns) | Addresses[0].[PublicIp,InstanceId,AssociationId] |
| Filter expression | Addresses[?AssociationId==`null`] |
| Function call | length(Addresses) |
S3 (limited coverage in source content)
aws s3 cp local.txt s3://my-demo-bucket/encrypted.txt --sse AES256aws s3 cp local.txt s3://my-kms-bucket/kms-file.txt --sse aws:kmsaws s3api put-object \ --bucket my-ssec-bucket --key secret.txt --body local.txt \ --sse-customer-algorithm AES256 --sse-customer-key fileb://my_key.binNot covered: IAM CLI (create-role, attach-role-policy, etc.) and STS donโt appear anywhere in our source guides โ IAM only shows up as inline JSON policy documents attached to other services. aws s3 sync and aws s3 ls also arenโt documented in our current content. If you need these, the official AWS CLI reference is the best source until we add dedicated guides.