How to Start and Stop EC2 Instances Automatically for Cost Efficient Usage

How to Start and Stop EC2 Instances Automatically for Cost Efficiency Using EventBridge



Managing your AWS EC2 instances efficiently can save significant costs, especially if your instances are not required to run 24/7. By using Amazon EventBridge, you can automate starting and stopping EC2 instances based on your schedule. This step-by-step guide will show you how to set up this automation with EventBridge, even if you are a beginner.


Why Automate EC2 Start and Stop?

  1. Cost Savings: Avoid paying for instances when they are idle.
  2. Time Efficiency: Automation saves you from manual intervention.
  3. Scalability: Manage multiple instances with a single schedule.

Step 1: Define the Automation Requirements

Before starting, decide the following:

  • Start Time: When your instance should start running (e.g., 7:00 AM UTC).
  • Stop Time: When your instance should stop (e.g., 7:00 PM UTC).

Step 2: Create an IAM Role

  1. Go to the IAM Console:
    • Create a new role for AWS Lambda.
  2. Attach Policies:
    • Add the AmazonEC2FullAccess policy to allow Lambda to start and stop EC2 instances.
  3. Save the Role: Note the role name, as you will need it later.

Step 3: Write Lambda Functions

Amazon EventBridge triggers AWS Lambda functions to start or stop the EC2 instance. Here’s how to set up the Lambda functions.

A. Create a Lambda Function to Start the Instance

  1. Open the Lambda Console and click Create Function.
  2. Choose Author from Scratch:
    • Function Name: StartEC2Instance
    • Runtime: Python 3.x
    • Role: Select the IAM role you created earlier.
  3. Use the following Python code to start the instance:

python

import boto3

 

def lambda_handler(event, context):

    ec2 = boto3.client('ec2')

    instance_id = 'your-instance-id'  # Replace with your EC2 instance ID

    ec2.start_instances(InstanceIds=[instance_id])

    print(f"Started instance: {instance_id}")

 

  1. Save and deploy the function.

B. Create a Lambda Function to Stop the Instance

  1. Repeat the same process as above.
  2. Function Name: StopEC2Instance
  3. Use the following Python code to stop the instance:

python

import boto3

 

def lambda_handler(event, context):

    ec2 = boto3.client('ec2')

    instance_id = 'your-instance-id'  # Replace with your EC2 instance ID

    ec2.stop_instances(InstanceIds=[instance_id])

    print(f"Stopped instance: {instance_id}")

  1. Save and deploy the function.

Step 4: Schedule the Tasks Using EventBridge

Amazon EventBridge allows you to create rules to trigger the Lambda functions at specific times.

A. Create a Rule to Start the Instance

  1. Open the EventBridge Console.
  2. Click Create Rule:
    • Rule Name: StartEC2Rule
  3. Define Event Source:
    • Event Source: Schedule
    • Schedule Expression: Use a cron expression for the start time (e.g., cron (0 7 * * ? *) for 7:00 AM UTC).
  4. Target:
    • Select the StartEC2Instance Lambda function.

B. Create a Rule to Stop the Instance

  1. Follow the same steps as above.
  2. Rule Name: StopEC2Rule
  3. Schedule Expression:
    • Use a cron expression for the stop time (e.g., cron (0 19 * * ? *) for 7:00 PM UTC).
  4. Target:
    • Select the StopEC2Instance Lambda function.

Step 5: Test the Automation

  1. Wait for the scheduled times or manually test the rules by clicking Test in the EventBridge console.
  2. Check your EC2 instances to ensure they start and stop as expected.

Key Benefits of Using EventBridge

  • Reliable Scheduling: EventBridge offers precise and scalable scheduling.
  • Seamless Integration: Works perfectly with Lambda and other AWS services.
  • Cost Optimization: Automates cost-saving actions without manual intervention.

Conclusion

By using Amazon EventBridge with AWS Lambda, you can easily schedule the automatic start and stop of your EC2 instances, ensuring cost efficiency and operational convenience. Whether you’re running a development environment or hosting a low-traffic application, this setup can save you significant time and money.


Let me know if you would like help generating visuals for this blog or tweaking the content further! 😊

Top of Form

 

Bottom of Form

 

0 Comments

n8n Automation: The Ultimate Guide to Mastering Workflow Automation

  The Ultimate Guide to n8n: A No-Code Workflow Automation Tool Introduction In the modern digital world, automation is the key to efficiency. Whether you're a developer, a marketer, or an entrepreneur, automating repetitive tasks can save time and boost productivity. One of the most powerful automation tools available today is n8n . n8n is an open-source, no-code workflow automation tool that helps you integrate various applications and services with ease. Unlike traditional automation platforms, n8n gives you the flexibility to customize workflows without limitations. This blog explores everything about n8n , from installation to advanced features, helping you leverage its full potential. What is n8n? n8n (pronounced as "n-eight-n") is a workflow automation tool that connects different apps and services. It provides a visual interface to design automation processes without writing extensive code. With over 300 integrations , n8n allows users to conn...
//]]>