Interview Prep/Python for DevOps/boto3, HTTP & API errors

`botocore.exceptions.ClientError: An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.`

BeginnerTroubleshootingboto3, HTTP & API errors
Answer

Likely causes → The identity lacks ec2:DescribeInstances, an SCP or permission boundary denies it, or you are in the wrong account/role.

Diagnose →

aws sts get-caller-identity
aws iam simulate-principal-policy --policy-source-arn <role-arn> --action-names ec2:DescribeInstances

For EC2 write calls the message includes an encoded authorization message: aws sts decode-authorization-message --encoded-message <msg>.

Fix → Add the permission to the role (least privilege), or switch to the correct role.

Prevent → Keep the tool's IAM policy in code next to the tool; test with a sandbox role.

All Python for DevOps questions Roadmap: Python for automation

More on boto3, HTTP & API errors