Overview

Commands target AWS CLI v2. Run aws sts get-caller-identity first to confirm which credentials are active. Region precedence is --region, then AWS_REGION, then AWS_DEFAULT_REGION, then the profile; --profile <name> or AWS_PROFILE selects a profile. For CI credentials see github-secrets; for JSON post-processing see jq-syntax.

S3

Preview with --dryrun before any sync or rm --recursive.

CommandEffect
aws s3 ls s3://bucket/prefix/List objects (aws s3 ls lists buckets).
aws s3 cp f.txt s3://bucket/f.txtUpload (swap the arguments to download).
aws s3 sync ./dist s3://bucket/ --dryrunPreview a sync; drop --dryrun to run it.
aws s3 sync ./dist s3://bucket/ --deleteAlso delete destination objects absent from the source.
aws s3 rm s3://bucket/prefix/ --recursiveDelete everything under a prefix.
aws s3 presign s3://bucket/f.txt --expires-in 3600Temporary download URL.
aws s3 mb s3://new-bucket / aws s3 rb s3://old-bucketCreate / delete an empty bucket.
# Static site: long cache for assets, no cache for the entry page
aws s3 sync dist s3://bucket/ --delete --cache-control "max-age=31536000,public" --exclude "index.html"
aws s3 cp dist/index.html s3://bucket/index.html --cache-control "no-cache"

EC2

Filter with --query instead of parsing text output.

CommandEffect
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,State.Name,PublicIpAddress]' --output tableCompact instance list.
aws ec2 start-instances --instance-ids i-0abc123Start (also stop-instances, terminate-instances).
aws ec2 describe-security-groups --group-ids sg-0abcInspect rules.
aws ec2 describe-images --owners selfYour AMIs.

Lambda and logs

CLI v2 treats --payload as base64 unless you set the binary format, so pass it with raw JSON.

CommandEffect
aws lambda invoke --function-name fn --cli-binary-format raw-in-base64-out --payload '{"a":1}' out.jsonSynchronous call; response in out.json.
aws lambda invoke ... --invocation-type Event ...Async call; returns 202 whether or not the function later fails.
aws lambda update-function-code --function-name fn --zip-file fileb://fn.zipDeploy a zip (fileb:// reads binary).
aws lambda update-function-configuration --function-name fn --timeout 30Change memory, timeout, env vars.
aws lambda list-functions, aws lambda get-function-configuration --function-name fnList functions / read one’s settings.
aws logs tail /aws/lambda/fn --followStream CloudWatch logs.

Set the format once: aws configure set cli_binary_format raw-in-base64-out.

STS and IAM

Use temporary role credentials and avoid long-lived access keys.

CommandEffect
aws sts get-caller-identityAccount, ARN, and user ID of the active credentials.
aws sts assume-role --role-arn <arn> --role-session-name s1Temporary credentials for a role.
aws iam list-attached-user-policies --user-name alicePolicies attached to a user.
aws iam get-role --role-name MyRoleTrust policy and ARN.
aws iam list-users, aws iam list-rolesEnumerate principals.
aws iam simulate-principal-policy --policy-source-arn <arn> --action-names s3:PutObjectTest whether a principal may act.

Prefer a profile with role_arn and source_profile in ~/.aws/config over exporting assumed-role keys by hand, and aws configure sso over long-lived access keys.

Gotchas

Check these when a command fails or hangs in a script.

GotchaFix
s3 sync leaves stale objects in the destination.Add --delete, and preview with --dryrun.
v2 sends output through a pager and stalls scripts.export AWS_PAGER="" or --no-cli-pager.
lambda invoke --payload '{}' fails with an invalid base64 error.Add --cli-binary-format raw-in-base64-out.
--output table is for people; --output json is for scripts.Filter with --query (JMESPath) before assigning to variables.
A CI default region can differ from your laptop.Set AWS_REGION or pass --region explicitly.
Access denied on every call.Run aws sts get-caller-identity to see who you are.