Cloud Computing

AWS CLI: 12 Powerful Commands Every Cloud Engineer Must Master in 2024

Think of the AWS CLI as your Swiss Army knife for cloud operations—compact, precise, and infinitely scalable. Whether you’re spinning up EC2 instances at 3 a.m. or auditing IAM policies across 50 accounts, this command-line interface transforms complex AWS console workflows into repeatable, scriptable, and auditable actions. Let’s cut through the noise and get you fluent—not just functional.

What Is AWS CLI—and Why It’s Non-Negotiable in Modern Cloud Infrastructure

The AWS CLI (Amazon Web Services Command Line Interface) is an open-source, Python-based tool that enables developers, DevOps engineers, and cloud architects to interact with over 300 AWS services programmatically. Unlike the AWS Management Console—a GUI built for discovery—the AWS CLI is engineered for precision, automation, and integration. It’s not merely a convenience; it’s the foundational layer for infrastructure-as-code (IaC) pipelines, CI/CD orchestration, and enterprise-scale governance.

Core Architecture: How AWS CLI Bridges Local Terminals to AWS Cloud

At its core, the AWS CLI operates as a thin client that translates human-readable commands into structured HTTP requests using AWS’s REST APIs. Every command—whether aws s3 ls or aws ec2 describe-instances—is mapped to a specific service action, authenticated via AWS Identity and Access Management (IAM) credentials, and signed using AWS Signature Version 4. This architecture ensures deterministic behavior, full traceability (via CloudTrail), and seamless compatibility with AWS SDKs.

Version Evolution: v1 vs. v2—Why You Must Upgrade Now

AWS CLI v2 (released in 2019) introduced critical enhancements over v1: built-in auto-paging, improved credential resolution (including SSO and Web Identity Federation), native support for AWS IAM Identity Center, and a bundled installer that eliminates Python dependency conflicts. As of AWS’s official deprecation timeline, CLI v1 reached end-of-support on July 1, 2024. Organizations still running v1 face unpatched security vulnerabilities, missing service integrations (e.g., Amazon Q, AWS HealthScribe), and zero compatibility with AWS IAM Identity Center’s role-based access workflows.

Security by Design: Credential Isolation and Least-Privilege Enforcement

The AWS CLI enforces security best practices by design. It supports multiple credential sources—including ~/.aws/credentials, environment variables (AWS_ACCESS_KEY_ID), ECS task roles, and EC2 instance profiles—with strict precedence rules. Crucially, it never stores plaintext secrets in command history or logs. When combined with AWS IAM roles and temporary credentials (via aws sts assume-role), the AWS CLI becomes a cornerstone of zero-trust infrastructure—ensuring every command executes with the minimal required permissions, auditable down to the millisecond.

Installation & Configuration: From Zero to Production-Ready in Under 5 Minutes

Getting the AWS CLI operational is intentionally frictionless—but misconfiguration remains the #1 cause of failed automation. This section walks through platform-specific installation, credential hygiene, and configuration that scales across teams and environments.

Platform-Specific Installation: macOS, Linux, and Windows (WSL2)macOS (Intel/Apple Silicon): Use brew install awscli—the Homebrew formula auto-updates and respects Apple Silicon architecture.Avoid pip install awscli unless you manage Python virtual environments explicitly.Linux (Debian/Ubuntu/CentOS): Prefer the bundled installer: curl “https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip” -o “awscliv2.zip” && unzip awscliv2.zip && sudo ./aws/install.This avoids Python package conflicts and ensures binary compatibility.Windows (Native or WSL2): For native Windows, use the MSI installer from AWS CLI official documentation..

For WSL2, install via sudo apt install awscli—but verify Python version compatibility (v3.8+ required).Secure Credential Management: Profiles, Roles, and SSONever hardcode credentials.Instead, leverage AWS CLI profiles: aws configure –profile dev creates an isolated credential set in ~/.aws/credentials.For cross-account access, use aws sts assume-role to generate temporary credentials, then export them to a new profile:.

