Auto-Resize AWS EBS Volume on High Disk Usage

Updated 21 July 2025

Introduction

Running out of disk space on your AWS EC2 instance can cause downtime or data loss. In this blog, you’ll learn how to automatically auto-resize EBS volumes when disk usage exceeds a set threshold.

Also If you’re concerned about protecting your AWS infrastructure, check out our article on AWS Shield protection against DDoS attacks.

 Overview

This solution automatically monitors disk usage on an EC2 instance. If it exceeds a defined threshold (e.g., 85%), the instance:

  1. Publishes a message to an SNS topic.
  2. The SNS topic triggers a Lambda function.
  3. The Lambda function increases the EBS volume size.
  4. The EC2 script detects the change and auto-resize the partition and filesystem accordingly.

Prerequisites

  • An EC2 instance with:
    • Root volume using EBS.
    • Filesystem: ext4 or xfs.
    • IAM Instance Role (e.g., InstanceRole) with:

An SNS topic (e.g., disk-alerts) with a subscription to your Lambda function.

A Lambda function (disk-auto-manage) with execution role that has EC2 modify permissions.

Step 1: EC2 Script to Monitor Disk

We use a Bash script that:

  • Checks disk usage.
  • If usage exceeds threshold, sends an SNS alert.
  • Waits for the volume to be resized.
  • Then auto-resize the Linux partition and filesystem.

Note: Replace [Region] with the region where your SNS is set up, and update the ARN value with your SNS topic’s ARN.

Step 2: IAM Role & SNS Setup

Attach an IAM role (e.g., InstanceRole) to the instance with permissions for SNS publishing.

Create an SNS topic (e.g., disk-alerts) in us-east-2 and link it to a Lambda.

Step 3: Lambda to Resize EBS Volume

The Lambda will:

  • Parse the SNS message.
  • Get the EBS volume ID from EC2 metadata.
  • Check current volume size.
  • If the volume size is below 100 GB, increase it by 20 GB,  if it is 100 GB or more, increase it by 50 GB

Make sure the Lambda IAM role allows:

Step 4: Set Cron Job

crontab -e

Final Output

Now, your EC2 will:

  • Monitor disk usage every 30 min.
  • Alert and expand volume automatically.
  • And auto-resize filesystem without downtime.
Category(s) Uncategorized
author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home