3
3
Table of Contents

 

Introduction

The utilization of event-driven architectures has gained popularity due to its ability to tackle the complexities inherent in building intricate systems that modern organizations commonly use. This approach emphasizes the use of microservices, which are small, specialized applications designed to perform a specific set of functions.

This blog post will examine an event-driven architecture and how we are monitoring it to keep track of AWS costs.

Scenario

We are utilizing AWS ECS Fargate to handle event-driven workloads that process substantial quantities of customer data simultaneously for various clients. This presents us with an opportunity to experiment with this type of architecture, which is becoming increasingly popular for various use cases. 

These include facilitating communication between microservices, integrating with third-party SaaS applications, replicating data across regions and accounts, and enabling parallel event processing and fanout.

User-flow:

Architecture

When users submit a request via the UI, the EBS code acts as the primary backend and transmits a message with event attributes such as clientId and jobId to the SNS topic. The AWS Fargate is then called upon by a lambda invocation triggered by the SNS topic to handle these requests.

However, once the AWS Fargate is initiated, our ability to manage it is limited. As a result, to prevent lengthy tasks, we require monitoring and automation measures.

Solution Approach

The following queries arise:

  • How do we monitor lengthy tasks?
  • How do we track expenses?
  • How can we ensure our system's functionality?

To address these concerns, we have implemented a lambda function that is triggered by an event bridge rule every 15 minutes. This function enables us to monitor tasks that run for more than an hour, allowing us to track expenses and ensure that the system is functioning correctly and not losing progress over time.

Solution Steps

Create an SNS topic that will be used by your lambda to get alerts:

  1. Go to SNS.
  2. Create an SNS with your name and then add a subscription to your email address where you want to receive the alerts.
  3. Here we can also integrate it with Slack which we have discussed in the Bonus section.

Simple Notification Service

Create a lambda Function:

  1. Go to AWS Lambda.
  2. Click on Create Function.
  3. Create a Python lambda.
  4. Use the default settings and click Create Function.
  5. Copy and paste this code into the code section.

Create Function Lambda

Fargate-cron-int - Lambda

Code

6. Now, Click on Configuration and then set environment variables.

fargate-alert-cron-int - Lambda

7. Once this is done configure the EventBridge Rule according to the steps described in the next section and you will see a trigger in this lambda as below:

fargate-alert-cron-int - Lambda

Create an EventBridge Rule:

  1. Go to Cloudwatch > Events > Rules.
  2. Create Rule.
  3. Give the Rule Name.
  4. Select the schedule and click Next.

Amazon EventBridge

5. Define the schedule for example here we will be running it every 15 mins.

Amazon EventBridge

6. Click Next and select the lambda you want to trigger in the Select Targets menu. You can add multiple targets if you want.

Amazon EventBridge

7. Review the config and click Create rule. Your rule will be created.

fargate-cron

Amazon EventBridge

Bonus

Integration with Slack

non-prod-fargate-alerts - Scripta Savings - Slack

non-prod-fargate-alerts - Scripta Savings - Slack

Conclusion

In conclusion, utilizing Lambda to track long-running AWS Fargate tasks can result in significant AWS cost savings for organizations. By automating the process of monitoring and terminating tasks, Lambda ensures that resources are only used when necessary, reducing the overall cost of running AWS Fargate tasks. Additionally, Lambda's ability to quickly scale up or down based on demand allows organizations to optimize their resource usage, further reducing costs. Ultimately, incorporating Lambda into AWS Fargate workflows can help organizations improve efficiency, reduce waste, and achieve greater cost savings.

CloudKeeper helps you cost-optimize your entire cloud infrastructure and provides instant and guaranteed savings of up to 25% on your AWS bills. Talk to our experts, to learn more.

12
Let's discuss your cloud challenges and see how CloudKeeper can solve them all!
0 Comment
Leave a Comment

Speak with our advisors to learn how you can take control of your Cloud Cost