• TechOps

    Need clarity?
    Chat with our experts now

    • Web Hosting SupportWeb Hosting Support
    • Helpdesk Support

      Skilled and professional 24/7 helpdesk support

    • Product Support

      Boost your product support with our expertise

    • Managed ServicesManaged Services
    • Server Management

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

    • Server Monitoring

      Safeguard your server health with our comprehensive monitoring solutions

    • Staff AugmentationStaff Augmentation
    • Hire an Admin

      Transform your business operations with our expert administrative support

    • Hire a Team

      Augment your workforce with highly skilled professional from our diverse talent pool

  • CloudOps

    Confused?
    Discuss with our sales team now.

    • Author ProfileAWS
      Well Architected Review
    • Author ProfileFinOps As a Service

      FinOps As a Service

    • Migrate

      Upgrade the journey: Migrate & Modernize seamlessly

    • Modernize

      Effortless CloudOps mastery for seamless cloud management

    • Optimize

      Efficient CloudOps: Boosting performance through optimization

    • Manage

      Simplify compliance complexities with our dedicated service

  • DevOps

    How Supportsages
    is creating an
    impact?

    View Casestudies
    • Author Profile24/7 DevOps As a Service

      Round-the-clock DevOps for uninterrupted efficiency

    • Author ProfileCI/CD Pipeline

      Automated CI/CD pipeline for seamless deployments

    • Author ProfileInfrastructure As a Code

      Crafting infrastructure with ingenious code

    • Author ProfileDevSecOps

      Integrated security in continuous DevOps practices

    • Author ProfileHire DevOps Engineers

      Level up your team with DevOps visionaries

    • Author ProfileConsulting Services

      Navigate success with expert DevOps consulting

  • SecOps

    Expert SecOps Services
    for any Scale

    • Author ProfileVAPT

      Vulnerability Assessment and Penetration Testing

    • Author ProfileSource Code Review

      Ensuring source code security and safe practices to reduce risks

    • Author ProfileSecurity Consultation

      On demand services for improving server security

    • Author ProfileSystem Hardening

      Reduced vulnerability and proactive protection

    • Author ProfileManaged SOC

      Monitors and maintains system security. Quick response on incidents

    • Author ProfileCompliance as a Service

      Regulatory compliance, reduced risk

  • Insights

    Explore our latest
    insights and resources

    Blog

    Explore our latest articles and insights

    Case Studies

    Read about our client success stories

  • Contact Us

  • About
  • Certifications
  • Life at Supportsages
  • Events
  • Contact
  • Careers
  • Blog

  • Dedicated Support Team
  • Quasi-dedicated Support Team
  • Hire a DevOps Engineer
  • Hire a Billing Support Staff
  • Per-ticket Support Plan
  • Managed Services

  • Microsoft Azure Expert
  • AWS Cloud Expert
  • Hire a developer
SS

SupportSages

Bites of wisdom @ work


Copyright © 2008 - 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy

EC2 Instance Resizing with AWS Lambda and Systems Manager

Arya P B

  • 4 min read
EC2 Instance Resizing with AWS Lambda and Systems Manager

Generating audio, please wait...

In many AWS environments, the workload demands on EC2 instances can vary throughout the day. To optimize costs and performance, it’s often beneficial to dynamically adjust the instance type based on the workload. In this blog post, we’ll explore how to automate EC2 instance resizing using AWS Lambda and Systems Manager.

Prerequisites

Before we dive into the solution, make sure you have the following set up:

  1. AWS Account: Make sure you have an AWS account with the necessary permissions to create Lambda functions, EC2 instances, and Systems Manager documents.
  2. EC2 Instances with Tags: Identify the EC2 instances that you want to resize and ensure they are tagged appropriately. For example, tags like scale-up, scale-down, scale-up-time, and scale-down-time can be used. Note that the time values in the scale-up-time and scale-down-time tags must be in 24-hour format, and each time should be separated with '/'.

Architecture Overview

The solution uses AWS Lambda to trigger instance resizing based on the specified time. The Lambda function checks the current instance type and compares it with the scheduled scale-up and scale-down times. If a resize operation is required, it uses AWS Systems Manager (SSM) to start an automation document, which performs the instance resizing.

Screenshot from 2024-02-08 13-00-21.png

Implementation Steps:

