Introduction:
We’ll explore cost optimization strategies using AWS Lambda. Our goal here is to optimize costs by identifying and managing unused Elastic IPs (EIPs) and sending email notifications about them using the Simple Email Service (SES). In this use case, we’ll create a Lambda function and schedule it to run periodically. The Lambda function will find unused EIPs and send this information via email to a predefined list of recipients.
Let’s dive into the detailed steps required to achieve this cost-saving strategy.
Step 1: Creating an IAM Role for Lambda Function
Before we create the Lambda function, we need to set up an Identity and Access Management (IAM) role with the necessary permissions. This role will grant our Lambda function the required permissions to interact with AWS services.
1.1. Create a Policy with Required Permissions:
- To create this policy, navigate to the AWS Management Console and click on “IAM.”
- Click on “Policies” and then “Create Policy.”
- We’ll need three statements in this policy: Statement 1: Describe Elastic IPs
- Action: ec2:DescribeAddresses
- Resources: All resources Statement 2: Send Emails using SES
- Action: ses:SendEmail
- Resources: All resources Statement 3: Access CloudWatch Logs
- Action: logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents
- Resources: All resources
1.2. Review and Create the Policy.
Step 2: Creating an IAM Role
Now, let’s create an IAM role and associate the previously created policy with it.
2.1. Navigate to “Roles” in the IAM console.
2.2. Click “Create Role,” and select “Lambda” as the service.
2.3. For permissions, filter for “Customer managed policies” and select the policy we just created.
2.4. Continue through the steps, providing a role name and description as needed.
2.5. Review and create the role.
Step 3: Creating the Lambda Function
3.1. Create a new Lambda function from scratch.
3.2. Choose the runtime (e.g., Python 3.8).
3.3. For permissions, use the existing IAM role that we just created (UnusedIPsRole).
Step 4: Writing the Lambda Function Logic
4.1. Now, let’s write the Python code for the Lambda function. We’ll use the boto3
library to interact with AWS services.
import boto3
# Get EC2 and SES clients
ec2_client = boto3.client('ec2', region_name='your-region')
ses_client = boto3.client('ses', region_name='your-region')
# Initialize a list for unused Elastic IPs
unused_ips = []
# Describe Elastic IPs
response = ec2_client.describe_addresses()
# Loop through the returned addresses
for address in response['Addresses']:
if 'InstanceId' not in address:
unused_ips.append(address['PublicIp'])
# Send an email with the list of unused IPs
from_email = 'your-from-email@example.com'
to_email = 'your-to-email@example.com'
subject = 'Unused Elastic IPs Report'
body = '\n'.join(unused_ips)
ses_client.send_email(
Source=from_email,
Destination={'ToAddresses': [to_email]},
Message={'Subject': {'Data': subject}, 'Body': {'Text': {'Data': body}}},
)
4.2. Ensure that the Lambda function has the necessary environment variables for from_email
and to_email
, which are the sender and recipient email addresses.
Step 5: Testing the Lambda Function
5.1. Before testing, ensure that the sender’s email address has been verified in SES.
5.2. Test the Lambda function to confirm that it retrieves and sends the list of unused Elastic IPs to your specified email address.
Step 6: Scheduling the Lambda Function
6.1. To make this cost optimization process automated, let’s schedule the Lambda function to run at specific intervals.
6.2. Configure a CloudWatch Events rule with a scheduled expression that fits your desired frequency (e.g., monthly or weekly).
6.3. Enable triggers, and you’re all set! The Lambda function will now run periodically, identifying unused Elastic IPs and sending email notifications to the specified recipients.
Conclusion:
By following these steps, you can implement an automated cost optimization strategy in AWS using Lambda. This approach helps you identify and manage unused Elastic IPs, ultimately reducing unnecessary expenses and improving your AWS resource utilization. Cost optimization is an essential aspect of cloud management, and Lambda functions offer a powerful way to achieve it.