Sub topic 7
Anatomy of an AWS CLI command
This chapter assumes you have installed AWS CLI v2 and set up at least one profile with credentials and a default Region. The previous chapters covered both. You should also be comfortable reading JSON.
The basic pattern
Every AWS CLI command follows the same shape:
aws <service> <operation> [options]awsis the program.- The service is the AWS service to call, such as
ec2,s3api, orsts. - The operation is the action to perform. Operation names are lowercase and hyphenated, such as
describe-instancesorget-caller-identity. - Options modify the call. Some are specific to the operation, and others are global.
aws sts get-caller-identity --profile dev --region eu-west-1 --output jsonHere sts is the service and get-caller-identity is the operation. The three options are all global, so they work on any command.
<br>
Global options you will use constantly
| Option | Effect |
|---|---|
| `--profile` | Chooses which named profile supplies credentials and defaults |
| `--region` | Sends the call to a specific Region |
| `--output` | Sets the format: `json`, `text`, `table`, or `yaml` |
| `--query` | Filters the result on your machine with JMESPath |
| `--debug` | Prints request details, useful when a call fails |
Global options override configuration. For the Region, the CLI checks the --region flag first, then the AWS_REGION or AWS_DEFAULT_REGION environment variables, then the profile's region setting. The profile follows a similar order: --profile, then AWS_PROFILE, then default.
Built-in help
You do not need the web documentation to find an operation. Add help at any level:
aws help
aws ec2 help
aws ec2 describe-instances helpThe first lists services. The second lists the operations of one service. The third shows a full description, every option, and examples. The help pages open in a pager, so press q to exit.
Passing option values
Operation options take plain values, lists separated by spaces, shorthand syntax, or JSON:
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 i-0fedcba9876543210
aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running"
aws ec2 describe-instances \
--filters '[{"Name":"instance-state-name","Values":["running"]}]'The second and third commands are equivalent. To build a complex request, run aws ec2 run-instances --generate-cli-skeleton. It prints a JSON template that you can fill in and pass back with --cli-input-json file://request.json.
Two commands for S3
S3 has two command sets. aws s3 offers high-level commands such as ls, cp, and sync. aws s3api maps one-to-one to the S3 API operations, such as list-buckets and get-object. Use s3 for everyday file work and s3api when you need an option the high-level commands do not expose.
Common mistakes
- Using the wrong Region. Many commands return an empty list instead of an error when the resources live elsewhere.
- Forgetting
--profileand getting the default identity. Check withaws sts get-caller-identity. - Writing the operation in camelCase. The CLI expects
describe-instances, notDescribeInstances. - Misquoting JSON. In a Bash shell, wrap JSON in single quotes.
Exercise
You run aws ec2 describe-vpcs and get an empty list, but you know a VPC exists in eu-west-1. Your profile's default Region is us-east-1. Give two ways to make the command target eu-west-1.
Show answer
Add the global option to the command: aws ec2 describe-vpcs --region eu-west-1. Alternatively, set the AWS_REGION environment variable to eu-west-1, or change the region value in the profile. The --region flag takes precedence over the environment variable and the profile setting.