Skip to content

Menu

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 sso

Wizard that asks for a session name, start URL, SSO region and scopes, then lets you pick an account and role

From the docs, not run by us
aws configure sso --use-device-code

Same wizard with device authorization, for a machine that cannot open a browser

From the docs, not run by us
aws configure sso-session

Create or edit only the [sso-session] block in ~/.aws/config

From the docs, not run by us
aws sso login --profile my-dev-profile

Open the browser sign-in and cache a token for that profile

From the docs, not run by us
aws sso login --sso-session my-sso

Sign in once for a session that several profiles share

From the docs, not run by us
aws sso logout

Delete cached Identity Center credentials for all profiles

From the docs, not run by us
aws sts get-caller-identity --profile my-dev-profile

Show the account, user ID and ARN the profile resolves to

From the docs, not run by us

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

From the docs, not run by us
--query 'Volumes[?Size < `20`].VolumeId'

Filter a list by a numeric field; literals go in backticks

From the docs, not run by us
--query 'Volumes[].{ID:VolumeId,Type:VolumeType}'

Multiselect hash: pick fields and rename them

From the docs, not run by us
--query 'sort_by(Images, &CreationDate)[-1].ImageId'

Sort with a function and take the newest item

From the docs, not run by us
--query 'length(Volumes[?Iops > `1000`])'

Count matching items instead of printing them

From the docs, not run by us
--output json | yaml | yaml-stream | text | table | off

Choose 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

From the docs, not run by us
--filters "Name=status,Values=attached"

Server-side filter on EC2 describe calls, applied before --query

From the docs, not run by us

--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

From the docs, not run by us
aws s3 cp filename.txt s3://amzn-s3-demo-bucket

Upload one file; swap source and target to download

From the docs, not run by us
aws s3 cp - s3://amzn-s3-demo-bucket/filename.txt

Upload from stdin; use - as the target to stream to stdout

From the docs, not run by us
aws s3 sync . s3://amzn-s3-demo-bucket/path --delete

Copy new or changed files and remove target objects that no longer exist locally

From the docs, not run by us
aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt" --include "MyFile*.txt"

Combine exclude and include filters; order matters

From the docs, not run by us
aws s3 rm s3://amzn-s3-demo-bucket/path --recursive

Delete every object under a prefix

From the docs, not run by us
aws s3 rb s3://amzn-s3-demo-bucket --force

Delete all objects, then the bucket; fails on versioned buckets with retained versions

From the docs, not run by us
aws s3api head-bucket --bucket my-bucket --output off

Check that a bucket exists using only the exit code

From the docs, not run by us

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 --follow

Stream new log events until you press Ctrl-C

From the docs, not run by us
aws logs tail my-group --since 1h --format short

Show the last hour with a shortened timestamp

From the docs, not run by us
aws logs tail my-group --filter-pattern ERROR

Only events matching a CloudWatch filter pattern

From the docs, not run by us
aws logs tail my-group --log-stream-name-prefix web

Limit to streams whose names start with a prefix

From the docs, not run by us
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Placement.AvailabilityZone, State.Name, InstanceId]' --output text

One 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

From the docs, not run by us
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

From the docs, not run by us

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 text with a --query that ends in [0] and getting several results.

    Text output runs the query on each page. Switch to --output json, or pipe to head -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 --delete in the wrong direction.

    The target is the second argument and is the side that loses files. Do a run without --delete first and read the planned operations.

  • Keeping long-lived access keys in ~/.aws/credentials for daily work.

    Use aws configure sso with an sso-session; tokens refresh automatically and expire on their own.

  • Using --query alone to cut down a huge EC2 listing.

    --query runs after the whole response arrives. Add server-side --filters first so the service sends less data.