Author: Saddam Shaikh
In this blog, we will learn how to call the AWS Lambda function in MuleSoft.
What is AWS Lambda?
AWS offers serverless computing through Lambda functions. The concept of “serverless” computing means customers don’t have to manage the server/infrastructure to run these functions. AWS Lambda is used in file processing, stream processing, web applications, mobile backends and other similar applications.
MuleSoft Amazon Lambda Connector
Amazon Lambda Connector allows us to execute AWS Lambda functions within Mule flows.
Prerequisite:
- Anypoint Platform Account
- Anypoint Studio 7.6 or later
- AWS Identity & Access Management (IAM) access key. You can check this article to generate an access key.
AWS Lambda Configuration
Follow the below steps to create the AWS Lambda function in python
1. Search AWS Lambda in AWS Search Console
2. Click on Create a function.

3. Enter the function name and choose Runtime in which you are going to write the Lambda function. As of now, Lambda supports Python, Java, .NET, Node.js, etc as Runtime. You can check article to know what all languages Lambda supports. I have selected Python 3.9 as Runtime. Click on Create function.

4. Write a lambda function in python that takes input as country name and returns information about that country

Python Code
import jsonimport urllib.errorfrom urllib.request import urlopen
def lambda_handler(event, context): countryName = event["name"] url = "https://restcountries.com/v3.1/name/" + countryName + "?fullText=true" method = "GET"
httprequest = urllib.request.Request(url, method=method)
try: with urllib.request.urlopen(httprequest) as httpresponse: response = { "status": httpresponse.status, "body": json.loads(httpresponse.read().decode()), } except urllib.error.HTTPError as e: response = {"status": e.code, "body": json.loads(e.read().decode())}
return response
5. Deploy the Lambda function by clicking Deploy button
6. Configure a new test by selecting “Configure test event” from Test option

Test Configuration for Japan

Lambda function execution result
We have completed the Lambda configuration in AWS. Let’s check how to call this Lambda function in MuleSoft code.
MuleSoft Code Configuration
1. Download Amazon Lambda Connector from Anypoint Exchange. (version = 1.0.4)
2. Add Amazon Lambda Connector Config in Global Elements. Provide the following details in the config
Access Key: AWS access key
Secret Key: AWS secret key
Region: Region of AWS instance

Amazon Lambda Connector Configuration
3. Create a mule application to execute the AWS Lambda function using AWS Lambda Connector Invoke operation. Configure the following parameters in Invoke operation
Function Name: Lambda function Name or ARN
X Amz Invocation Type: Set REQUEST_RESPONSE to invoke the function synchronously
X Amz Log Type: To exclude the execution log in the response, set X Amz Log Type to NONE
Body: Request to Lambda function

Invoke Operation Configuration
4. Test flow using Postman

Postman Snapshot
I hope you find this article helpful. Thanks for reading.
References: