Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Automating EC2 Start & Stop Using AWS Lambda, EventBridge & CloudWatch (Beginner DevOps Guide)

🚀 Automating EC2 Start & Stop Using AWS Lambda, EventBridge & CloudWatch (Beginner-Friendly DevOps Guide)

Cloud costs can quickly increase if EC2 instances run 24/7. What if you could automatically start and stop your EC2 instances based on a schedule — without managing servers?

In this blog, we’ll explore:

  1. What serverless really means

  2. AWS services involved

  3. Step-by-step EC2 automation architecture

  4. Lambda code example

  5. DevOps and cost optimization benefits

This guide is beginner-friendly and SEO-optimized for learners exploring AWS, DevOps, and Serverless Architecture.


🌩️ What Is Serverless?

Serverless is a cloud computing model where:

  1. You don’t provision or manage servers

  2. You deploy code as functions

  3. Scaling is automatic

  4. You pay only for usage

Initially, serverless was known as FaaS (Function as a Service), pioneered by:

  • AWS Lambda

But today, serverless also includes managed services like:

  1. Databases

  2. Messaging systems

  3. Storage

  4. Containers

⚠️ Important:
Serverless does NOT mean “no servers.”
It means you don’t manage them.


🧠 AWS Serverless Services Used in This Project

1️⃣ AWS Lambda

AWS Lambda is:

  1. Event-driven compute service

  2. Pay-as-you-go

  3. Auto-scaling

  4. No server management

You upload your function code, and AWS runs it when triggered.

You’re charged based on:

  1. Number of requests

  2. Execution time

Perfect for automation tasks like:

  1. Starting EC2 instances

  2. Stopping EC2 instances

  3. Cleaning S3 buckets

  4. Sending alerts


2️⃣ Amazon EventBridge

Amazon EventBridge is a serverless event bus that connects AWS services and SaaS applications.

Key Features:

  1. Supports cron and rate expressions

  2. Routes events to Lambda, EC2, ECS, CodeBuild

  3. Enables event-driven architecture

  4. No custom polling required

In this project:

👉 EventBridge triggers Lambda every 5 minutes
👉 Lambda starts or stops EC2 instances

Example Cron Expression

cron(0 12 * * ? *)

This runs every day at 12:00 PM (UTC).


3️⃣ Amazon CloudWatch

Amazon CloudWatch monitors:

  1. EC2 instances

  2. Lambda logs

  3. RDS databases

  4. Application metrics

CloudWatch helps in:

  1. Viewing Lambda execution logs

  2. Monitoring failures

  3. Creating dashboards

  4. Setting alarms

It integrates with:

  1. Amazon SNS

  2. EC2 Auto Scaling

  3. CloudTrail

  4. IAM


🏗️ Architecture: Automating EC2 Start & Stop

Here’s how the automation works:

1️⃣ EventBridge rule triggers on schedule
2️⃣ Lambda function executes
3️⃣ Lambda calls EC2 API
4️⃣ EC2 instance starts or stops
5️⃣ Logs are stored in CloudWatch

This creates a fully automated, serverless cost-optimization solution.


💻 Lambda Code Example (Start EC2)

import boto3

region = 'ap-south-1'
instances = ['i-06a14086f3b314a40']

ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('Starting your instances: ' + str(instances))

💻 Lambda Code Example (Stop EC2)

import boto3

region = 'ap-south-1'
instances = ['i-06a14086f3b314a40']

ec2 = boto3.client('ec2', region_name=region)

def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('Stopping your instances: ' + str(instances))

🔐 Required IAM Permissions

Your Lambda function needs permissions to:

  1. ec2:StartInstances

  2. ec2:StopInstances

  3. ec2:DescribeInstances

Always follow the principle of least privilege.


💰 Why Automate EC2 Start & Stop?

Many companies run development servers only during business hours.

Example:

  1. Start: 9 AM

  2. Stop: 6 PM

This reduces costs significantly.

Benefits:

✔ Saves cloud cost
✔ No manual intervention
✔ Improves DevOps efficiency
✔ Scales automatically
✔ Beginner-friendly project


🔄 EventBridge Rule Setup

You can create two rules:

Rule 1 – Start EC2

  1. Schedule: cron expression

  2. Target: Lambda (Start function)

Rule 2 – Stop EC2

  1. Schedule: cron expression

  2. Target: Lambda (Stop function)

You can also use:

rate(5 minutes)

for recurring triggers.


📊 Monitoring with CloudWatch Logs

After execution:

  1. Go to CloudWatch

  2. Open Log Groups

  3. Select your Lambda function

  4. View execution logs

This helps in debugging errors and performance tuning.


🧩 Real-World DevOps Learning

This project helps you understand:

  1. Serverless architecture

  2. Event-driven systems

  3. Infrastructure automation

  4. Cloud cost optimization

  5. IAM security best practices

It’s an excellent beginner DevOps project for:

  1. Students

  2. Cloud learners

  3. Interview preparation

  4. Resume building


🌍 Where This Is Used in Industry

Companies use similar automation for:

  1. Dev/Test environments

  2. Non-production servers

  3. Temporary workloads

  4. Scheduled batch systems

This is real-world cloud engineering practice.


🚀 Final Thoughts

Serverless is transforming DevOps.

By combining:

  1. AWS Lambda

  2. EventBridge

  3. CloudWatch

You can build scalable automation without managing infrastructure.

If you're learning AWS and DevOps, this project is a must-try.

Start small. Automate smart. Optimize costs.


💬 If you enjoyed this blog, stay connected for more hands-on cloud and DevOps tutorials.

See you in the next one!

Post a Comment

0 Comments

Ad Code

Responsive Advertisement