Home > OS >  How to create an event when Amazon Elastic Container Service (Amazon ECS) Fargate task successfully
How to create an event when Amazon Elastic Container Service (Amazon ECS) Fargate task successfully

Time:06-01

I have ECS cluster running fargate services(spring boot applications).

I wanted to run a lambda function which will check if the applications(spring boot) are up and running or not. And this function has to be called after receiving an event of successfully running fargate tasks.

Is there a way to configure such event in Cloudwatch or any other possible solution for this?

CodePudding user response:

A way to write Lambda functions that can interact with AWS Fargate containers is to write an AWS Lambda function that uses the AWS ECS API. You can write the AWS Lambda function in a supported programming language.

I am not 100% clear what you mean by Consuming Services. DO you want your Lambda function to perform tasks on the container such as stopping, starting tasks, etc.

If so, you can write an AWS Lambda function using the Java runtime API and then use the software.amazon.awssdk.services.ecs.EcsClient to achieve your business logic. Using this Service Client, you can perform many ECS tasks and all from within an AWS Lambda function.

On the other hand, if you want your Lambda function to retrieve data from a service running in a container (for example JSON data), you can use programming logic to invoke the service from within the Lambda function and handle the response. No Specific AWS APIs are needed to do this.

Also AWS Cloud Watch is not used to invoke a service running in a container. Its simply a service that logs/monitors data.

CodePudding user response:

You can configure an EventBridge (formerly known as CloudWatch Events) rule to react to an ECS state change by triggering a Lambda. Documentation for setting up the trigger is here, and information about ECS event types is here.

I'm not sure if either of those pages show an actual event rule. Here's one copied from a live system; you'll need to change the account ID and task definition name:

{
  "detail-type": ["ECS Task State Change"],
  "source": ["aws.ecs"],
  "detail": {
    "taskDefinitionArn": [{
      "prefix": "arn:aws:ecs:us-east-1:123456789012:task-definition/MyTaskDefinition:"
    }],
    "lastStatus": ["RUNNING"]
  }
}

Also be aware that for something like a web app, the container might be up and running before the app is ready to handle requests.

  • Related