🚀 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:
What serverless really means
AWS services involved
Step-by-step EC2 automation architecture
Lambda code example
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:
You don’t provision or manage servers
You deploy code as functions
Scaling is automatic
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:
Databases
Messaging systems
Storage
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:
Event-driven compute service
Pay-as-you-go
Auto-scaling
No server management
You upload your function code, and AWS runs it when triggered.
You’re charged based on:
Number of requests
Execution time
Perfect for automation tasks like:
Starting EC2 instances
Stopping EC2 instances
Cleaning S3 buckets
Sending alerts
2️⃣ Amazon EventBridge
Amazon EventBridge is a serverless event bus that connects AWS services and SaaS applications.
Key Features:
Supports cron and rate expressions
Routes events to Lambda, EC2, ECS, CodeBuild
Enables event-driven architecture
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:
EC2 instances
Lambda logs
RDS databases
Application metrics
CloudWatch helps in:
Viewing Lambda execution logs
Monitoring failures
Creating dashboards
Setting alarms
It integrates with:
Amazon SNS
EC2 Auto Scaling
CloudTrail
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:
ec2:StartInstances
ec2:StopInstances
ec2:DescribeInstances
Always follow the principle of least privilege.
💰 Why Automate EC2 Start & Stop?
Many companies run development servers only during business hours.
Example:
Start: 9 AM
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
Schedule: cron expression
Target: Lambda (Start function)
Rule 2 – Stop EC2
Schedule: cron expression
Target: Lambda (Stop function)
You can also use:
rate(5 minutes)
for recurring triggers.
📊 Monitoring with CloudWatch Logs
After execution:
Go to CloudWatch
Open Log Groups
Select your Lambda function
View execution logs
This helps in debugging errors and performance tuning.
🧩 Real-World DevOps Learning
This project helps you understand:
Serverless architecture
Event-driven systems
Infrastructure automation
Cloud cost optimization
IAM security best practices
It’s an excellent beginner DevOps project for:
Students
Cloud learners
Interview preparation
Resume building
🌍 Where This Is Used in Industry
Companies use similar automation for:
Dev/Test environments
Non-production servers
Temporary workloads
Scheduled batch systems
This is real-world cloud engineering practice.
🚀 Final Thoughts
Serverless is transforming DevOps.
By combining:
AWS Lambda
EventBridge
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!
0 Comments