In modern cloud environments, optimizing costs while maintaining operational efficiency is crucial. AWS offers powerful tools like EventBridge and Lambda that can automate tasks such as shutting down EC2 and RDS instances when they’re not actively in use. This automation reduces costs and ensures that resources are used efficiently based on your organization’s operational schedules.
Why Automate EC2 and RDS Shutdown?
EC2 instances and RDS databases can incur costs even when they’re idle. By automating their shutdown during non-business hours or when not actively used, you can:
- Reduce Costs: Eliminate unnecessary expenditure on compute and database resources.
- Improve Security: Reduce exposure to potential security risks associated with running unnecessary instances.
- Operational Efficiency: Ensure resources are available when needed without manual intervention.
Prerequisites
Before setting up automation, ensure you have the following prerequisites:
- AWS Account: Access to AWS Management Console with appropriate permissions.
- EventBridge Rule: Configured to trigger Lambda functions based on a schedule or event.
- Lambda Function: Defined to perform shutdown actions on EC2 and RDS instances.
- IAM Roles: Required permissions for Lambda to interact with EC2 and RDS instances.
Setting Up Automation
Step 1: Create Lambda Function
Create a Lambda Function: Begin by creating a Lambda function in the AWS Management Console. This function will contain the logic to identify and shut down EC2 and RDS instances.
import boto3
def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’)
rds = boto3.client(‘rds’)
# Describe instances to identify those running
ec2_instances = ec2.describe_instances(Filters=[{‘Name’: ‘instance-state-name’, ‘Values’: [‘running’]}])
rds_instances = rds.describe_db_instances()
# Shutdown EC2 instances
for reservation in ec2_instances[‘Reservations’]:
for instance in reservation[‘Instances’]:
instance_id = instance[‘InstanceId’]
ec2.stop_instances(InstanceIds=[instance_id])
# Stop RDS instances
for instance in rds_instances[‘DBInstances’]:
instance_id = instance[‘DBInstanceIdentifier’]
rds.stop_db_instance(DBInstanceIdentifier=instance_id)
return {
‘statusCode’: 200,
‘body’: ‘Instances shutdown successfully!’
}
- Customize the Lambda function to suit your specific instance identification and shutdown requirements.
Step 2: Set Up EventBridge Rule
- Configure EventBridge Rule: Navigate to EventBridge in the AWS Management Console and create a rule that triggers the Lambda function based on a schedule (e.g., daily at non-business hours).
- Event Source: Schedule (e.g., cron expression).
- Target: Lambda function created in Step 1.
Step 3: IAM Permissions
- Grant IAM Permissions: Ensure the Lambda function has IAM permissions to describe and stop EC2 and RDS instances. Create an IAM role with the necessary policies attached and assign it to your Lambda function.
Testing and Monitoring
- Testing: Validate the setup by manually triggering the EventBridge rule or waiting for the scheduled trigger.
- Monitoring: Monitor Lambda function execution logs in CloudWatch to ensure instances are being shut down as expected.
Conclusion
Automating the shutdown of EC2 and RDS instances using AWS EventBridge and Lambda offers a cost-effective and efficient way to manage cloud resources. By leveraging these tools, organizations can achieve significant cost savings and improve operational reliability.