Interview Prep/Python for DevOps/boto3 & AWS automation

How do you iterate over all regions?

IntermediateConceptboto3 & AWS automation
Answer
regions = [r["RegionName"] for r in
           boto3.client("ec2", region_name="us-east-1").describe_regions(
               Filters=[{"Name": "opt-in-status", "Values": ["opt-in-not-required", "opted-in"]}]
           )["Regions"]]

describe_regions returns only enabled regions by default (add AllRegions=True to see disabled ones). boto3.Session().get_available_regions("ec2") comes from the SDK's local data and may include regions not enabled for your account, which then fail with AuthFailure or UnrecognizedClientException.

All Python for DevOps questions Roadmap: Python for automation

More on boto3 & AWS automation