Sub topic 15
Troubleshooting Common Errors
This chapter assumes you have installed AWS CLI v2, created at least one named profile, and read the earlier chapters on credentials, Regions, and the command structure. Most CLI failures fall into three groups: the wrong identity, the wrong Region, and missing permissions. Each group has its own error messages and its own first diagnostic step.
A Method for Diagnosing Errors
Work through the same three questions every time, in this order:
- Who am I? Which identity and account does the CLI resolve?
- Where am I? Which Region is the command sent to?
- Am I allowed? Does that identity have permission for this action on this resource?
The order matters. A permission error for the wrong identity is not fixed by editing a policy, and a "resource not found" error in the wrong Region is not fixed by creating the resource again.
Step 1: Verify the Identity
Run this command first. It needs no permissions, so it works for any valid credentials.
aws sts get-caller-identity --profile devThe output shows UserId, Account, and Arn. Compare the account ID and the role or user name with what you expect. If you see an unexpected account, a root ARN, or an old IAM user, you have found the problem before changing anything.
Credential errors
| Error message | Likely cause | Fix |
|---|---|---|
| `Unable to locate credentials. You can configure credentials by running "aws configure".` | No credentials found in environment variables, profile files, or a role | Run `aws configure` or `aws configure sso`, or pass `--profile` |
| `The config profile (dev) could not be found` | The profile name is misspelled or defined in a different file | Check `~/.aws/config` for `[profile dev]` |
| `InvalidClientTokenId` | The access key ID is wrong, deleted, or belongs to a different partition | Create or select valid credentials |
| `SignatureDoesNotMatch` | The secret key is wrong, has stray whitespace, or the system clock is far off | Re-enter the key and check the clock |
| `ExpiredToken` or `The security token included in the request is expired` | Temporary credentials have expired | Sign in again, for example `aws sso login` |
| `Error loading SSO Token: Token for ... does not exist` | You have not signed in, or the cached token was removed | Run `aws sso login --profile dev` |
Environment variables override profiles
The credential lookup order puts command-line options first, then environment variables, then the profile files. A stale AWS_ACCESS_KEY_ID left in your shell silently beats the profile you named in ~/.aws/config. This is a common reason get-caller-identity returns an identity you did not expect.
Check for leftovers:
env | grep '^AWS_'Remove them with unset:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_PROFILENote that AWS_SESSION_TOKEN must be removed together with the key pair. A session token paired with the wrong key produces InvalidClientTokenId.
Clock skew
Requests are signed with a timestamp. If your machine's clock differs from AWS by more than a few minutes, requests fail with SignatureDoesNotMatch or a message that the request signature has expired. This often appears in virtual machines that were suspended and resumed. Resync the system clock with your operating system's time service.
Step 2: Verify the Region
Most services are Regional. A resource that exists in eu-west-1 is invisible to a call sent to us-east-1. The CLI picks the Region in this order: --region, the AWS_REGION or AWS_DEFAULT_REGION environment variable, then the profile's region setting.
Region symptoms
| Symptom | Likely cause |
|---|---|
| `You must specify a region. You can also configure your region by running "aws configure".` | No Region in any source |
| A list command returns empty results although the Console shows resources | The Console and the CLI use different Regions |
| `Could not connect to the endpoint URL: "https://ec2.xx-fake-1.amazonaws.com/"` | A mistyped Region name |
| `An error occurred (AuthFailure)` or `OptInRequired` | The Region is not enabled for your account |
| `NoSuchBucket` or `InvalidInstanceID.NotFound` for something you know exists | The resource lives in another Region |
To see the Region a profile will use, run:
aws configure get region --profile devIf the command prints nothing, the profile has no Region and the CLI falls back to the environment. Add a region line to the profile, or pass --region on the command.
Global and special-case services
IAM, Route 53, and CloudFront are global, but their API calls still go to a specific endpoint. Billing and some other global features are served from us-east-1. If a global service call fails with a Region error, try --region us-east-1. Amazon S3 bucket names are global, but each bucket lives in one Region. Commands such as aws s3 ls list all buckets, while operations on the bucket contents go to the bucket's own Region.
The Console Region selector
In the Console, the Region selector in the top bar decides what you see. If a resource exists in the CLI but not in the Console, compare the Region in the selector with the Region the CLI used. The same applies in CloudShell, where the Region is set by the selector at launch time.
Step 3: Verify Permissions
When identity and Region are right, the remaining failures are usually authorization errors.
An error occurred (AccessDenied) when calling the ListBuckets operation: User: arn:aws:iam::111122223333:user/example is not authorized to perform: s3:ListAllMyBuckets because no identity-based policy allows the s3:ListAllMyBuckets actionRead the message in four parts: the principal (who), the action (what), the resource (on what), and the stated reason. The reason names the policy type that blocked the call, which tells you where to look.
| Reason in the message | Where to look |
|---|---|
| no identity-based policy allows | The user's, group's, or role's attached policies |
| with an explicit deny in an identity-based policy | A `Deny` statement in a user or role policy |
| with an explicit deny in a service control policy | AWS Organizations SCPs |
| no resource-based policy allows | The bucket policy, key policy, or similar |
| because no permissions boundary allows | The permissions boundary on the user or role |
Different services spell the error differently. EC2 returns UnauthorizedOperation and may include an encoded message. Decode it with aws sts decode-authorization-message, which requires the sts:DecodeAuthorizationMessage permission.
aws sts decode-authorization-message \
--encoded-message "ENCODED_MESSAGE_FROM_ERROR" \
--query DecodedMessage \
--output textPermission traps
- An explicit
Denyalways wins over anyAllow. - SSO permission set changes can take a few minutes to reach the role. Sign out and back in with
aws sso loginif the permissions seem stale. - An action can be allowed but still fail because of a condition, such as a required MFA, a source IP, or a tag.
- Some calls need permissions on more than one resource. For example, launching an instance with
iam:PassRolerequires that permission for the role being passed.
Other Frequent Failures
Quoting and JMESPath
A broken --query often produces an error such as Bad value for --query or returns None. On Linux and macOS, wrap the expression in single quotes. In Windows PowerShell and cmd, quoting differs, so use double quotes outside and backticks or escaped quotes for literals. If a filter returns nothing, drop --query and inspect the raw JSON to confirm the field names, which are case sensitive.
Filters versus queries
--filters is processed by the service, while --query runs in the CLI after results return. If --filters uses the wrong name or value, the service returns an error or an empty list. Filtering the wrong field name with --query instead returns null values with no error.
Truncated results
If a list seems too short, check for pagination. The CLI pages automatically unless you set --max-items, and then it prints a NextToken. Pass it with --starting-token to continue.
Pager and output surprises
AWS CLI v2 sends long output to a pager such as less. In scripts this can block. Disable it with --no-cli-pager or set AWS_PAGER="".
Debugging with `--debug`
When the cause is unclear, add --debug. It prints the credential provider chain, the resolved Region, the endpoint, and the raw HTTP response.
aws s3api list-buckets --profile dev --debug 2>&1 | grep -iE 'credentials|region|endpoint'Debug output can include sensitive details, so do not paste it into public tickets without checking it first.
Summary Checklist
- Run
aws sts get-caller-identityand confirm account and role. - Look for
AWS_*environment variables that override the profile. - Confirm the Region with
aws configure get regionor--region. - Read the
AccessDeniedmessage for the policy type that blocked the call. - Use
--debugwhen the first four steps do not explain the failure.
Exercise
Your aws ec2 describe-instances call returns an empty list, but the Console shows three running instances. aws sts get-caller-identity shows the right account. What do you check next, and which command shows the Region your profile uses?
Show answer
Check the Region. The Console's Region selector probably differs from the CLI's Region. Run aws configure get region --profile NAME to see the profile's Region, check AWS_REGION and AWS_DEFAULT_REGION in the environment, and retry with --region set to the Region shown in the Console.
Exercise
A command returns An error occurred (AccessDenied) ... with an explicit deny in a service control policy. Will adding an Allow statement to your IAM role's policy fix it? Why or why not?
Show answer
No. An explicit deny overrides any allow, and a service control policy is set at the AWS Organizations level. An Organizations administrator must change the SCP, or you must use an account or action the SCP permits.