Listen to this Post

Using event-driven approaches in AWS can significantly optimize workflows, especially when combined with serverless technologies. Amazon EventBridge offers powerful features like the global event bus (capturing all AWS API actions) and the EventBridge Scheduler (for recurring or one-time tasks). By integrating EventBridge Scheduler with AWS Lambda, you can automate actions such as starting/stopping EC2 instances, scaling resources, or triggering backups.
Example Use Case:
Ryan Galazka demonstrated how to stop and start EC2 instances on a schedule using Lambda and EventBridge. This approach reduces costs by stopping unused instances during non-business hours.
You Should Know:
1. AWS Lambda Function for EC2 Control
Hereβs a Python Lambda function to stop an EC2 instance:
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
instance_id = 'i-1234567890abcdef0' Replace with your instance ID
ec2.stop_instances(InstanceIds=[bash])
return f"Stopped instance: {instance_id}"
To start the instance:
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
instance_id = 'i-1234567890abcdef0' Replace with your instance ID
ec2.start_instances(InstanceIds=[bash])
return f"Started instance: {instance_id}"
2. Setting Up EventBridge Scheduler
1. Open Amazon EventBridge β Scheduler.
2. Click Create Schedule.
3. Enter a name (e.g., `Stop-EC2-Nightly`).
- Under Schedule pattern, choose Recurring schedule (e.g., `cron(0 20 ? )` for daily at 8 PM UTC).
- For Target, select Lambda function and choose your function.
6. Click Create.
3. Verify with AWS CLI
Check if the instance stopped/started:
aws ec2 describe-instance-status --instance-id i-1234567890abcdef0
4. Extending Automation
- Auto-Scale Based on CPU Usage: Use CloudWatch alarms with Lambda.
- Backup Snapshots: Trigger EBS snapshots via EventBridge.
- Cleanup Old Resources: Schedule Lambda to delete unused resources.
What Undercode Say
Automating AWS workflows with EventBridge + Lambda is a game-changer for cost optimization and efficiency. Here are additional Linux/Windows commands to enhance automation:
Linux Commands for AWS Automation
List all EC2 instances aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,State.Name]' Start/Stop via CLI aws ec2 start-instances --instance-ids i-1234567890abcdef0 aws ec2 stop-instances --instance-ids i-1234567890abcdef0 Check EventBridge Rules aws events list-rules
Windows PowerShell for AWS
Stop EC2 Instance Stop-EC2Instance -InstanceId i-1234567890abcdef0 Get Instance Status Get-EC2InstanceStatus -InstanceId i-1234567890abcdef0
For advanced automation, combine Terraform or AWS CDK to deploy scheduled Lambda functions.
Expected Output:
- EC2 instances stop/start automatically based on schedule.
- Cost savings by avoiding unnecessary uptime.
- Scalable automation for backups, scaling, and cleanup.
Explore more AWS automation here.
References:
Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass β


