AWS CLI v2 Cheat Sheet: SSO Profiles, --query, S3, EC2 and CloudWatch Logs
Checked against AWS CLI AWS CLI v2 (latest tag 2.42.5 on GitHub, 28 Nov 2026) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet covers the AWS CLI v2 commands a developer reaches for every week, from signing in with IAM Identity Center to filtering output and tailing logs. It follows the v2 user guide and command reference as published on 28 Nov 2026; the newest tag on the aws/aws-cli repo was 2.42.5. The CLI is blocked in my test shell, so every row is from the docs and marked untested.
When to use what
Sign in with IAM Identity Center (aws configure sso) rather than long-lived access keys. The docs recommend the sso-session style config because it refreshes tokens for you, and one session can back several profiles for different accounts and roles. Run aws sts get-caller-identity whenever you are unsure who you are; it is the cheapest way to catch the wrong profile before you change anything.
For filtering, use server-side --filters (or --filter, depending on the service) first, because the service then sends less data. Use --query after that to shape what you see. --query takes JMESPath, which looks like JSONPath but is a different language; our JSONPath evaluator helps with the JSON shape, but test the final JMESPath expression with the CLI itself.
Pick --output json for scripts that parse results, text only together with a --query that fixes the column order, and table for your own eyes. The new off format is handy in CI when only the exit code matters.
For S3, aws s3 (cp, sync, rm) is the high-level layer you want most of the time; drop to aws s3api when you need a specific API call such as head-bucket or aborting a multipart upload. If you are letting an AI agent drive AWS, the AWS MCP server and our post on MCP servers for cloud infrastructure cover the setup, and the IAM policy evaluator helps you scope the role it uses.
The token cache lives under ~/.aws/sso/cache. One [sso-session] block can be shared by many [profile] blocks.
aws configure ssoWizard that asks for a session name, start URL, SSO region and scopes, then lets you pick an account and role
aws configure sso --use-device-codeSame wizard with device authorization, for a machine that cannot open a browser
aws configure sso-sessionCreate or edit only the [sso-session] block in ~/.aws/config
aws sso login --profile my-dev-profileOpen the browser sign-in and cache a token for that profile
aws sso login --sso-session my-ssoSign in once for a session that several profiles share
aws sso logoutDelete cached Identity Center credentials for all profiles
aws sts get-caller-identity --profile my-dev-profileShow the account, user ID and ARN the profile resolves to
With --output text the CLI paginates first and runs the query on each page, so a [0] can return one item per page. JSON and YAML run the query once on the full result.
--query 'Volumes[*].Attachments[].State'Flatten a nested list into one array of values
--query 'Volumes[?Size < `20`].VolumeId'Filter a list by a numeric field; literals go in backticks
--query 'Volumes[].{ID:VolumeId,Type:VolumeType}'Multiselect hash: pick fields and rename them
--query 'sort_by(Images, &CreationDate)[-1].ImageId'Sort with a function and take the newest item
--query 'length(Volumes[?Iops > `1000`])'Count matching items instead of printing them
--output json | yaml | yaml-stream | text | table | offChoose the format per command; AWS_DEFAULT_OUTPUT and the profile's output set the default
Source: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output-format.html
--filters "Name=status,Values=attached"Server-side filter on EC2 describe calls, applied before --query
--exclude and --include apply in the order you write them, so the last matching rule wins.
aws s3 ls s3://amzn-s3-demo-bucket/example/List objects under a prefix; with no target it lists your buckets
aws s3 cp filename.txt s3://amzn-s3-demo-bucketUpload one file; swap source and target to download
aws s3 cp - s3://amzn-s3-demo-bucket/filename.txtUpload from stdin; use - as the target to stream to stdout
aws s3 sync . s3://amzn-s3-demo-bucket/path --deleteCopy new or changed files and remove target objects that no longer exist locally
aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt" --include "MyFile*.txt"Combine exclude and include filters; order matters
aws s3 rm s3://amzn-s3-demo-bucket/path --recursiveDelete every object under a prefix
aws s3 rb s3://amzn-s3-demo-bucket --forceDelete all objects, then the bucket; fails on versioned buckets with retained versions
aws s3api head-bucket --bucket my-bucket --output offCheck that a bucket exists using only the exit code
logs tail defaults to the last ten minutes across all streams in the group. --since takes one unit only: 5m works, 5h30m does not.
aws logs tail /aws/lambda/my-fn --followStream new log events until you press Ctrl-C
aws logs tail my-group --since 1h --format shortShow the last hour with a shortened timestamp
aws logs tail my-group --filter-pattern ERROROnly events matching a CloudWatch filter pattern
aws logs tail my-group --log-stream-name-prefix webLimit to streams whose names start with a prefix
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Placement.AvailabilityZone, State.Name, InstanceId]' --output textOne line per instance with zone, state and ID, ready for grep or awk
Source: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output-format.html
aws ec2 describe-images --owners self --query 'reverse(sort_by(Images,&CreationDate))[:5].{id:ImageId,date:CreationDate}'Your five newest AMIs, newest first
Source: https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-filter.html
Worked example: Find large attached EBS volumes in one zone
You want the attached volumes in us-west-2a that are bigger than 50 GiB, with readable column names. Filter on the server, then shape on the client:
aws sts get-caller-identity --profile my-dev-profile
aws ec2 describe-volumes --profile my-dev-profile \
--filters "Name=availability-zone,Values=us-west-2a" "Name=status,Values=attached" \
--query 'Volumes[?Size > `50`].{Id:VolumeId,Size:Size,Type:VolumeType}' \
--output table
I couldn't run this here, so here is what to look for rather than captured output. The first command should print the account ID and an ARN containing the role you picked in aws configure sso; if the account is wrong, stop. The second prints a table with Id, Size and Type columns. The --filters part runs inside EC2, so only attached volumes in that zone come back over the wire. The --query part then drops anything 50 GiB or smaller and renames the fields.
If the table is empty, remove the --query and check the raw JSON: the size field is Size with a capital S, and JMESPath is case sensitive. If you see an ExpiredToken style error, run aws sso login --profile my-dev-profile again.
Common mistakes
Using
--output textwith a--querythat ends in[0]and getting several results.Text output runs the query on each page. Switch to
--output json, or pipe tohead -1, as the filtering guide suggests.Writing JMESPath string literals in double quotes inside a double-quoted shell argument.
Wrap the whole expression in single quotes and put literals in backticks, as in
[?State==attached], or follow the quoting guide for your shell.Running
aws s3 sync --deletein the wrong direction.The target is the second argument and is the side that loses files. Do a run without
--deletefirst and read the planned operations.Keeping long-lived access keys in
~/.aws/credentialsfor daily work.Use
aws configure ssowith ansso-session; tokens refresh automatically and expire on their own.Using
--queryalone to cut down a huge EC2 listing.--queryruns after the whole response arrives. Add server-side--filtersfirst so the service sends less data.
Related cheat sheets
Sources
- https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html
- https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-filter.html
- https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-output-format.html
- https://docs.aws.amazon.com/cli/latest/userguide/cli-services-s3-commands.html
- https://docs.aws.amazon.com/cli/latest/reference/logs/tail.html
- https://github.com/aws/aws-cli/tags
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.