Interview Prep/Python for DevOps/Concurrency: threads, processes & asyncio

What is a thread-safe way to share a boto3 client or Session?

AdvancedConceptConcurrency: threads, processes & asyncio
Answer

boto3 low-level clients are documented as generally thread-safe, so one client can be shared across threads. Session objects and resources are not thread-safe. The safe pattern is: create the Session per thread (or create all clients up front in the main thread) and never create clients concurrently from a shared default session, which can raise intermittent errors like KeyError: 'endpoint_resolver' or credential_provider races. In our lab, each region worker creates its own boto3.session.Session().

All Python for DevOps questions Roadmap: Python for automation

More on Concurrency: threads, processes & asyncio