• 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
AWS Backup Management with Lambda

AWS Backup Management with Lambda

Arya P B

  • 4 min read
AWS Backup Management with Lambda

Generating audio, please wait...

Managing backups is a critical task for ensuring data integrity and availability in cloud environments. AWS Backup provides a centralized way to automate and manage backups across AWS services. However, keeping track of backup plans, selections, and jobs can become complex as your infrastructure grows.

In this blog, I’ll guide you through creating an AWS Lambda function that automates the process of listing backup plans, selections, and jobs, giving you a clear overview of your backup strategy.

Prerequisites

Before diving into the implementation, make sure you have:

  • An AWS account
  • Basic knowledge of AWS Lambda and the Backup service
  • Appropriate IAM permissions to access AWS Backup resources

Overview

The Lambda function we’ll create will perform the following tasks:

  1. List all backup plans in your AWS account.
  2. For each backup plan, list the resource selections.
  3. Retrieve and display the most recent backup job for each plan.

Let’s dive into the code and understand how it works.

Step 1: Setting Up IAM Roles and Policies

Before creating the Lambda function, ensure you have the necessary IAM permissions. Create an IAM role with a policy that allows access to AWS Backup services:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"backup:DescribeBackupVault",
"backup:ListBackupPlans",
"backup:ListBackupJobs",
"backup:GetBackupPlan",
"backup:ListBackupVaults",
"backup:GetBackupVaultAccessPolicy",
"backup:ListBackupSelections",
"backup:DescribeBackupVault",
"backup:ListRecoveryPointsByBackupVault",
"backup:GetBackupSelection"
],
"Resource": "*"
}
]
}

Attach this policy to the role that your Lambda function will use.

Step 2: Create the Lambda Function

Now, let’s create the Lambda function using the following Python script:

import boto3
import json

backup_client = boto3.client('backup')

def lambda_handler(event, context):
backup_plans = backup_client.list_backup_plans()['BackupPlansList']

results = []

for plan in backup_plans:
plan_id = plan['BackupPlanId']
plan_name = plan['BackupPlanName']
print(f"Backup Plan: {plan_name} (ID: {plan_id})")

plan_details = backup_client.get_backup_plan(BackupPlanId=plan_id)['BackupPlan']
rules = plan_details['Rules']

for rule in rules:
rule_name = rule['RuleName']
policy = rule.get('ScheduleExpression', 'No Schedule Expression')

vault_name = rule.get('TargetBackupVaultName', None)

if vault_name:
vault_details = describe_backup_vault(vault_name)

if vault_details:
recovery_points = list_recovery_points(vault_name)

results.append({
'plan_name': plan_name,
'plan_id': plan_id,
'vault_name': vault_name,
'rule_name': rule_name,
'policy': policy,
'recovery_points': recovery_points
})
else:
print(f"Failed to describe vault for {vault_name}")
else:
print(f"No vault associated with rule {rule_name} in plan {plan_name}")

return {
'statusCode': 200,
'body': json.dumps(results, default=str) # Convert datetime objects to string
}

def describe_backup_vault(vault_name):
try:
response = backup_client.describe_backup_vault(BackupVaultName=vault_name)
return response
except Exception as e:
print(f"Error describing vault {vault_name}: {str(e)}")
return None

def list_recovery_points(vault_name):
try:
recovery_points = []
next_token = None

while True:
# Fetch recovery points with the vault name and next_token if it exists
if next_token:
response = backup_client.list_recovery_points_by_backup_vault(BackupVaultName=vault_name, NextToken=next_token)
else:
response = backup_client.list_recovery_points_by_backup_vault(BackupVaultName=vault_name)

for point in response.get('RecoveryPoints', []):
recovery_points.append({
'recovery_point_arn': point.get('RecoveryPointArn'),
'creation_date': point.get('CreationDate'),
'resource_arn': point.get('ResourceArn'),
'resource_type': point.get('ResourceType'),
'backup_size': point.get('BackupSizeBytes')
})

next_token = response.get('NextToken')
if not next_token:
break
return recovery_points

except Exception as e:
print(f"Error listing recovery points for vault {vault_name}: {str(e)}")
return [];

Explanation

  • AWS SDK (Boto3): We use Boto3 to interact with AWS Backup.
  • List Backup Plans: Retrieves a list of all backup plans in the account.
  • List Backup Selections: For each backup plan, lists the associated resource selections.
  • List Backup Jobs: Retrieves the most recent backup job for each plan to provide a status update.
  • Lambda Handler: Orchestrates the process and returns the results in JSON format.

Step 3: Deploy the Lambda Function

To deploy the function:

  1. Open the AWS Management Console and navigate to the Lambda service.
  2. Click “Create function” and choose “Author from scratch.”
  3. Enter a name for your function and select the execution role created earlier.
  4. Copy and paste the above Python script into the function code editor.
  5. Click “Deploy.”

Step 4: Test the Lambda Function

You can test your Lambda function using the AWS Lambda console:

  • Click on “Test” and create a new test event with the default template.
  • Execute the function and check the results in the execution output.

Sample Output

The function returns a JSON array with details of each backup plan, including resource selections and the last backup job execution time:
sample output:

[
{
"plan_name": "sample-backup-plan",
"plan_id": "sample-plan-id",
"vault_name": "sample-vault-name",
"frequency": "cron(0 12 ? * * *)",
"recovery_points": [
{
"recovery_point_arn": "arn:aws:backup:region:account-id:recovery-point:sample-recovery-point-id",
"creation_date": "2024-01-01 00:00:00+00:00",
"resource_arn": "arn:aws:rds:region:account-id:cluster:sample-cluster",
"resource_type": "Aurora",
"backup_size": null
}
]
}
]

Conclusion

By following these steps, you have automated the process of monitoring AWS Backup plans, selections, and jobs using an AWS Lambda function. This setup provides a clear view of your backup strategy, helping you ensure that your data is protected and readily available when needed.

Implement this AWS Lambda function to automate monitoring your AWS Backup plans and gain full visibility over your backup strategy! Don’t leave your data security to chance.

  • 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