aws sts assume-role –role-arn “arn:aws:iam::123456789012:role/PowerUser” –role-session-name “cli-session” –duration-seconds 3600 > /tmp/role-creds.json && export AWS_ACCESS_KEY_ID=$(jq -r ‘.Credentials.AccessKeyId’ /tmp/role-creds.json) && export AWS_SECRET_ACCESS_KEY=$(jq -r ‘.Credentials.SecretAccessKey’ /tmp/role-creds.json) && export AWS_SESSION_TOKEN=$(jq -r ‘.Credentials.SessionToken’ /tmp/role-creds.json)

For enterprise SSO, configure aws configure sso—this integrates with your organization’s IdP (e.g., Okta, Azure AD) and caches short-lived tokens automatically.

Advanced Configuration: Output Formats, Pagination, and Region Defaults

Configure globally via ~/.aws/config:

  • output = json (default), text, or table—use table for human readability, json for scripting.
  • region = us-east-1 sets default region—critical for services like S3 (which requires explicit region for cross-region bucket access).
  • cli_pager = disables automatic less paging for CI/CD pipelines.
  • max_attempts = 10 and retry_mode = adaptive improve resilience against transient API failures.

Core AWS CLI Commands: The 12 Most Impactful Commands Explained

Forget memorizing 1,200+ commands. Master these 12—and you’ll automate 90% of daily cloud operations. Each includes real-world context, common pitfalls, and production-grade examples.

1. aws sts get-caller-identity: Your Identity Audit Command

This is your first command—always. It validates *who* you are, *which* account you’re in, and *what* permissions are active:

aws sts get-caller-identity –output table

Output reveals Account, Arn, and UserId. If Arn shows assumed-role, you’re using temporary credentials—essential for security audits. Pair with --query for filtering: --query 'Account' returns only the account ID for scripting.

2. aws configure list: Debug Credential Conflicts Instantly

When commands fail mysteriously, run aws configure list. It displays *all* credential sources in precedence order: environment variables, shared config, instance profile, etc. If access_key shows None but profile shows dev, you’re missing credentials for that profile—no guesswork needed.

3. aws s3 ls & cp: The Backbone of Data Operations

  • aws s3 ls s3://my-bucket/ --recursive --human-readable: Lists all objects with human-readable sizes.
  • aws s3 cp ./local-file.txt s3://my-bucket/ --sse aws:kms --sse-kms-key-id alias/my-key: Encrypts uploads client-side using KMS.
  • aws s3 sync ./local-dir s3://my-bucket/ --delete --exclude "*.tmp": Syncs with deletion and exclusion—critical for static site deployments.

Pro tip: Use --dryrun before sync to preview changes—prevents accidental deletions in production buckets.

4. aws ec2 describe-instances: Infrastructure Discovery at Scale

Replace console scrolling with targeted queries:

aws ec2 describe-instances –filters “Name=instance-state-name,Values=running” “Name=tag:Environment,Values=prod” –query ‘Reservations[*].Instances[*].[InstanceId,InstanceType,LaunchTime,Tags[?Key==`Name`].Value|[0]]’ –output table

This returns only running production instances with their IDs, types, launch time, and Name tags—no parsing required. Add --region us-west-2 to target specific regions.

5. aws cloudformation describe-stacks: IaC Health Monitoring

Monitor CloudFormation stacks across environments:

  • aws cloudformation describe-stacks --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE: Lists healthy stacks only.
  • aws cloudformation list-stack-resources --stack-name my-app --query 'StackResourceSummaries[?ResourceStatus==`CREATE_FAILED`].[LogicalResourceId,ResourceStatusReason]' --output table: Pinpoints failed resources and root causes.

6. aws iam list-users & simulate-policy: Proactive Permission Validation

Before granting access, simulate it:

