AWS, Cloud Computing

3 Mins Read

A Guide to Explore AWS Costs Using AWS Lambda

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
Get Started

Introduction

  1. 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.

  1. 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

  1. 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.

  1. 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:

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.

  1. 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

  1. 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.
  2. 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.
  3. Check Execution Logs: Once deployed, you can check the Amazon CloudWatch Logs for the Lambda function to view the output and any potential errors.
  4. 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

By combining AWS Lambda, Boto3, and AWS Cost Explorer, you can create a powerful solution for programmatically fetching cost details for all AWS services. This not only aids in monitoring expenses but also allows for customized analysis and reporting based on your organization’s specific needs.

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
Get Started

About CloudThat

CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront Service Delivery PartnerAmazon OpenSearch Service Delivery PartnerAWS DMS Service Delivery PartnerAWS Systems Manager Service Delivery PartnerAmazon RDS Service Delivery PartnerAWS CloudFormation Service Delivery PartnerAWS ConfigAmazon EMR and many more.

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.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!