Lädt...


🔧 Monitoring AWS Lambda Costs with Terraform.


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Case Study

Imagine you have a team that uses various cloud services from AWS, including AWS Lambda to run serverless functions. As the team progresses in their projects, they start noticing that their AWS costs are increasing. However, they are unsure which of their Lambda functions is causing most of these costs.

Challenge

The challenge here is to identify which Lambda function is consuming the most resources and, therefore, generating unnecessary costs. This can be complicated, especially if you have multiple functions and lack a proper monitoring system. Without effective monitoring, it’s easy to lose track of costs and performance metrics.

Monitoring Solution

To tackle this problem, we can implement a monitoring solution using Terraform, Amazon CloudWatch, and Amazon SNS. This will not only help you identify the functions generating excessive costs but also allow you to receive real-time notifications about any anomalies.

Project:

Terraform Setup

First, make sure you have Terraform installed on your machine. You can download it from the official Terraform website. If you are a Mac user, this video will guide you on how to install Terraform on your computer:

Code Overview for Deploying the CloudWatch Dashboard using Terraform:

resource "aws_cloudwatch_dashboard" "lambda_cost_dashboard" {
  dashboard_name = "LambdaCostDashboard"
  dashboard_body = jsonencode({
    widgets = [
      {
        type = "metric",
        x = 0,
        y = 0,
        width = 24,
        height = 6,
        properties = {
          metrics = [
            ["AWS/Lambda", "Duration", "FunctionName", "${aws_lambda_function.my_lambda.function_name}"],  # Replace with your Lambda function name
            ["AWS/Lambda", "Cost", "FunctionName", "${aws_lambda_function.my_lambda.function_name}"]
          ],
          title = "Lambda Costs and Duration",
          period = 300,
          stat = "Average",
          view = "timeSeries",
          region = "us-east-1"
        }
      }
    ]
  })
}

resource "aws_cloudwatch_metric_alarm" "lambda_cost_alarm" {
  alarm_name          = "HighLambdaCost"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "15"
  datapoints_to_alarm = "15"
  metric_name        = "Duration"
  namespace          = "AWS/Lambda"
  period             = "60"
  statistic          = "Average"
  threshold          = "25000"  # Change this threshold as needed
  treat_missing_data = "missing"
  dimensions = {
    FunctionName = "${aws_lambda_function.my_lambda.function_name}"  # Replace with your Lambda function name
  }

  alarm_description = "Alarm for high Lambda costs"
  alarm_actions     = [aws_sns_topic.lambda_cost_notifications.arn]
  insufficient_data_actions = []
}

This code will create a dashboard in CloudWatch that allows you to visualize the costs and duration of your Lambda function, along with an alarm that will notify you if the cost exceeds the defined threshold. You will also receive email notifications via SNS.

CloudWatch

It is important to note that the email subscribed to Amazon SNS (Simple Notification Service) must be valid and confirmed to receive alerts. This means that if a message is sent to an SNS topic with a pending confirmation endpoint, it will not work as expected until the endpoint is confirmed.

You should look for a subscription confirmation email or check the topic in SNS to ensure that the subscription has been confirmed. Without this confirmation, the dashboard will not function properly, as alerts will not be sent.

Confirmation

Resource Optimization

It is crucial to optimize the memory configuration of your Lambda functions. It is recommended to start with at least 512 MB in most cases. This strategy not only improves performance but also helps minimize costs. Adjusting memory settings can significantly impact operational efficiency and the costs associated with using your Lambda functions.

Conclusion

Implementing a monitoring system for your AWS Lambda functions is essential for controlling costs and optimizing performance. By using Terraform, CloudWatch, and SNS, you can automate the creation of dashboards and alarms that will help you quickly identify any function generating excessive costs. With this solution, you will be better positioned to manage your cloud resources and ensure that your AWS Lambdas operate efficiently and cost-effectively.

Here you will find the GitHub repository containing the code and instructions.

GitHub Repository

Ready to streamline your AWS deployments?

Start using Terraform to manage your infrastructure as code. Experiment with the provided code and feel free to share your experiences in the comments. If you liked this article, follow me for more content on AWS and Terraform.

Your participation is valuable, and I would love to hear from you.

