• DevOps
    Case Study

    How we built a resilient multi-account, multi-cloud solution for a Health Tech service provider!

    READ CASESTUDY
    icon

    24/7 DevOps as a Service

    Round-the-clock DevOps for uninterrupted efficiency.

    icon

    Infrastructure as a Code

    Crafting infrastructure with ingenious code.

    icon

    CI/CD Pipeline

    Automated CI/CD pipeline for seamless deployments.

    icon

    DevSecOps

    Integrated security in continuous DevOps practices.

    icon

    Hire DevOps Engineers

    Level up your team with DevOps visionaries.

    icon

    Consulting Services

    Navigate success with expert DevOps consulting.

  • TechOps
    Case Study

    How we built a scalable Odoo solution for a Travel Tech service provider!

    READ CASESTUDY

    WEB HOSTING SUPPORT

    icon

    HelpDesk Support

    Highly skilled 24/7 HelpDesk Support

    icon

    Product Support

    Boost your product support with our expertise.

    MANAGED SERVICES

    icon

    Server Management

    Don’t let server issues slow you down. Let us manage them for you.

    icon

    Server Monitoring

    Safeguard your server health with our comprehensive monitoring solutions.

    STAFF AUGMENTATION

    icon

    Hire an Admin

    Transform your business operations with our expert administrative support.

    icon

    Hire a Team

    Augment your workforce with highly skilled professionals from our diverse talent pool.

  • CloudOps
    Case Study

    How we helped a Private Deemed University in India, save US $3500/m on hosting charges!

    READ CASESTUDY
    icon

    AWS Well Architected Review

    Round-the-clock for uninterrupted efficiency

    icon

    Optimize

    Efficient CloudOps mastery for seamless cloud management

    icon

    Manage

    Automated CI/CD pipeline for seamless deployments

    icon

    Migrate

    Upgrade the journey, Migrate & Modernize seamlessly

    icon

    Modernize

    Simplify compliance complexities with our dedicated services

    icon

    FinOps as a Service

    FinOps as a Service

  • SecOps
    Case Study

    How we built a scalable Odoo solution for TravelTech service provider!

    READ CASESTUDY
    icon

    VAPT

    Vulnerability Assessment and Penetration Testing

    icon

    Source Code Review

    Ensuring source code security ans safe practices to reduce risks

    icon

    Security Consultation

    On demand services for improving server security

    icon

    System Hardening

    Reduced vulnerability and proactive protection

    icon

    Managed SoC

    Monitors and maintains system security. Quick response on incidents.

    icon

    Compliance as a Service

    Regulatory compliance, reduced risk

  • Insights
    Case Study

    How we helped a Private Deemed University in India, save US $3,500/m on hosting charges!

    READ CASESTUDY
    icon

    Blog

    Explore our latest articles and insights

    icon

    Case Studies

    Read about our client success stories

    icon

    Flipbook

    Explore our latest Flipbook

    icon

    Events

    Join us at upcoming events and conferences

    icon

    Webinars

    Watch our educational webinar series

  • Our Story
  • Contact Us

Interested to collaborate?

Get in touch with us!

Ready to elevate your business with certified cloud expertise? Contact us today to learn how our team can help you leverage cloud technology to drive growth, streamline operations, and enhance security.

  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Kubernetes Consultancy Services
  • K8s & Cloud native Solutions
  • 24/7 Infrastructure Monitoring
  • DevOps as a Service
  • Cloud CI/CD Solutions
  • White Labeled MSP Support
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us

Connect with us!


LinkedInFacebookXInstagramYouTube

aws partneraws advanced partner
SupportSages

Copyright © 2008 – 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy
Automatically Retrieve AWS Activated Regions Using AWS Lambda and Boto3

Automatically Retrieve AWS Activated Regions Using AWS Lambda and Boto3

Arya P B

  • 2 min read
Automatically Retrieve AWS Activated Regions Using AWS Lambda and Boto3

Generating audio, please wait...

Introduction

AWS Regions allow users to deploy applications closer to their customers. Sometimes, it’s important to dynamically determine which AWS regions are activated in an account. This blog demonstrates how to use an AWS Lambda function with Boto3 to list all activated AWS regions.

Overview

The Lambda function performs the following actions:

  1. Connects to the AWS EC2 service.
  2. Calls the describe_regions API to fetch all AWS regions.
  3. Filters for regions that are activated (opted-in or do not require opt-in).

Prerequisites

  1. IAM Role: The Lambda function must have permissions to call ec2:DescribeRegions.
  2. Boto3: AWS SDK for Python is required to interact with AWS services.

Lambda Function Code

import boto3
import json

def get_activated_regions(ec2_client):
"""Get all activated AWS regions for the assumed role."""
try:
# Retrieve all regions
response = ec2_client.describe_regions(AllRegions=True)
all_regions = response["Regions"]
# Filter for regions that are currently enabled
activated_regions = [region["RegionName"] for region in all_regions if region["OptInStatus"] == "opt-in-not-required" or region["OptInStatus"] == "opted-in"]

return activated_regions
except Exception as e:
print(f"Error fetching regions: {e}")
return []
def lambda_handler(event, context):
"""Lambda handler function."""
try:
# Create an EC2 client
ec2_client = boto3.client('ec2')

# Get activated AWS regions
activated_regions = get_activated_regions(ec2_client)

print(f"Activated regions: {activated_regions}")

return {
'statusCode': 200,
'body': json.dumps({
'message': 'Successfully retrieved activated regions.',
'regions': activated_regions
})
}
except Exception as e:
print(f"Error in lambda_handler: {e}")
return {
'statusCode': 500,
'body': json.dumps({
'message': 'Error retrieving activated regions.',
'error': str(e)
})
}

Key Concepts

  1. Activated Regions: AWS regions that are either automatically enabled or have been opted-in manually.
  2. EC2 describe_regions API: This API call returns the list of AWS regions with their opt-in status.
  3. Boto3: AWS SDK for Python, which is used to interact with AWS services from the Lambda function.

How to Deploy

  1. Create Lambda Function
    Open AWS Lambda and create a new function.
    Copy and paste the code provided above.
  2. IAM Role
    Attach a policy to the Lambda’s execution role with permission for ec2:DescribeRegions.
  3. Test the Function
    Run a test event in the Lambda console to verify that the list of activated regions is returned.

Possible Enhancements

  1. Logging and Monitoring: Add logging to AWS CloudWatch for better debugging.
  2. Custom Alerts: Use AWS SNS to send notifications if the Lambda encounters any errors.

Conclusion

This Lambda function dynamically retrieves all activated AWS regions, which can be useful for compliance, resource deployment, and auditing purposes. By using this automated approach, you can save time and maintain accuracy in managing your AWS infrastructure.

  • AWS
  • DevOps

Continue Your Journey With…

DevOps as a Service

DevOps as a Service

Let us do the heavy lifting for you

Promotional banner
Promotional banner

Analyzing AWS IAM Users: Access Key and Password Age

Analyzing AWS IAM Users: Access Key and Password Age
  • DevOps
logo

Analyzing AWS IAM Users: Access Key and Password Age

Analyzing AWS IAM Users: Access Key and Password Age
  • AWS
  • DevOps
logo

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide
  • DevOps
logo

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide

Auto-Restart EC2 Instances on Status Check Failure: Quick Setup Guide
  • AWS
  • DevOps
logo

Posts by Arya P B

Athena