Practical AWS CLI Examples for Cloud Management
The AWS Command Line Interface (AWS CLI) is a foundational tool for developers, engineers, and operations teams who want to manage AWS resources efficiently. With AWS CLI, you can script routine tasks, integrate cloud workflows into CI/CD pipelines, and retrieve actionable insights from your AWS account without switching to the console. This article provides real-world AWS CLI examples that cover common use cases, best practices, and how to structure commands for reliable, repeatable work.
Getting started with the AWS CLI
Before you run commands, install the AWS CLI and configure your credentials. The AWS CLI has two major versions, with AWS CLI v2 offering improved authentication flows and cross-platform support. After installation, run:
aws --version
aws configure
During configuration, you provide your access key, secret key, default region, and output format. A typical setup looks like this:
AWS Access Key ID [None]: AKIA...EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7...
Default region name [None]: us-west-2
Default output format [None]: json
To keep credentials secure and easily switch environments, you can use named profiles:
aws configure --profile staging
aws configure --profile production
Then you select the profile in your commands or environment:
aws s3 ls --profile production
AWS_PROFILE=staging aws s3 ls
Core AWS CLI patterns that boost productivity
- Use queries to shape output and extract fields with the
--queryoption. - Choose a consistent output format (json, text, or table) with
--output. - Leverage filters to narrow results, such as
Name=instance-state-name,Values=runningfor EC2. - Enable pagination when listing large collections with
--max-itemsand--page-sizeor simply--no-paginatewhen you need a single page. - Keep your operations idempotent and idempotency friendly by using stable identifiers and dry-run ideas when available.
Amazon S3: common storage tasks with the CLI
Amazon S3 is a frequent target for automation. The AWS CLI provides straightforward commands to manage buckets and objects. Below are representative workflows you can adapt.
List all buckets
aws s3 ls
Create a bucket in a specific region
aws s3 mb s3://my-unique-bucket-name --region us-east-1
List objects in a bucket
aws s3 ls s3://my-unique-bucket-name
Copy a file into a bucket
aws s3 cp ./reports/summary.csv s3://my-unique-bucket-name/reports/summary.csv
Synchronize a local folder with a bucket
aws s3 sync ./local-dir s3://my-unique-bucket-name/backup
Delete an object and manage exclusions
aws s3 rm s3://my-unique-bucket-name/reports/old-report.csv
Tips: use --exclude and --include to fine-tune transfers. For large transfers, consider multipart uploads and monitoring transfer progress.
EC2: managing compute instances with the AWS CLI
EC2 commands enable fast instance management, image creation, and network changes without touching the console.
Describe running instances
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --region us-west-2
Start, stop, and reboot instances
aws ec2 start-instances --instance-ids i-0123456789abcdef0
aws ec2 stop-instances --instance-ids i-0123456789abcdef0
aws ec2 reboot-instances --instance-ids i-0123456789abcdef0
Create an AMI from an instance
aws ec2 create-image --instance-id i-0123456789abcdef0 --name "ProdServer-AMI" --no-reboot
Allocate and associate an Elastic IP
aws ec2 allocate-address
aws ec2 associate-address --instance-id i-0123456789abcdef0 --allocation-id eipalloc-12345678
Useful patterns include filtering results with --filters and extracting identifiers with --query to feed into subsequent commands.
IAM: user and permission management via the CLI
Automating identity and access management reduces errors and ensures consistent permissions across environments. The AWS CLI covers typical IAM tasks.
List users
aws iam list-users --output json
Create a new user and attach a policy
aws iam create-user --user-name dev-team-member
aws iam attach-user-policy --user-name dev-team-member --policy-arn arn:aws:iam::aws:policy/IAMReadOnlyAccess
Create and manage access keys for a user
aws iam create-access-key --user-name dev-team-member
Tip: avoid embedding long-lived credentials in scripts; prefer roles and temporary credentials where feasible, and use MFA where appropriate.
CloudFormation: infrastructure as code tooling with the CLI
CloudFormation stacks can be managed from the command line to deploy, update, validate, and monitor infrastructure described in templates.
Validate a template
aws cloudformation validate-template --template-body file://template.yaml
Package and deploy a stack
aws cloudformation package --template-file template.yaml --s3-bucket my-cf-packages --output-template-file packaged.yaml
aws cloudformation deploy --template-file packaged.yaml --stack-name prod-app --parameter-overrides Env=prod
Use describe-stacks to monitor progress and status, and list-stacks to audit existing stacks.
RDS: database services and automation
RDS commands help you inspect instances, manage snapshots, and automate routine maintenance tasks.
aws rds describe-db-instances
aws rds stop-db-instance --db-instance-identifier mydb
aws rds start-db-instance --db-instance-identifier mydb
Snapshot and backup considerations
aws rds create-db-snapshot --db-instance-identifier mydb --db-snapshot-identifier mydb-snapshot-001
Lambda: serverless functions from the CLI
Interacting with Lambda from the CLI enables quick testing and orchestration alongside other AWS services.
aws lambda list-functions --region us-east-1
aws lambda invoke --function-name myFunction --payload '{"key1":"value1"}' response.json
When invoking, you can specify an output file for the response and use --log-type Tail to retrieve logs for debugging.
Best practices for reliable AWS CLI workflows
- Prefer consistent naming and naming conventions across resources to ease scripting and auditing.
- Use
--queryto return only the fields you need, reducing noise and speeding up pipelines. - Adopt
--output jsonfor machine readability ortextfor easier human scanning in scripts. - Leverage profiles and region overrides to prevent accidental cross-region changes, and document the intended region in your script header.
- Automate common tasks with simple shell scripts or Makefiles to reduce manual steps and improve reproducibility.
Troubleshooting common AWS CLI issues
As with any automation tool, issues can arise. Common scenarios include permission errors, region mismatches, and stale credentials. Quick checks:
- Verify credentials and profile:
aws sts get-caller-identity --profile production - Confirm the region in use:
aws configure get region --profile productionorAWS_DEFAULT_REGIONin the environment. - Review IAM policies and trust relationships to ensure the CLI user or role has the necessary permissions for the requested actions.
- Enable verbose output for troubleshooting: add
--debugto commands to inspect HTTP requests and responses.
Continuing education and staying current
The AWS CLI evolves with new services and features. To stay current, monitor the official AWS CLI release notes, participate in community forums, and test changes in a dedicated staging environment before implementing them in production. Practicing with real-world scenarios—like automating backups, scaling decisions, or rotating credentials—helps cement best practices and keeps your cloud operations resilient.
Conclusion
The AWS CLI enables practical, scalable cloud management for teams of all sizes. By combining simple commands, thoughtful scripting, and consistent patterns, you can reduce manual toil, accelerate deployments, and improve observability across your AWS environment. Whether you are a developer, a sysadmin, or a cloud architect, integrating the AWS CLI into your daily workflow will unlock faster, repeatable, and audit-friendly cloud operations.