How would you speed up a boto3 script that audits 30 accounts and 17 regions?
AdvancedConceptboto3 & AWS automation
Answer
- Parallelize across account/region pairs with
ThreadPoolExecutor; boto3 calls are I/O bound. - Create one Session per thread (Sessions are not thread-safe; clients are), and one client per service and region, reused across calls.
- Raise
max_pool_connectionsinConfigto match workers. - Use
adaptiveretry mode so throttling slows you down instead of failing. - Filter server-side (
Filters=) rather than downloading everything. - Use batch APIs (
get_metric_data, Resource Groups Tagging API), or AWS Config advanced queries / Resource Explorer for inventory instead of describe calls at all.
More on boto3 & AWS automation
What is the difference between a boto3 Session, client and resource?BeginnerHow does boto3 find credentials?BeginnerHow does boto3 decide the region, and what is the gotcha with `AWS_REGION`?AdvancedWhy do you need paginators, and how do you use them?BeginnerWhat is JMESPath `search()` on paginators?AdvancedHow do you handle errors from boto3?Beginner