...

🔧 Monitoring AWS Lambda Costs with Terraform.


📈 47.91 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 5 Introduction to AWS Lambda Web Adapter


📈 35.5 Punkte
🔧 Programmierung

🔧 How to Import Pandas(library) in AWS Lambda Functions - AWS Lambda Layers


📈 35.5 Punkte
🔧 Programmierung

🔧 Enhancing AWS Lambda with AWS Lambda Powertools: A Complete Guide to CRUD Operations in DynamoDB


📈 35.5 Punkte
🔧 Programmierung

🔧 Spring Boot 3 application on AWS Lambda - Part 6 Develop application with AWS Lambda Web Adapter


📈 35.5 Punkte
🔧 Programmierung

🔧 AWS Cost Management and Optimization: Tips and Tools for Monitoring and Reducing Your AWS Costs


📈 30.92 Punkte
🔧 Programmierung

🔧 Secrets Management in .NET Lambda: AWS SDK vs. Lambda Extension


📈 29.63 Punkte
🔧 Programmierung

🔧 Day 22 of 100 Days of Cloud: Mastering AWS Lambda and Lambda Layers


📈 29.63 Punkte
🔧 Programmierung

🔧 Turbocharge your Lambda Functions with AWS Lambda Powertools for Python


📈 29.63 Punkte
🔧 Programmierung

🔧 Lambda Internals: Why AWS Lambda Will Not Help With Machine Learning


📈 29.63 Punkte
🔧 Programmierung

🔧 Supercharge Your AWS Lambda Game With Lambda Powertools


📈 29.63 Punkte
🔧 Programmierung

🔧 How to optimize your lambda functions with AWS Lambda power tuning


📈 29.63 Punkte
🔧 Programmierung

🔧 AWS Lambda support Node.js 18 now. Should we update the version of Node.js in the Lambda runtime?


📈 29.63 Punkte
🔧 Programmierung

🔧 Techniques to Save Costs Using AWS Lambda Functions with CDK


📈 28.87 Punkte
🔧 Programmierung

🔧 Controlling AWS Lambda Costs


📈 28.87 Punkte
🔧 Programmierung

🔧 How To Reduce Operational Costs With AWS Lambda


📈 28.87 Punkte
🔧 Programmierung

🔧 How to save costs using AWS Lambda SnapStart for Java based functions


📈 28.87 Punkte
🔧 Programmierung

🔧 How to Build a BlueSky RSS-like Bot with AWS Lambda and Terraform


📈 28.74 Punkte
🔧 Programmierung

🔧 Unleashing the Power of Python Lambda Functions with Terraform for Email Automation via AWS SES


📈 28.74 Punkte
🔧 Programmierung

🔧 Automating EC2 Instance Management with AWS Lambda and EventBridge Using Terraform


📈 28.74 Punkte
🔧 Programmierung

🔧 From Code to Cloud with Terraform: EKS Golang Client & E2E AWS Lambda CI/CD with IaC


📈 28.74 Punkte
🔧 Programmierung

🔧 Automate REST API Management with Terraform: AWS API Gateway Proxy Integration and Lambda


📈 28.74 Punkte
🔧 Programmierung

🔧 Creating an Image Thumbnail Generator Using AWS Lambda and S3 Event Notifications with Terraform


📈 28.74 Punkte
🔧 Programmierung

🔧 Simplifying Serverless Architecture with Terraform and AWS Lambda


📈 28.74 Punkte
🔧 Programmierung

🔧 Invoking an AWS Lambda Function with Terraform


📈 28.74 Punkte
🔧 Programmierung

🔧 Terraform, Lambda y Event Bridge: El trío perfecto para la optimización de costos en AWS


📈 28.74 Punkte
🔧 Programmierung

🔧 Terraform, Lambda, and Event Bridge: The perfect trio for AWS cost optimization


📈 28.74 Punkte
🔧 Programmierung

🔧 Creating an AWS Lambda Function with Terraform


📈 28.74 Punkte
🔧 Programmierung

🔧 Building a Free DMARC Monitoring and Reporting Tool with AWS Lambda: The Story of DmarcEye


📈 25.81 Punkte
🔧 Programmierung

matomo