Voiced by Amazon Polly |
Overview
As organizations scale their operations on AWS, understanding and managing costs become crucial. AWS provides a robust tool called AWS Cost Explorer that allows users to analyze, visualize, and understand their AWS costs. In this blog post, we’ll explore leveraging AWS Lambda functions in Python to retrieve costing information for all services using the AWS SDK for Python (Boto3) and AWS Cost Explorer.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
- AWS Lambda
With the serverless compute service provided by AWS Lambda, you may run code without setting up or maintaining servers. It is perfect for event-driven systems, where several AWS services can trigger functions.
- AWS Cost Explorer
AWS Cost Explorer is a service that offers a complete set of tools for managing your AWS costs. It enables you to visualize, understand, and manage your AWS spending with features such as cost breakdowns, forecasts, and custom reports.
Steps to Create AWS Lambda Function
- Setting Up the AWS Lambda Function
Start by creating a new AWS Lambda function in the AWS Management Console. Choose Python as the runtime and configure the required permissions, such as AWS managed Billing Policy for accessing Cost Explorer.
- Writing the AWS Lambda Code
The Python code for the AWS Lambda function will use Boto3 to interact with the AWS Cost Explorer API. Below is a basic example to get you started:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
## import boto3 import datetime import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): try: # Set up Boto3 Cost Explorer client ce_client = boto3.client('ce') # Get the current date and one month ago end_date = datetime.datetime.now() start_date = end_date - datetime.timedelta(days=30) # Convert dates to string format start_date_str = start_date.strftime('%Y-%m-%d') end_date_str = end_date.strftime('%Y-%m-%d') # Query for total costs by service response = ce_client.get_cost_and_usage( TimePeriod={ 'Start': start_date_str, 'End': end_date_str }, Granularity='MONTHLY', Metrics=['UnblendedCost'], GroupBy=[ { 'Type': 'DIMENSION', 'Key': 'SERVICE' }, ] ) logger.info(f"Cost and Usage of Services: ${str(response)}") # Extract and print costing information for result in response['ResultsByTime']: for group in result['Groups']: service_name = group['Keys'][0] cost_amount = group['Metrics']['UnblendedCost']['Amount'] print(f"Service: {service_name}, Cost: ${cost_amount}") logger.info(f"Service: {str(service_name)}, Cost: ${str(cost_amount)}") return { 'statusCode': 200, 'body': 'Costing information retrieved successfully.' } except Exception as e: logger.error(f"Error - {str(e)}") return { 'statusCode': 500, 'body': f'Error: {str(e)}' } |
This AWS Lambda function queries the AWS Cost Explorer API for the total costs by service for the last 30 days and prints the results.
- Configuring Environment Variables
Consider using environment variables to store parameters like the time range and granularity to enhance flexibility. This allows you to modify these values without changing the code.
Testing and Execution
- Test the AWS Lambda Function Locally: Before deploying, you can test the AWS Lambda function locally using the AWS SAM CLI or other testing frameworks.
- Deploy the AWS Lambda Function: Deploy the function to AWS Lambda using the AWS Management Console or tools like AWS CLI or AWS SAM CLI.
- Check Execution Logs: Once deployed, you can check the Amazon CloudWatch Logs for the Lambda function to view the output and any potential errors.
- Set Up Schedule: In the AWS Lambda function configuration, set up an Amazon CloudWatch Events or Amazon EventBridge trigger to schedule the function at desired intervals.
Conclusion
As cloud infrastructure continues to evolve, having automated cost analysis tools becomes increasingly critical, and the combination of AWS Lambda and AWS Cost Explorer offers a versatile and scalable solution.
Drop a query if you have any questions regarding AWS Cost Explorer and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is an official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, AWS EKS Service Delivery Partner, and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best-in-industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. What time period does the AWS Lambda function consider for cost analysis, and is it customizable?
ANS: – As provided in the example, the AWS Lambda function analyzes costs for the last 30 days. However, the time period is customizable. The start_date and end_date variables in the code can be adjusted to specify the desired time range for cost analysis.
2. How is the AWS Lambda function triggered to run periodically?
ANS: – The AWS Lambda function can be scheduled to run periodically using Amazon CloudWatch Events. Users can configure an Amazon CloudWatch Events trigger for the AWS Lambda function and set the desired schedule, allowing the function to execute at specified intervals (e.g., hourly, daily, weekly, monthly).
3. Can this AWS Lambda function be adapted for more granular cost analysis or specific services?
ANS: – Yes, the AWS Lambda function’s parameters can be customized to fetch more granular cost data or focus on specific services. The Granularity parameter in the get_cost_and_usage call can be adjusted, and additional parameters can be included based on the requirements for detailed analysis.
4. How often should I schedule the AWS Lambda function to run for effective cost monitoring?
ANS: – The scheduling frequency depends on your organization’s needs. Hourly, Daily, Weekly, or Monthly schedules are common for regular cost monitoring. Consider the frequency that aligns with your cost management goals and the granularity required for analysis.
WRITTEN BY Raghavendra Santosh Kulkarni
Raghavendra is a skilled Full Stack Developer with expertise in a wide range of technologies. He has a strong working knowledge of AWS and is always looking to learn about new and emerging technologies. In addition to his technical skills, Raghavendra is a highly motivated and dedicated professional, committed to delivering high quality work.
Click to Comment