aws iam simulate-principal-policy –policy-source-arn arn:aws:iam::123456789012:user/jane –action-names s3:GetObject s3:ListBucket –resource-arns arn:aws:s3:::my-bucket arn:aws:s3:::my-bucket/*

Returns allowed or explicitDeny—no need to test in production. Combine with aws iam list-users --query 'Users[?CreateDate<`2024-01-01`].[UserName,CreateDate]' --output table to find stale accounts.

7. aws logs filter-log-events: Real-Time Log Triage

Search CloudWatch Logs without Kibana:

  • aws logs filter-log-events --log-group-name "/aws/lambda/my-function" --start-time $(date -d '1 hour ago' +%s000) --filter-pattern "ERROR" --query 'events[*].[timestamp,message]' --output table
  • Use --next-token for pagination across millions of events.

8. aws rds describe-db-instances: Database Operational Intelligence

Get DB health in one command:

aws rds describe-db-instances –query ‘DBInstances[?DBInstanceStatus==`available`].[DBInstanceIdentifier,DBInstanceClass,AllocatedStorage,EngineVersion,BackupRetentionPeriod]’ –output table

Add --filters Name=db-instance-class,Values=db.t3.medium to audit cost-optimized instances.

9. aws lambda list-functions & invoke: Serverless Debugging

Invoke functions synchronously for testing:

  • aws lambda invoke --function-name my-api --payload '{"path":"/health"}' --log-type Tail /dev/stdout 2>&1 | grep "LogResult" | cut -d'"' -f4 | base64 -d: Captures CloudWatch logs inline.
  • aws lambda list-functions --query 'Functions[?Runtime==`python3.12`].[FunctionName,LastModified]' --output table: Finds functions needing runtime updates.

10. aws secretsmanager get-secret-value: Secure Secret Retrieval

Never store secrets in code. Retrieve and inject securely:

aws secretsmanager get-secret-value –secret-id prod/database/password –query ‘SecretString’ –output text | jq -r ‘.password’

Works with rotation—AWS Secrets Manager auto-updates the secret, and AWS CLI fetches the latest version. Use --version-stage AWSCURRENT for explicit version control.

11. aws autoscaling describe-auto-scaling-groups: Scaling Policy Auditing

Validate auto-scaling health:

  • aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[?DesiredCapacity>0].[AutoScalingGroupName,MinSize,MaxSize,DesiredCapacity,HealthCheckType]' --output table
  • Add --filters Name=tag:Team,Values=backend to scope by ownership.

12. aws resourcegroupstaggingapi get-resources: Cross-Service Tag Governance

Find *all* tagged resources—even across services:

aws resourcegroupstaggingapi get-resources –tag-filters Key=Environment,Values=prod –resources-per-page 50 –query ‘ResourceTagMappingList[*].[ResourceARN,Tags[?Key==`Owner`].Value|[0]]’ –output table

This is indispensable for cost allocation (via Owner tag), compliance (e.g., PCI-DSS), and decommissioning unused resources.

Advanced AWS CLI Workflows: Automation, Scripting & CI/CD Integration

The true power of AWS CLI emerges when embedded in repeatable workflows—not just ad-hoc commands.

Shell Scripting Patterns: Idempotent, Safe, and Idiomatic

Every production script must include:

  • Error handling: set -euo pipefail ensures immediate exit on any failure.
  • Region validation: aws ec2 describe-regions --region us-east-1 > /dev/null || { echo "Invalid region"; exit 1; }
  • Idempotency: Use aws s3 ls s3://my-bucket/ && echo "exists" || echo "missing" instead of mkdir-style assumptions.

Example: A safe S3 bucket creation script with versioning and encryption:

#!/bin/bash
BUCKET_NAME=”my-secure-bucket-$(date +%s)”
aws s3api create-bucket –bucket $BUCKET_NAME –region us-east-1 –create-bucket-configuration LocationConstraint=us-east-1
aws s3api put-bucket-versioning –bucket $BUCKET_NAME –versioning-configuration Status=Enabled
aws s3api put-bucket-encryption –bucket $BUCKET_NAME –server-side-encryption-configuration ‘{“Rules”:[{“ApplyServerSideEncryptionByDefault”:{“SSEAlgorithm”:”AES256″}}]}’

CI/CD Integration: GitHub Actions, GitLab CI, and Jenkins

In GitHub Actions, use the official aws-actions/configure-aws-credentials action:

  • It injects temporary credentials via OIDC—no long-term keys in secrets.
  • Supports role assumption: role-to-assume: arn:aws:iam::123456789012:role/GitHubActions.
  • Auto-configures AWS_REGION and AWS_DEFAULT_REGION.

For Jenkins, use the AWS Credentials Plugin and invoke CLI via sh 'aws s3 sync ./dist s3://my-bucket/ --delete'—always wrap in try/catch blocks for failure visibility.

JSON Querying Mastery with –query and JMESPath

JMESPath is the query language behind --query. Master these patterns:

  • --query 'Reservations[*].Instances[*].{ID:InstanceId,Type:InstanceType,State:State.Name}': Projects custom objects.
  • --query 'sort_by(Reservations[*].Instances[*], &LaunchTime)[-1].InstanceId': Gets most recently launched instance.
  • --query 'length(Reservations[*].Instances[?State.Name==`running`])': Counts running instances.

Test queries interactively at jmespath.org before deploying.

Troubleshooting AWS CLI: Diagnosing Failures Like a Senior Engineer

When AWS CLI fails, it’s rarely the tool—it’s the environment, credentials, or permissions. Here’s how to diagnose systematically.

Debug Mode: aws –debug and CloudTrail Correlation

Run aws --debug s3 ls s3://my-bucket to see full HTTP request/response, including headers, signing process, and error payloads. Cross-reference the x-amzn-requestid header with CloudTrail logs to trace the exact API call, source IP, and IAM principal.

Credential Resolution Debugging: aws configure list –debug

This extended debug mode shows *every* credential source checked, in order, and why each was rejected or accepted. If profile shows None, the profile doesn’t exist in ~/.aws/credentials. If credential_source shows Ec2InstanceMetadata, you’re on an EC2 instance—and credentials are fetched from IMDSv2.

Common Error Decoding: From ‘NoSuchBucket’ to ‘AccessDenied’

  • NoSuchBucket: Bucket doesn’t exist *or* you’re in the wrong region (S3 buckets are globally unique but region-scoped).
  • AccessDenied: Check IAM policy and bucket policy—S3 bucket policies override IAM policies.
  • InvalidSignatureException: System clock skew >15 minutes—run sudo ntpdate -s time.nist.gov.
  • ExpiredToken: Temporary credentials expired—re-run aws sts assume-role or refresh SSO session.

Security Hardening: Beyond Basic Configuration

Hardening the AWS CLI is non-optional in regulated environments (HIPAA, SOC 2, GDPR).

Enforcing MFA for Sensitive Operations

Require MFA for destructive actions by configuring IAM policies with aws:MultiFactorAuthAge:

{
“Version”: “2012-10-17”,
“Statement”: [
{
“Effect”: “Deny”,
“Action”: [
“ec2:TerminateInstances”,
“rds:DeleteDBInstance”
],
“Resource”: “*”,
“Condition”: {
“NumericLessThan”: {
“aws:MultiFactorAuthAge”: “300”
}
}
}
]
}

Then, use aws sts get-session-token --serial-number arn:aws:iam::123456789012:mfa/jane --token-code 123456 to obtain MFA-authenticated credentials.

Logging & Auditing: CLI Command History and CloudTrail

Enable CloudTrail in all regions and configure it to log aws:cli user agents. Use Athena to query:

SELECT eventname, useridentity.arn, sourceipaddress, eventtime, requestparameters
FROM cloudtrail_logs
WHERE useridentity.sessioncontext.sessionissuer.type = ‘Role’
AND eventname IN (‘DeleteBucket’, ‘TerminateInstances’, ‘DeleteDBInstance’)
ORDER BY eventtime DESC
LIMIT 50

Also, enable shell history logging: export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$(whoami) [$$] $(history 1 | sed "s/^[ ]*[0-9]*[ ]*//") [$RETRN_VAL]"'.

