Working with the AWS CLI and AWS CloudShell
This lesson explains how the AWS Command Line Interface (AWS CLI) calls AWS APIs from a terminal, and how it is used throughout development, scripting and troubleshooting.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how profiles store settings and credentials.
- Use options such as --query, --output and --dry-run to filter output and check permissions.
- Explain what AWS CloudShell provides and whose permissions its commands use.
Profiles and credentials
The AWS Command Line Interface (AWS CLI) needs two kinds of information for every command: settings, such as the Region and output format, and credentials that identify the caller. Both are stored in profiles, named sets of settings kept in ~/.aws/config and, for long-term access keys, ~/.aws/credentials. A command uses the default profile unless another is selected with the --profile option or the AWS_PROFILE environment variable.
Long-term access keys stored in files are a security risk, so profiles should use temporary credentials where possible. aws configure sso creates a profile that signs in through IAM Identity Center, and aws sso login refreshes it. A profile can also name a role_arn and a source_profile, in which case the CLI assumes that role automatically before each command, which is how developers work in other accounts.
When the same setting is available from several places, the CLI and the SDKs apply a fixed order of precedence. Command-line options, such as --region, override environment variables, such as AWS_ACCESS_KEY_ID and AWS_REGION, which in turn override the profile in the config and credentials files. On AWS compute, such as EC2 instances and Lambda functions, credentials come from the attached IAM role when nothing else is set. Forgotten environment variables are a frequent cause of a command running as an unexpected identity. aws configure list shows where each value comes from, and aws sts get-caller-identity confirms which identity is actually in use.
Useful options
The AWS CLI provides options and features that control output, permissions checks and pagination. The following table describes the most useful of these.
| Option | What it does |
|---|---|
--query | Filters output with JMESPath |
--output | Chooses json, table, text or yaml |
--dry-run | Checks permissions for EC2 actions without making changes |
--max-items, --page-size, --starting-token | Control pagination |
Waiters, e.g. aws dynamodb wait table-exists | Poll until a resource reaches a state |
--debug | Prints the full request and response for troubleshooting |
Example. Filtering output with --query:
aws lambda list-functions --query "Functions[?Runtime=='python3.9'].FunctionName" --output text
aws ec2 describe-instances --profile prod --region eu-west-1 \
--filters Name=instance-state-name,Values=running \
--query "Reservations[].Instances[].[InstanceId,InstanceType]" --output table--dry-run answers DryRunOperation if you are allowed, or UnauthorizedOperation if not. --max-items with --starting-token pages through long results:
aws ec2 run-instances --dry-run --image-id ami-0abc1234 --instance-type t3.micro
aws s3api list-objects-v2 --bucket logs --max-items 100 --starting-token "$NEXT_TOKEN"AWS CloudShell
AWS CloudShell is a browser-based shell that opens from the AWS Management Console. It runs in an AWS-managed environment that is already signed in with the console user's credentials, so commands run with exactly the permissions of that user without any access keys. The AWS CLI, the SAM CLI, Python, Node.js, Git and other common tools are preinstalled.
Each user receives 1 GB of persistent storage per Region in the home directory, which keeps scripts and files between sessions; files elsewhere are lost when the session ends. CloudShell suits quick administrative tasks, trying commands while learning and running the hands-on labs in this course, but not long-running processes, because inactive sessions end after a period without interaction.
Answering a question with one command
A team lead asks: "Which of our Lambda functions still use Python 3.9, and how much memory do they have?" Rather than clicking through the console, the developer combines a query and an output format:
aws lambda list-functions --profile prod --region eu-west-1 \
--query "Functions[?Runtime=='python3.9'].[FunctionName,MemorySize]" --output tableThe CLI paginates automatically, so all functions are included even if there are hundreds. --profile selects the production credentials, --query (JMESPath) filters and selects fields on the client, and --output table makes it readable. For a risky command, --dry-run on EC2 checks permissions without making changes.
Which CLI option selects only the fields you need from a response?
Show answer
--query, which applies a JMESPath expression to the response on the client. --output then chooses the format, such as text or table.
Exam Tips
Remember for the exam
- To check whether you have permission without making the change (for EC2), use
--dry-run. - To filter CLI output to specific fields, use
--query. - To run CLI commands without installing or configuring anything, use CloudShell.
- To find out which credentials the CLI is using, run
aws configure listoraws sts get-caller-identity. - To wait in a script until a resource is ready, use a CLI waiter.