How AWS Lambda Invoke works?

AWS Lambda Invoke


AWS Lambda Invoke

AWS Lambda Invoke

Lambda functions can be invoked directly using:

  • Lambda console
  • Lambda API
  • AWS SDK
  • AWS CLI
  • AWS toolkits

+ Or by configuring other AWS services for the sake of invoking your function

+ Or by configuring Lambda to start reading from a stream or queue then invoking your function

Upon invoking a function, you will be given the chance to choose between invoking synchronously or asynchronously.

Synchronous: awaiting the function to finish processing the event and then returning a response.

Asynchronous: Lambda will queue the event to get it processed and then it will return a response directly. Lambda will also handle retries and is capable of sending invocation records to a specific destination.

One or more triggers may be added for the sake of utilizing a function for processing data automatically.

Trigger: Lambda resource or one which is in a different service which may be configured to start invoking a function as a response to lifecycle events + external requests + a schedule. A number of triggers may be obtained by a function. Every one of those triggers will be acting as a client who invokes the function in an independent manner. Every event passed by Lambda to a function will merely obtain data from a single client or trigger.

For processing items while using a stream or queue, it’s possible to start by creating an event source mapping.

Event source mapping: Resource found in Lambda which is capable of reading items from an SQS queue + Kinesis stream + DynamoDB stream, and later on sending them to the function in forms of batches. Every one of those event which a function has is capable of containing hundreds or thousands of items.

Invoking a function directly: Other AWS services and resources. Every service will differ in its method used for invoking a function, structure of an event, and the way of getting it configured.

Who and how a function gets invoked, will affect and alter the scaling behavior along with the types of errors which may occur.

Synchronously invoking a function: receiving errors in the returned response and it can retry.

AWS Lambda Invoke - asynchronous demo

AWS Lambda Invoke – asynchronous demo

Asynchronously invoking a function: utilizing an event source mapping – configuring a different service for the sake of invoking this function, make the retry requirements and the ways of scaling for handling a huge variety of events may differ.

Requirements for AWS Lambda Invoke:

For invoking a function asynchronously, you will need to set InvocationType to Event.

Synchronous invocation includes in the response body and headers: details describing a function’s response, such as errors.

In case of an error occurring, the function is capable of being invoked various times. This Retry behavior differs and changes according to:

– Error type

– Client

– Event source

– Invocation type

AWS Lambda Invoke - asynchronous function execution result

AWS Lambda Invoke – asynchronous function execution result

Asynchronous invocation:

Lambda will be adding events to the queue prior to going ahead and sending those events straight to the function. In case this function lacks the necessary capacity for keeping up with the queue, it may result in loss of events. At an occasional manner, a function might start receiving the same event a couple of times, regardless of an error not occur. For the sake of retaining the events which fail to get processed, you will need to configure the function using a dead-letter queue.

The Syntax of a Request

POST /2015-03-31/functions/FunctionName/invocations?Qualifier=Qualifier HTTP/1.1

X-Amz-Invocation-Type: InvocationType

X-Amz-Log-Type: LogType

X-Amz-Client-Context: ClientContext

 

Parameters for the URI Request

AWS Lambda Invoke - parameters for URI request

AWS Lambda Invoke – parameters for URI request

  • ClientContext

Data of the invoking client having Up to 3583 bytes of base64-encoded for being passed to the function which is found in the context object.

  • FunctionName

Name of the Lambda’s:

– Function

– Version

– Alias

Formats of the Name

  • Name of the Function: my-function (name-only), my-function:v1 (with alias).
  • ARN of the Function: arn:aws:lambda:us-west-2:123456789012:function:my-function.
  • Partial ARN of the function: 123456789012:function:my-function.

Length constraint: only to full ARN.

Function name: max of 64 characters.

Constraints of Length: Min length being 1. Max length being 170.

The Pattern: (arn:(aws[a-zA-Z-]*)?:lambda:)?([a-z]{2}(-gov)?-[a-z]+-\d{1}:)?(\d{12}:)?(function:)?([a-zA-Z0-9-_\.]+)(:(\$LATEST|[a-zA-Z0-9-_]+))?

  • InvocationType

Select one of the below options.

  • RequestResponse (by default): Invoking the function in a synchronous manner. The connection must remain open till the function gets a response returned or simply  times out. API response will contain the function response + more data.
  • Event: Asynchronously invoking the function. Sending events failing various times to the function’s dead-letter queue (in case of having it configured). API response merley has a status code.
  • DryRun: Validation of the values of parameter and verifying the user or role is granted permission for the sake of invoking the function.

Valid Values: Event | RequestResponse | DryRun

  • LogType

Selected as Tail for including the execution log as information in the response.

Values than are valid: None or Tail

  • Qualifier

Set a version or alias for invoking a published version of a function.

Constraints for Length: Min length 1. Max length 128.

The Pattern: (|[a-zA-Z0-9$_-]+)

 

  • Payload

JSON which you’ll be providing to the Lambda function as an input.

The Response Syntax

HTTP/1.1 StatusCode

X-Amz-Function-Error: FunctionError

X-Amz-Log-Result: LogResult

X-Amz-Executed-Version: ExecutedVersion

 

  • StatusCode

HTTP status code must be in the 200 range for getting a successful request. A RequestResponse invocation type has a status code of 200. An Event invocation type has a status code of 202. A DryRun invocation type has a status code of 204.

Response HTTP headers:

  • ExecutedVersion

The version of the function that executed. When you invoke a function with an alias, this indicates which version the alias resolved to.

Length Constraints: Minimum length of 1. Maximum length of 1024.

Pattern: (\$LATEST|[0-9]+)

  • FunctionError

If present, indicates that an error occurred during function execution. Details about the error are included in the response payload.

  • LogResult

Last 4 KB of an execution log [base64 encoded].

Response HTTP body:

  • Payload

This is a response coming from the function, or from a specific error object.

aws lambda scaling