Least-Privilege Role Assumption with Session Policies

When assuming a role, attach a session policy to further restrict permissions:

aws sts assume-role –role-arn arn:aws:iam::123456789012:role/PowerUser –role-session-name cli-session –policy-arn arn:aws:iam::123456789012:policy/ReadOnlyS3Only

This ensures even if the target role has broad permissions, the CLI session is constrained to read-only S3 access.

Future-Proofing Your AWS CLI Skills: Integrations & Emerging Patterns

The AWS CLI isn’t static—it’s evolving alongside AWS’s AI, observability, and security innovations.

AI-Powered CLI: aws cli v2 + Amazon Q Integration

With Amazon Q, the AWS CLI now supports natural-language queries:

  • aws q "show me all EC2 instances with high CPU in the last 24 hours"
  • aws q "generate a CloudFormation template for a secure VPC with public/private subnets"

Amazon Q interprets intent, queries CloudWatch and Config, and generates CLI commands or templates—reducing cognitive load and accelerating incident response.

Observability Integration: CLI + AWS Distro for OpenTelemetry

Use aws observability list-services (new in CLI v2.13+) to discover OpenTelemetry-collected services. Export traces to CloudWatch ServiceLens with:

aws observability get-trace –trace-id 1-63a2f1a2-1234567890abcdef12345678 –output json > trace.json

