Monitoring Long Running Fargate Tasks
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.
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.
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.
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.
Create an SNS topic that will be used by your lambda to get alerts:
- Go to SNS.
- Create an SNS with your name and then add a subscription to your email address where you want to receive the alerts.
- Here we can also integrate it with Slack which we have discussed in the Bonus section.
Create a lambda Function:
- Go to AWS Lambda.
- Click on Create Function.
- Create a Python lambda.
- Use the default settings and click Create Function.
- Copy and paste this code into the code section.
6. Now, Click on Configuration and then set environment variables.
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:
Create an EventBridge Rule:
- Go to Cloudwatch > Events > Rules.
- Create Rule.
- Give the Rule Name.
- Select the schedule and click Next.
5. Define the schedule for example here we will be running it every 15 mins.
6. Click Next and select the lambda you want to trigger in the Select Targets menu. You can add multiple targets if you want.
7. Review the config and click Create rule. Your rule will be created.
Integration with Slack
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.