1. Lambda Function Setup

Create a new Lambda function in the AWS Management Console with the following configuration:

  • Runtime: Python 3.10
  • Role: Create a new role with permissions for EC2 and SSM.
  • Triggers: Set up a CloudWatch Events trigger to schedule the Lambda function execution.

2. Lambda Function Code

Copy and paste the provided Python code into your Lambda function. This code uses the Boto3 library to interact with AWS services, including EC2 and SSM. It incorporates the AWS Systems Manager document with the name “AWS-ResizeInstance” to orchestrate the resizing process.

import boto3
from datetime import datetime, timedelta

ssm = boto3.client('ssm')
ec2 = boto3.client('ec2')

def get_instance_type(instance_id):
    response = ec2.describe_instances(InstanceIds=[instance_id])
    return response['Reservations'][0]['Instances'][0]['InstanceType'] if response['Reservations'] else None
def get_tags(response):
    for reservation in response['Reservations']:
        for instance in reservation['Instances']:
            tags = {tag['Key']: tag['Value'] for tag in instance.get('Tags', [])}
            return tags.get('scale-down'), tags.get('scale-up'), tags.get('scale-down-time'), tags.get('scale-up-time')
def resize_instance(instance_id, new_instance_type):
    try:
        ssm.start_automation_execution(
            DocumentName="AWS-ResizeInstance", 
            Parameters={'InstanceId': [instance_id], 'InstanceType': [new_instance_type]}
        )
        return f"Resizing {instance_id} to {new_instance_type} started."
    except Exception as e:
        return f"Error resizing instance: {str(e)}"

def find_times_within_30_minutes(time_list):
    current_time = datetime.now()

    for time_str in time_list:
        time = datetime.strptime(time_str, "%H:%M")
        thirty_minutes_ago = current_time - timedelta(minutes=30)

        if thirty_minutes_ago.time() <= time.time() <= current_time.time():
            return time.strftime("%H:%M")
    return None

# Lambda handler function
def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    instance_ids = [instance['InstanceId'] for reservation in ec2.describe_instances()['Reservations'] for instance in reservation['Instances']]

    for instance_id in instance_ids:
        current_instance_type = get_instance_type(instance_id)
        scale_down, scale_up, scale_down_time, scale_up_time = get_tags(ec2.describe_instances(InstanceIds=[instance_id]))

        if current_instance_type == scale_up and find_times_within_30_minutes(scale_down_time.split('/')):
            return resize_instance(instance_id, scale_down)
        
        elif current_instance_type == scale_down and find_times_within_30_minutes(scale_up_time.split('/')):
            return resize_instance(instance_id, scale_up)

3. EC2 Instance Tagging

Tag your EC2 instances with the following key-value pairs:

  • scale-up: The target instance type for scale-up.
  • scale-down: The target instance type for scale-down.
  • scale-up-time: The time window for scale-up operations (e.g., 17:51/01:10).
  • scale-down-time: The time window for scale-down operations (e.g., 17:00/02:30).

4. Testing

Test the Lambda function by manually triggering it and observing the logs. Ensure that the function correctly identifies the current instance type and performs the resizing operation within the specified time.

5. Scheduling

Set up a recurring CloudWatch Events rule to trigger the Lambda function at the desired intervals. For example, you might schedule it to run every hour. Configure the rule to trigger at regular intervals using a cron expression (e.g., cron(0 0 * * ? *) for daily execution).

AWS Systems Manager Document

The AWS Systems Manager document named “AWS-ResizeInstance” plays a crucial role in this solution. It orchestrates the resizing process by taking parameters InstanceId and InstanceType and executing the necessary steps to resize the specified EC2 instance.

Optimize your EC2 sizes effortlessly with this robust DevOps approach! Discover the simple Lambda + Systems Manager combo to auto-resize based on workload. Save costs, and boost performance. Read the guide & conquer operational complexity with ease!

  • DevOps

Looking for AWS Experts?

We provide top-of-the-line custom AWS setup services tailored to your needs.

EC2 Instance Resizing with AWS Lambda and Systems Manager

Analyzing AWS IAM Users: Access Key and Password Age

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

Analyzing AWS IAM Users: Access Key and Password Age

Analyzing AWS IAM Users: Access Key and Password Age
  • 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

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

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

Posts by Arya P B

Athena