Can a lambda be invoked by an ECS task? - amazon-web-services

I currently have a service running with ECS + Fargate. Now I want to create a lambda, which would be triggered synchronously by an ECS task in the previous service.
Is it possible to trigger a lambda by an ECS task? I have come across a documentation in which Lambda is used to invoke ECS, but not the other way around.

"You can invoke Lambda functions directly with the Lambda console, the Lambda API, the AWS SDK, the AWS CLI, and AWS toolkits." So you'll have to decide which method is going to work best for your ECS tasks. You will need to set the correct permissions so that your ECS will be able to invoke the Lambda function.

If you want to invoke Lamda in an async manner, you can publish a message to SQS from your ECS task and SQS can trigger the Lamda on message receive.

Related

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

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

AWS Lambda invoking Fargate task

I am new to AWS and experimenting with AWS Lambda and Fargate. I have a long-running process that I have defined as an AWS Fargate containerized task. This task is triggered using ecs.runTask(taskParams, callback) api call from an AWS Lambda function. The Lambda function is triggered by a notification of a file uploaded into an S3 bucket.
You can now configure your AWS Lambda functions to run up to 15
minutes per execution. Previously, the maximum execution time
(timeout) for a Lambda function was 5 minutes. (Source: Amazon)
My question is, does ecs.runTask() run the task asynchronously inside an on-demand container without the lambda function that triggered it waiting for its completion? Does that explain how the lambda function is no longer bound by the task running time? Is this a recommended approach for long-running processes where we don't want an ECS instance just around?
Finally, what is the difference between ecs.runTask() and ecs.startTask() api calls?
asynchronously inside an on-demand container without the lambda function that triggered it waiting for its completion?
Yes. Lambda will just start it.
what is the difference between ecs.runTask() and ecs.startTask() api calls?
startTask can be only used on EC2 launch type and requires you to explicitly choose which EC2 instance to use for your task. Can't be used for Fargate and allows you to launch a single task.
runTask can be use for both EC2 and Fargate launch types. When you use runTask ECS decides where to place your tasks, e.g. which instance. Also, you can run multiple copies of a single task at once.
There are probably more differences, but I think the above are the key ones.

How can I make a Redis event on ElastiCache trigger a Lambda function?

I have a small function I want to move from a dedicated EC2 instance to Lambda. This function is currently triggered by a broadcast from a Redis instance in ElastiCache. How can I make a Redis broadcast trigger a lambda function?
Someone frome AWS said that this could be done through CloudWatch, but the only ElastiCache event I can find is "AWS API Call via CloudTrail".
If your use Redis as a queue, try SQS instead. SQS can trigger Lambda. ElastiCache doesn't create events or log entries similar to broadcast events.

Need to programmatically create amazon Lambda schedule triggers

I need to be able to programmatically create amazon lambda schedule triggers. Like to execute a function every five minutes. I can easily do it with a console, but I need many in different environments, so I need to do it in a script. Java or python or even a cli call will do.
Any ideas?
Thanks
You can use Amazon CloudWatch Events to achieve it.
Take a look at AWS Lambda documentation. These pages show you how to trigger a Lambda function on a schedule:
Using AWS Lambda with Scheduled Events
Run an AWS Lambda Function on a Schedule Using the AWS CLI
You can use this package in Python to run AWS CLI commands.

How can I create a Scheduled Event for a lambda function using the AWS CLI?

The AWS CLI does not have an option to schedule a lambda function. This is possible via the AWS console right now.
Any ideas on how I can do this?
aws lambda create-event-source-mapping # does not support scheduling events
It is not possible to use the API to create a schedule event sources with AWS Lambda at this time. That means it is not possible to use the AWS CLI to create the schedule. It is also not possible to use CloudFormation to schedule a AWS Lambda function.
Unfortunately using the GUI is the only option until AWS release an API.
We use Lambda to create print-ready file: http://blog.peecho.com/blog/using-aws-lambda-functions-to-create-print-ready-files