The aws command (AWS CLI) drives your AWS resources from the terminal. Two settings are easy to forget and change everything: the profile (the account and its credentials) and the region (where the command runs). With the wrong profile, a command acts on the wrong account; in the wrong region, an empty list does not mean that nothing exists.
This page works in two stages. The first part needs no AWS account: in a practice folder, you read the configuration, choose a profile, find out where each key comes from and understand the errors. The second part needs an account: sign-in, sts get-caller-identity, lists, filters. These notions also apply to OUTSCALE: its AWS-compatible APIs are driven with the same command, by changing the address it contacts (end of page).
What you will learn
- Install AWS CLI v2 and check its version
- Write a practice configuration with three profiles, without touching your real
configandcredentialsfiles - Predict which profile, region and keys a command will use
- Read
aws configure listand the exit codes 252, 253, 254 and 255 - Sign in to an account and check your identity with
aws sts get-caller-identity - List resources and filter the response with
--queryand--filters
Prerequisites
You can open a Linux or macOS terminal and run a command. The practice folder uses bash: if your terminal runs Zsh, you will start bash inside it. The second part needs an AWS account (a test account is enough); the first part needs none.
Install AWS CLI
AWS CLI v2 ships as a standalone package with its own Python interpreter: you install no dependency. The v1, installed with pip on older machines, is not covered. The latest v2 on 24 November 2026 is 2.42.3; the outputs of the first part were obtained with 2.32.17, and the commands used are the same in both.
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"unzip awscliv2.zipsudo ./aws/installOn an ARM processor, replace x86_64 with aarch64. If unzip is missing, install it with your distribution's package manager. AWS also publishes a .sig signature next to each archive, which you can verify with gpg (key A6310ACC4672475C).
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"sudo installer -pkg AWSCLIV2.pkg -target /msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msiThese three installers come from AWS. Third-party packages (Homebrew, winget) are not maintained by AWS: their version may lag behind.
Create the practice folder
aws reads its configuration from ~/.aws/config and ~/.aws/credentials. To learn without risk, you point it to other files, in a throwaway folder, with environment variables. They only apply in a subshell, a second shell started inside your terminal: as long as you stay in it, your real config and credentials files are neither read nor modified, and exit takes you back to your original terminal. Start bash on its own first, otherwise the rest of the block would run in your terminal.
mkdir -p ~/aws-lab && cd ~/aws-labbashIn this new shell, set the variables. AWS_PAGER= turns off the pager (less) on long outputs, AWS_EC2_METADATA_DISABLED=true stops aws from looking for credentials on the network of an EC2 instance, and the unset lines remove any AWS variables your terminal already carries.
export AWS_CONFIG_FILE=$PWD/config AWS_SHARED_CREDENTIALS_FILE=$PWD/credentialsexport AWS_PAGER= AWS_EC2_METADATA_DISABLED=trueunset AWS_PROFILE AWS_DEFAULT_PROFILE AWS_REGION AWS_DEFAULT_REGIONunset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_ENDPOINT_URLecho $AWS_CONFIG_FILEThe last line must end with aws-lab/config. If it does not, do not go on: you would still be in your original terminal. Now write the practice configuration, three profiles each with its own region, and an empty credentials file.
printf '[default]\nregion = eu-west-3\n\n[profile recette]\nregion = eu-west-1\n\n[profile prod]\nregion = us-east-1\n' > config: > credentialschmod 600 credentialsaws --versionaws-cli/2.32.17 Python/3.13.11 Linux/6.8.0-149-generic exe/x86_64.ubuntu.24On your machine, the version number and the system differ, and your home folder replaces /home/vous in the outputs. After an exit, the config and credentials files stay in ~/aws-lab: to resume, run cd ~/aws-lab again, then bash, then the export and unset lines.
The aws command in detail
An aws command is written aws [options] service action [parameters]. The service says what you are talking about (s3, ec2, iam, configure), the action says what you do with it (ls, describe-instances, get), and the parameters refine the action. Global options, such as --profile, can be added to any command. Read the line from left to right: each number points to the explanation of that piece.
Anatomy of the command
Syntaxaws [options] service action [parameters]
aws--profile prodconfiguregetregion -
awsthe programThe AWS command line. -
--profile prodthe profileoptionalThe account and the region to use. -
configurethe serviceHere, the local configuration. -
getthe actionWhat is asked of the service. -
regionthe parameterDepends on the action.
Outputus-east-1
Where aws reads profiles and the region
A profile is a named set of settings: a region, an output format, credentials. The config file holds the settings, credentials holds the keys. Each profile has its own section in square brackets there, but the two files do not name it the same way: exercise 1 makes you see it.
The region is the place where AWS runs the command. Most services are regional, so the same command queries one region at a time, and an empty response often means "nothing in this region". aws configure list-profiles gives the known profiles, aws configure get region the region written in the file for the active profile.
aws configure list-profilesaws configure get regiondefaultrecetteprodeu-west-3With no option or variable, aws takes the default profile: its region is eu-west-3. To see where each value comes from, aws configure list adds a TYPE column (the origin) and a LOCATION column (the file or the variable).
aws configure listNAME : VALUE : TYPE : LOCATIONprofile : <not set> : None : Noneaccess_key : <not set> : None : Nonesecret_key : <not set> : None : Noneregion : eu-west-3 : config-file : /home/vous/aws-lab/configLook at the region line: its TYPE is config-file, so the value comes from the config file. The keys are <not set>, since credentials is empty.
Choose a profile with AWS_PROFILE or --profile
There are two ways to pick another profile. The AWS_PROFILE variable applies to every command in the terminal where it is set, the --profile option only to the command where it is written. The second of the commands below combines both.
AWS_PROFILE=recette aws configure get regionAWS_PROFILE=recette aws configure list --profile prodeu-west-1NAME : VALUE : TYPE : LOCATIONprofile : prod : manual : --profileaccess_key : <not set> : None : Nonesecret_key : <not set> : None : Noneregion : us-east-1 : config-file : /home/vous/aws-lab/configWith the variable alone, the region is the one from [profile recette]. With both, the TYPE column of the profile is manual and LOCATION is --profile: the option wins over the variable, and the region is the one from prod. The variable acts silently: a variable set in the morning steers the whole afternoon to the wrong account, and nothing in the command reminds you of it.
How aws picks a profile, step by step
The map follows an aws configure get region command with three profiles. Before using it, predict the region read in three cases: AWS_PROFILE=recette alone, --profile prod alone, both together. Then check: tick or untick each option, move forward one source at a time, and see which one is chosen and which one is ignored.
Mental model
How aws chooses a profile
look at --profileotherwise AWS_PROFILEotherwise the default profile for each command, before any call
Try Profiles of the config file
The first one found wins
Chosen profile
$ aws configure get region
- No --profile option on the line: aws looks at the AWS_PROFILE variable.
- The AWS_PROFILE variable is not set: only the default profile is left.
- Neither option nor variable: aws takes the default profile.
What the animation shows
- The option comes before the variable, which comes before the
defaultprofile. - The variable acts silently, in the whole terminal where it is set.
- The region follows the profile, unless
AWS_REGIONis set. - A missing profile is an error, never a fallback to
default.
Golden rules
Check before acting
Which profile, and where does it come from?
aws configure listThen ask for the real account
This command queries AWS:
aws sts get-caller-identityEssential vocabulary
Read the configuration · without calling AWS
get region- The default region
aws configure get region→ eu-west-3 list-profiles- The known profiles
aws configure list-profiles→ 3 profiles AWS_REGION- A forced region
AWS_REGION=eu-central-1 aws configure list | grep -E 'profile|region' | cut -c1-36→ eu-central-1 --profile- The profile of the command
aws configure get region --profile prod→ us-east-1
Understand a failure · before any call
255- An unknown profile
aws sts get-caller-identity --profile absent; echo $?→ code 255 252- An unknown action
aws s3 lss > /dev/null 2>&1; echo $?→ code 252 252- An unknown service
aws s4 ls > /dev/null 2>&1; echo $?→ code 252 253- No key found
aws ec2 describe-instances > /dev/null 2>&1; echo $?→ code 253
A profile that cannot be found
A colleague added two profiles to config by hand, test and bac, both in region eu-south-1. They say that bac works and that test cannot be found. Reproduce their configuration, observe the failure and find the difference between the two sections.
Add the two profiles
At the end of config, add a [test] section and a [profile bac] section, each with region = eu-south-1.
Query each profile
Read the region of each one with --profile, and display the exit code of the command that fails.
Fix the faulty section
Compare the two lines in square brackets in the file, then rewrite the test one so that it is read.
- aws configure get
- config
A hint 1 / 1
In config, a profile other than default is written in a specific way. Look at the profiles that aws configure list-profiles reports.
I have tried: show the solution
printf '\n[test]\nregion = eu-south-1\n\n[profile bac]\nregion = eu-south-1\n' >> configaws configure get region --profile test; echo $?aws configure get region --profile bacThe config profile (test) could not be found255eu-south-1aws does not read the [test] section: in config, it must be [profile test]. The profile does not even appear in aws configure list-profiles. It is not replaced by default either, it is an error, exit code 255. Rewrite the line as [profile test], like the bac one, and the region becomes readable. In credentials, on the contrary, the name is written on its own: [test].
Create a profile without writing the file by hand
Writing config by hand makes it easy to forget the word profile. aws configure set writes the right section for you: with --profile neuf, it creates [profile neuf] in config. It is the only command in this part that writes to the aws configuration, and it writes to the file designated by AWS_CONFIG_FILE: the practice folder's config as long as your variables are set.
aws configure set region eu-west-2 --profile neufaws configure get region --profile neuftail -3 configeu-west-2region = eu-south-1[profile neuf]region = eu-west-2The first line is the region read for the neuf profile. The next three lines are the end of config: the section created does carry the word profile. The aws configure --profile NAME command also creates a profile, but by asking four questions about the keys; it belongs to the part that needs an account.
Where the key in use comes from: aws configure list
A profile can hold its keys in credentials, but aws can also receive them from elsewhere, for example through environment variables. In aws configure list, the TYPE column of the access_key and secret_key lines says where the key in use comes from. The keys in the exercise are fake: they give access to nothing.
Forgotten exported keys
A colleague exported keys this morning for another account. This afternoon, they set AWS_PROFILE=recette to work on the staging environment. They believe they are using the staging keys. Reproduce their situation and say which keys aws will use, where they come from and what would make it use the staging keys.
Give keys to the recette profile
Write in credentials a [recette] section with the key AKIAIOSFODNN7EXAMPLE00 and the secret secret-recette.
Export the forgotten keys
Export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE and AWS_SECRET_ACCESS_KEY=secret-factice in your subshell.
Compare the two ways of targeting recette
Run aws configure list with the AWS_PROFILE variable, then with the option, and read the end of access_key and its TYPE column. Then remove the exported keys.
- aws configure list
- credentials
A hint 1 / 1
aws configure list masks the key but leaves its last four characters visible. Compare them with those written in credentials.
I have tried: show the solution
printf '[recette]\naws_access_key_id = AKIAIOSFODNN7EXAMPLE00\naws_secret_access_key = secret-recette\n' > credentialsexport AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE AWS_SECRET_ACCESS_KEY=secret-facticeAWS_PROFILE=recette aws configure listaws configure list --profile recetteunset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEYNAME : VALUE : TYPE : LOCATIONprofile : recette : env : ['AWS_PROFILE', 'AWS_DEFAULT_PROFILE']access_key : ****************MPLE : env :secret_key : ****************tice : env :region : eu-west-1 : config-file : /home/vous/aws-lab/configNAME : VALUE : TYPE : LOCATIONprofile : recette : manual : --profileaccess_key : ****************LE00 : shared-credentials-file :secret_key : ****************ette : shared-credentials-file :region : eu-west-1 : config-file : /home/vous/aws-lab/configWith AWS_PROFILE=recette, the region does come from recette (eu-west-1), but the key ends with MPLE and comes from env: it is the key exported in the morning. A command run this way acts with the identity of the other account, in the staging region. With --profile recette, the profile's keys win: the key ends with LE00 and comes from shared-credentials-file. When an identity is surprising, aws configure list shows where it comes from; env | grep AWS_ lists the variables involved.
aws configure list answers a different question from aws configure get region. The second only reads the configuration files; the first resolves each value the way a real command would, variables included. With AWS_REGION exported, get gives the region from the file, list the one that will actually be used.
AWS_REGION=eu-central-1 aws configure get regionAWS_REGION=eu-central-1 AWS_PROFILE=recette aws configure list | grep -E 'profile|region' | cut -c1-43eu-west-3profile : recette : envregion : eu-central-1 : envThe region, in order of priority: --region, AWS_REGION, AWS_DEFAULT_REGION, then the profile's region.
Read errors without an account
When aws refuses a command, its exit code classifies the failure, and echo $? displays it right after. The message then says precisely what is wrong: always read it before looking elsewhere.
| Code | Meaning |
|---|---|
| 252 | invalid command, syntax or parameter |
| 253 | invalid environment or configuration (missing credentials) |
| 254 | the AWS service answered with an error |
| 255 | general failure, including a profile that cannot be found |
aws sts get-caller-identity --profile absent; echo $?The config profile (absent) could not be found255The absent profile does not exist: aws stops before any network call. First look for a typo in aws configure list-profiles. A misspelled action gives exit code 252, with a suggested fix.
aws s3 lssaws: [ERROR]: argument subcommand: Found invalid choice 'lss'
Maybe you meant:
* ls
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]Maybe you meant suggests the neighbouring action. An unknown service (aws s4 ls) gives the same exit code 252 and the same usage reminder, with argument command: Found invalid choice 's4' and no suggestion. A valid command without a key fails with exit code 253:
aws ec2 describe-instances --region eu-west-3Unable to locate credentials. You can configure credentials by running "aws login".aws found neither a key nor a session, and it stops before any network call. In the practice folder, this is normal; on your real configuration, this message points to a profile without credentials. Exit code 254 only appears with an account: AWS received the command and refuses it.
Discover an action and test --query without an account
Action names follow a logic: describe-* or list-* read, create-* creates, delete-* or terminate-* removes, update-* or modify-* change. aws SERVICE help opens the help of a service in a pager (q to quit, / to search), and aws SERVICE ACTION help details an action. --generate-cli-skeleton prints, as JSON, the parameters of an action without sending anything to AWS.
aws ec2 describe-instances --generate-cli-skeleton | head -5{ "InstanceIds": [ "" ], "DryRun": true,These lines name the parameters of describe-instances: InstanceIds (a list) and DryRun. DryRun matches the --dry-run option, accepted by EC2 commands to test a command without running it.
With --generate-cli-skeleton output, aws produces the sample response of the action, still without an account. That is enough to try --query: the option extracts a field from the response with a JMESPath (in French) expression, and --output text prints it as plain text.
aws ec2 describe-instances --generate-cli-skeleton output \ --query 'Reservations[].Instances[].InstanceId' --output textInstanceIdThe sample response contains a single InstanceId field, filled with its own name: the Reservations[].Instances[].InstanceId expression did extract it. Once you have an account, the same expression will list your instance identifiers.
The first part is over: exit closes the subshell and the variables disappear with it. The second part works on your real configuration.
exitConnect to your AWS account (requires an account)
From here on, you need an AWS account. The outputs in these sections carry fictitious values: you will see your own account numbers and resource names. These commands use your real configuration, hence the exit just before.
aws does not store a password: it signs each request with credentials. AWS ranks the ways to get them by order of recommendation, and temporary credentials come first. Long-term access keys are marked "Not recommended" in the AWS documentation.
| Method | What it delivers | Status at AWS |
|---|---|---|
aws login (AWS CLI 2.32.0 or later) | temporary credentials from a console session | recommended for anyone who signs in to the console with an IAM identity |
| IAM Identity Center (corporate single sign-on) | temporary credentials for a corporate user | recommended |
| IAM role on an EC2 instance | temporary credentials through the metadata service | recommended |
aws configure and IAM keys | long-term keys, no expiry | not recommended |
aws login opens the browser, you authenticate as you would on the console, and the command renews the credentials when needed, every 15 minutes, for up to 12 hours of session. The token is cached in ~/.aws/login/cache. The IAM identity must carry the SignInLocalDevelopmentAccess policy. The AWS documentation also allows the root account; it is better to create a named IAM identity first (see the lesson on IAM). To understand why temporary credentials are preferred, read temporary credentials (in French).
aws loginaws sts get-caller-identityIn a company that uses IAM Identity Center, aws configure sso creates the profile, then aws sso login --profile my-sso-profile reopens the session. For access keys, aws configure --profile NAME asks four questions and writes the answers to credentials and config. It tests nothing: a key copied wrongly only shows up on the first command.
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLEAWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYDefault region name [None]: eu-west-3Default output format [None]: jsonAn access key carries the permissions of the IAM user or role it belongs to, no more and no less. It is dangerous because it never expires: a leaked key stays valid until it is revoked. Never commit it to Git: bots scan public repositories.
Check your identity with sts get-caller-identity (requires an account)
aws sts get-caller-identity answers "which account am I on, and under which identity?". Run it before any command that deletes or stops something.
aws sts get-caller-identity --profile dev{ "UserId": "AIDACKCEVSQ6C2EXAMPLE", "Account": "123456789012", "Arn": "arn:aws:iam::123456789012:user/stephane_robert"}Look at Account, the account number: it is what prevents the accident. Arn gives the identity, an IAM user or an assumed role.
If the identity is not the one you expected, look for an override: the AWS documentation puts command-line options before variables, then files. That is what exercise 2 showed for keys. For the region, an exported AWS_REGION variable takes precedence over the region of the profile that --profile prod designates.
List your resources (requires an account)
Read commands change nothing. S3 buckets (object storage spaces) have global names: aws s3 ls lists those of the whole account. EC2 commands stay limited to the active region, which is why --region matters.
aws s3 lsaws ec2 describe-instances --region eu-west-3aws iam list-users --output tableThe ec2 and iam commands return JSON by default; aws s3 ls always prints text. An empty list or "You must specify a region" comes from a region missing in the profile or instances located elsewhere: add --region eu-west-3 or fix config.
On large listings, aws sends several calls to read all the pages. --max-items 100 limits the number of results; --no-paginate makes a single call and returns only the first page, so a truncated list (no effect on aws s3 ls). --no-cli-pager turns off the pager for one command.
Filter results with --query and --filters (requires an account)
AWS JSON is long. Two options shrink it, at two different moments: --filters asks AWS to send only certain items, --query chooses, on the terminal side, which fields to display from a response already received.
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running"--query uses the JMESPath syntax. The example below keeps three fields of each instance, and --output table puts them in columns. --output table on its own stays unreadable on an EC2 response, with dozens of columns.
aws ec2 describe-instances \ --query 'Reservations[*].Instances[*].[InstanceId,State.Name,InstanceType]' \ --output table------------------------------------------------| DescribeInstances |+--------------------+---------+---------------+| i-0abc123def456 | running | t3.micro || i-0def789abc012 | stopped | t3.small |+--------------------+---------+---------------+Each row of the table is an instance, each column a field from the bracketed list. To add a condition, the literal value is written between backticks or between single quotes. Backticks fit inside an expression wrapped in single quotes:
aws ec2 describe-instances \ --query 'Reservations[].Instances[?State.Name==`running`].InstanceId' \ --output textWith single quotes around the value, wrap the whole expression in double quotes: --query "Reservations[].Instances[?State.Name=='running'].InstanceId". The real failure is this one: single quotes written inside an expression that is itself in single quotes. The shell removes them before launching aws, and JMESPath then reads running as a field name, not as text. You can see it with echo, without aws:
echo 'State.Name=='running''State.Name==runningThe quotes have disappeared: the condition no longer compares with the text running, and nothing passes the filter. The list is empty, with no message, and the exit code is 0.
The json format (default) suits jq, text suits awk or a shell loop, table suits human reading. When --query is no longer enough, jq takes over.
Use aws with OUTSCALE (requires an OUTSCALE account)
aws talks to any service that implements the same APIs. The --endpoint-url option replaces the address contacted without changing the syntax: this is how the same aws command works for OUTSCALE, on its AWS-compatible APIs. The profile, the region and the credentials are set as in the previous sections, but they must match OUTSCALE, not AWS. The guide OUTSCALE access tools (in French) details the addresses and the limits of this approach. For a local emulator with no account, Floci answers on http://localhost:4566.
aws s3 ls --endpoint-url https://oos.eu-west-2.outscale.com --profile outscaleTo avoid repeating the option, declare the addresses in the outscale profile used above: services points to a [services ...] section that carries one endpoint_url per service. With this profile, the --endpoint-url option becomes unnecessary.
[profile outscale]region = eu-west-2output = jsonservices = outscale-services
[services outscale-services]s3 = endpoint_url = https://oos.eu-west-2.outscale.comec2 = endpoint_url = https://fcu.eu-west-2.outscale.comThe addresses follow a pattern: https://oos.{region}.outscale.com for S3 (OOS) and https://fcu.{region}.outscale.com for EC2 (FCU).
Which command should you choose?
This table finds the right command from the question.
| Your question | Command |
|---|---|
| Which account am I on? | aws sts get-caller-identity |
| Which profile, region and keys are active, and where do they come from? | aws configure list |
| How do I create a profile without picking the wrong section? | aws configure set region eu-west-3 --profile NAME |
| Which parameters does an action take? | aws SERVICE ACTION help, --generate-cli-skeleton |
| I want to test without running (EC2) | --dry-run |
Troubleshooting
The aws error messages are explicit. The table starts from the message and leads to the fix.
| Symptom | Likely cause | Solution |
|---|---|---|
The config profile (absent) could not be found, exit code 255 | Unknown profile name, or [profile name] written without the word profile in config | aws configure list-profiles, then fix the name or the section |
Unable to locate credentials, exit code 253 | Neither key nor session for this profile | aws login, or fill in the profile's credentials |
Found invalid choice 's4', exit code 252 | Misspelled service or action | Read the Maybe you meant line, or aws SERVICE help |
You must specify a region, or empty list | No region in the profile, or resources in another region | --region eu-west-3 or add region to config |
| Commands target the wrong account | AWS_PROFILE or keys exported earlier in this terminal | aws configure list, env | grep AWS_, then unset |
--query returns an empty list with no error | Single quotes swallowed by the shell inside an expression in single quotes | Backticks, or double quotes around the expression |
Clean up
If the practice folder's subshell is still open, close it with exit. Then go back to your home folder, so you do not stay in a deleted folder, and delete the practice folder:
cd ~rm -r ~/aws-labKey points
- Every command follows
aws [options] service action [parameters], and most return JSON by default (notaws s3 ls). - The profile comes from
--profile, otherwise fromAWS_PROFILE, otherwise fromdefault. A missing profile is an error, never a fallback. - Exported keys (
AWS_ACCESS_KEY_ID) take precedence over those of a profile chosen byAWS_PROFILE: only--profilemakesawsuse the profile's keys. - In
config, a profile is written[profile name]; incredentials,[name].aws configure set ... --profile NAMEavoids the mistake. aws configure get regionreads the files,aws configure listgives what will actually be used.- Before any command that deletes or stops something:
aws sts get-caller-identity, a read ofAccount, then--dry-run. Prefer temporary credentials, and keep access keys out of Git. --filtersreduces what AWS sends,--querywhat the terminal displays; a literal value is written between backticks.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
A new full quiz with random questions
Work again only on the questions you missed