Amazon Lambda Connector 1.0 Examples - Mule 4

This example shows how to use the Anypoint Connector for Amazon Lambda (Amazon Lambda Connector) to invoke a synchronous call and get results from AWS Lambda.

Prerequisites

Verify that Amazon Lambda Connector is added to your Mule project in Anypoint Studio. If it’s not, follow the instructions in Add the Connector to Your Mule Project.

Synchronous Invocation Flow

This example shows a simple AWS Lambda function call that is invoked using an HTTP listener. When the http://localhost:8081/invoke URL is called, the Lambda Invoke operation is performed, and the result is returned directly to you.

Create the Configuration File

  1. In Studio, go to the Package Explorer view and right-click the /src/main/resources folder in your project.

  2. Select New > File.

  3. Name the file environment.properties.

  4. Edit the file to define the properties and values you need.

Configure HTTP Listener

Configure an HTTP Listener source to initiate the Amazon Lambda Connector Invoke operation flow each time it receives a request on the configured host (localhost) and port (8081):

  1. In Mule Palette, select HTTP > Listener.

  2. Drag Listener to the Studio canvas.

  3. Click + next to Connector configuration to configure a global element that can be used by all instances of the HTTP Listener source in the app.

  4. In the General tab, specify connection information for the connector.

  5. Click Test Connection to confirm that Mule can connect with the specified server.

  6. Click OK.

Configure the Invoke Operation

  1. In Mule Palette, select Amazon Lamdbda Connector > Invoke.

  2. Drag Invoke to the Studio canvas to the right of the Listener source:

    Simple invoke example flow in Studio
  3. Configure the Invoke operation:

    Simple invoke operation General properties configuration

    In this example, the function name is supplied in the environment.properties file as $config.functionName.

    The payload from the HTTP Listener is passed directly into the body of the Invoke operation, which is specified as REQUEST_RESPONSE. This tells AWS Lambda that it does not need to store any logs for this operation and effectively makes this a synchronous call, which waits until the AWS Lambda function is finished processing before it returns its results.

    Alternatively, you can set Invocation Type to EVENT, which acts like an asynchronous call and returns an empty response immediately while AWS Lambda performs its action in the background.

Create an AWS Lambda Function

Depending on your Lambda function, when you run this flow, you might need to send in a request body. For example, you can create a Python 3.8 Lambda function like this:

def lambda_handler(event, context):
    message = 'Hello {} {}!'.format(event['first_name'], event['last_name'])

	return {'message' : message}

Consult the official AWS documentation if you don’t know how to create a new AWS Lambda function.

This function takes the fields first_name and last_name from the input in JSON format and sends a greeting using the specified first and last names.

Call the http://localhost:8081/invoke endpoint with a JSON body like this:

{
    "first_name":"<first_name>",
    "last_name":"<last_name>"
}

The Mule flow responds with a Code 200 message:

{
    "message": "Hello <first_name> <last_name>!"
}

XML for the Invoke Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule app:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:amazon-lambda="http://www.mulesoft.org/schema/mule/amazon-lambda" xmlns:http="http://www.mulesoft.org/schema/mule/http"
	xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/amazon-lambda http://www.mulesoft.org/schema/mule/amazon-lambda/current/mule-amazon-lambda.xsd">
	<flow name="amazon-lambda-invoke-example">
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/invoke"/>
		<amazon-lambda:create20150331-functions-invocations-by-function-name doc:name="Invoke" config-ref="Test_Aws_Config" functionName="${config.functionName}" xAmzInvocationType="REQUEST_RESPONSE">
		</amazon-lambda:create20150331-functions-invocations-by-function-name>
	</flow>
</mule>