This bridges CLI workflows with distributed tracing—critical for debugging microservices.

Security Automation: CLI + AWS Security Hub & Inspector

Automate security validation:

  • aws securityhub get-findings --filters '{"SeverityLabel":[{"Value":"CRITICAL","Comparison":"EQUALS"}]}' --query 'Findings[*].[Title,Resources[0].Id,Severity.Label]' --output table
  • aws inspector2 list-findings --filter-criteria '{"severity":[{"comparison":"EQUALS","value":"CRITICAL"}]}' --query 'findings[*].[title,severity,firstObservedAt]' --output table

Integrate with PagerDuty or Slack via AWS EventBridge Pipes—turning CLI output into real-time alerts.

Frequently Asked Questions (FAQ)

What’s the difference between AWS CLI and AWS SDKs?

The AWS CLI is a command-line tool built on top of AWS SDKs (specifically, the Python SDK—boto3). SDKs are libraries for embedding AWS functionality into applications (e.g., Python, Java, JavaScript), while the AWS CLI is optimized for human interaction, scripting, and automation. You use the CLI for infrastructure management; you use SDKs for application logic.

Can I use AWS CLI without an internet connection?

No. The AWS CLI requires internet connectivity to authenticate with AWS IAM and call service APIs. However, it supports offline *scripting*—you can write and test shell scripts locally, then execute them in connected environments. For air-gapped environments, use AWS Systems Manager Session Manager or on-premises AWS PrivateLink.

How do I update AWS CLI to the latest version?

For CLI v2: aws --version to check current version, then run curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip && sudo ./aws/install --update. On macOS with Homebrew: brew update && brew upgrade awscli. Always verify with aws --version post-update.

Is AWS CLI free to use?

Yes—the AWS CLI itself is open-source and free. However, every command you run incurs standard AWS service charges (e.g., S3 GET requests, CloudWatch Logs ingestion, EC2 instance hours). The CLI is a *client*, not a service—so there are no CLI-specific fees.

How can I run AWS CLI commands in parallel for faster execution?

Use GNU Parallel: echo us-east-1 us-west-2 us-gov-west-1 | parallel -j 3 'aws ec2 describe-regions --region {} --query "Regions[?RegionName=="{}"].RegionName" --output text'. Or use aws --profile dev --region us-east-1 ec2 describe-instances & with background jobs—but add wait for synchronization. For production, prefer AWS Systems Manager Automation for cross-region orchestration.

In closing, the AWS CLI is far more than a command-line tool—it’s the operational nervous system of modern cloud infrastructure. From foundational installation and secure credentialing to AI-augmented troubleshooting and cross-service governance, mastering these 12 commands and their underlying patterns transforms you from a cloud user into a cloud architect. Every line of CLI code you write is infrastructure documented, auditable, and repeatable. So go ahead—open your terminal, run aws sts get-caller-identity, and start building with intention, not intuition.


Further Reading:

Back to top button