Invocation types

So far, we've learned that Lambda functions are triggered by events. In the next section we'll show you exactly which events are supported as sources, while in this section, we're going to explore the different ways you can invoke a function.

At a high level, Lambda functions can be invoked using the following:

  • A push
  • An event 
  • A stream-based model

The push model is a synchronous type of invocation where you make a request for the function to be invoked and then wait for the function to come back with a response. You would typically use this when you need an answer right away, and you might expect some meaningful data in the response. An example of this would be when, paired with a GET method in API gateway, we need the Lambda to be the backend processor to fetch and return the data. The caller or requester of the HTTP GET method is expecting a 200 response with some data:

Invoking a function with an API gateway event

The asynchronous or event method for invoking functions is mainly used by other services in the AWS ecosystem to prompt a lambda function to fire as a result of an action on that service.

For example, when a new file is uploaded into an S3 bucket, you might want to invoke a function to perform some processing or metadata collection on the file. Within S3, you would create an event trigger on the S3 PutObject API action. The key thing here is that it is actually the S3 service itself that invokes the Lambda instead of your own code. This is why the resource policies that we learned about in the previous section exist  so that permissions can be granted to that service:

Invoking a function with a PutObject event from S3

The stream-based invocation model resembles a mapping between Lambda and an event stream, such as Kinesis or DynamoDB streams. In a way, this is a type of pull model as well, but it's not only polling that's involved here. To explain this model, we will use the Kinesis Data Streams service.

As events are produced or published on a shard in a Kinesis stream, the Lambda service is polling the stream. Once a preconfigured number of messages has been reached, the Lambda service invokes your function and passes the batch of messages, along with the event object, so that the function can consume and process them:

AWS Lambda polling for new messages on a stream

You can specify the invocation type as a parameter when calling the Lambda through the API or SDKs. The options you can use are as follows:

  • RequestResponse: Use this to invoke the function synchronously and when you expect a response. The connection is kept open until the function returns a payload or terminates unexpectedly.
  • Event: Use this to invoke the function asynchronously. The invoke API call will respond with 202 Accepted, meaning that everything's going well.
  • DryRun: Use this type to check that all of your input parameters are valid and that your IAM user or role has the correct permissions to invoke the function. This will return an HTTP 204 No Content response if validated; otherwise, you will receive a 4xx or 5xx code that describes the problem.

The following is a simple Lambda function that invokes another Lambda function using the RequestResponse invocation type, and then logs the response. Don't worry too much about the syntax for now; we'll explain that very soon! 

const aws = require('aws-sdk');
const lambda = new aws.Lambda();

exports.handler = (event, context, callback) => {
let params = {
FunctionName: mySecondFunction,
InvocationType: 'RequestResponse',
Payload: JSON.stringify(event)
};

lambda.invoke(params, (e, data) => {
if(e) callback(e);
else {
console.log(data.Payload);
callback(null, JSON.parse(data.Payload));
}
});
}

This Node.js function imports the AWS SDK and uses the Lambda.invoke API action to invoke a function. The function that it invokes is specified in the params.FunctionName parameter. We are also passing some arbitrary data to the function being invoked with the params.Payload parameter. 

This is an example of invoking a function using the AWS SDK. Next, we will learn about what other methods there are.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.235