Useful AWS CLI Commands

A practical collection of AWS CLI commands for authentication, ECR, CodeArtifact, S3, CloudWatch Logs and secrets — plus tips for filtering output with --query.

Bart Tworek4 min readawscliecrcodeartifacts3

The AWS Console is great for exploring, but for repetitive tasks the AWS CLI is faster, scriptable, and easy to drop into a CI pipeline. These are the commands I use most often. Throughout, replace placeholders like <your-region> and <your-account-id> with your own values.

Authentication and profiles

Sign in with IAM Identity Center (SSO)

If your organisation uses IAM Identity Center, configure a profile once and then sign in through the browser whenever your session expires:

aws configure sso                       # one-off: creates a named profile interactively
aws sso login --profile <profile-name>  # opens the browser to refresh your session

Work with multiple profiles

To list the profiles you have configured, and to use a specific one — either per command or for the rest of your shell session:

aws configure list-profiles
aws s3 ls --profile <profile-name>
export AWS_PROFILE=<profile-name>

On Windows PowerShell, use $env:AWS_PROFILE = "<profile-name>" instead of export.

Verify which identity the CLI is using

If a command fails with an unexpected AccessDenied error, the first thing to check is which account and role the CLI is actually authenticated as:

aws sts get-caller-identity

This returns the account ID, the ARN of the user or role, and a unique identifier for the credentials in use. It's especially useful when you have multiple profiles configured or environment variables overriding them — more than once, the "permissions problem" turns out to be the wrong account.

Amazon ECR (container registry)

Log in to ECR

To authenticate Docker against your Elastic Container Registry, retrieve a token and pipe it straight into docker login:

aws ecr get-login-password --region <your-region> \
  | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.<your-region>.amazonaws.com

The token is valid for 12 hours. Passing it via --password-stdin keeps it out of your shell history and the process list.

Create a repository

aws ecr create-repository --repository-name <repo-name> --region <your-region> \
  --image-scanning-configuration scanOnPush=true

scanOnPush=true scans every pushed image for known vulnerabilities automatically.

Tag and push an image

Once logged in, tag a local image with the full repository URI and push it:

docker tag <image-name>:<tag> <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/<repo-name>:<tag>
docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/<repo-name>:<tag>

List repositories and images

To see all container repositories in the current account and region:

aws ecr describe-repositories --region <your-region> \
  --query 'repositories[].repositoryName' --output table

To see the five most recently pushed images in a repository:

aws ecr describe-images --repository-name <repo-name> --region <your-region> \
  --query 'sort_by(imageDetails, &imagePushedAt)[-5:].[imageTags[0], imagePushedAt]' \
  --output table

AWS CodeArtifact (package registry)

Log in to CodeArtifact

To authenticate your package manager against a CodeArtifact repository:

aws codeartifact login --tool <your-package-manager> --repository <your-repo-name> \
  --domain <your-domain-name> --domain-owner <your-account-id> --region <your-region>

Supported values for --tool are npm, pip, twine, dotnet, nuget and swift. For .NET projects, use --tool dotnet — this adds (or updates) a NuGet source in your NuGet config, so dotnet restore picks up packages from CodeArtifact. Like the ECR token, the login lasts 12 hours by default.

Get a token for CI pipelines or other tools

Some tools — Gradle, Maven, Yarn, or a nuget.config checked into source control — can't use codeartifact login directly. Instead, fetch a raw authorisation token and pass it in through an environment variable:

export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token \
  --domain <your-domain-name> --domain-owner <your-account-id> --region <your-region> \
  --query authorizationToken --output text)

To get the repository URL to point the tool at:

aws codeartifact get-repository-endpoint --domain <your-domain-name> --domain-owner <your-account-id> \
  --repository <your-repo-name> --format nuget --query repositoryEndpoint --output text

Amazon S3

aws s3 ls s3://<bucket-name>/<prefix>/                # list objects under a prefix
aws s3 cp ./report.csv s3://<bucket-name>/reports/    # upload a single file
aws s3 cp s3://<bucket-name>/reports/report.csv .     # download a single file
aws s3 sync ./dist s3://<bucket-name> --delete        # mirror a local folder to a bucket

sync only uploads files that have changed, which makes it ideal for deploying a static site. --delete also removes objects from the bucket that no longer exist locally — run it with --dryrun first to see what it would do.

CloudWatch Logs

To stream a log group to your terminal — for example, a Lambda function's logs while you test it:

aws logs tail /aws/lambda/<function-name> --follow --since 15m

Add --filter-pattern "ERROR" to only show matching lines.

Secrets Manager and Parameter Store

To read a secret's value from Secrets Manager:

aws secretsmanager get-secret-value --secret-id <secret-name> \
  --query SecretString --output text

And to read an encrypted parameter from Systems Manager Parameter Store:

aws ssm get-parameter --name /<app>/<environment>/<parameter-name> --with-decryption \
  --query Parameter.Value --output text

Be careful with these in shared terminals or CI logs — they print the secret in plain text.

Tip: shape the output with --query and --output

Most AWS CLI commands return large JSON documents. Two global options make them far easier to work with:

For example, to list running EC2 instances with their name and private IP address:

aws ec2 describe-instances --filters Name=instance-state-name,Values=running \
  --query 'Reservations[].Instances[].[Tags[?Key==`Name`]|[0].Value, InstanceId, PrivateIpAddress]' \
  --output table