Start a Project

Auto-Resize AWS EBS Volume on High Disk Usage

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 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:

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:

Make sure the Lambda IAM role allows:

Step 4: Set Cron Job

crontab -e

Final Output

Now, your EC2 will:

Exit mobile version