Interview Prep/Python for DevOps/Concurrency, async, tests, services & containers

pytest with moto fails with `botocore.exceptions.ClientError: An error occurred (InvalidClientTokenId) when calling the ... operation` (it is reaching real AWS).

IntermediateTroubleshootingConcurrency, async, tests, services & containers
Answer

Likely causes → The client was created outside the mock_aws context (at module import, in a session-scoped fixture created before the mock started), so it talks to real AWS with fake credentials.

Diagnose → Check where clients are created relative to mock_aws() / @mock_aws.

Fix → Create clients inside the mock (in fixtures that depend on the mock fixture), not at import time. Set fake credentials in an autouse fixture so a leak fails fast instead of using your real profile.

Prevent → No module-level clients; dependency-inject clients into functions.

All Python for DevOps questions Roadmap: Python for automation

More on Concurrency, async, tests, services & containers