Custom mediator to handle AWS Lambda responses in WSO2 API Manager

In this article, I discuss how we can modify responses using the http status code and the payload of AWS Lambda responses in WSO2 API Manager

Binod Karunanayake
4 min readSep 30, 2022

AWS Lambda as an Endpoint feature is very useful when it comes to utilize the benefits of WSO2 API Gateway for AWS Lambda Functions. Now, WSO2 API Manager has limited support for Lambda Proxy Integration and hence, users can pass headers and parameters to Lambda without modifying the payload. For more information, please refer to my article How to pass parameters to AWS Lambda using WSO2 API Manager.

Some users use the format mentioned in Lambda Proxy Integration for the Lambda response which contains a status code as below.

{
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}

However, the response from Lambda function will be passed-through to the user with 200 OK status code in WSO2 API Manager regardless of the statusCode and body attributes in the response. But you can change this default behavior of the response by changing the code as statusCode and the payload as body using a custom mediator.

Step 1: Write the custom mediator

(i) Create a new maven project using following directory layout.

├── pom.xml
└── src
└── main
└── java
└── org
└── example
└── mediators
└── ResponseMediator.java

(ii) Add following code for pom.xml.

(iii) Add following code for ResponseMediator.java.

(iv) Build maven project using mvn clean install and get lambda-proxy-1.0-SNAPSHOT.jar file.

Note: If there are build errors due to unresolved dependencies, try manually copying them to m2 repository from https://mvnrepository.com/

Step 2: Run WSO2 API Manager with the custom mediator

(i) Copy lambda-proxy-1.0-SNAPSHOT.jar file to <API-M_HOME>/repository/components/lib directory.

(ii) Start/Restart WSO2 API Manager.

Step 3: Enable custom mediation in Lambda API

Follow the steps below according to the WSO2 API Manager version. Use following lambda-proxy.xml file as the custom mediation policy.

for 3.2.0

  • Goto Runtime Configurations.
  • Under Response > Message Mediation click on edit button.
  • On the pop up box select Custom Policies and click on the upload button.
  • Select lambda-proxy.xml file and upload it.
  • On the pop up box click on the Select button.
  • Click on Save button at the bottom of the page.

for 4.0.0

  • Goto API Configurations > Runtime.
  • Under Response > Message Mediation click on edit button.
  • On the pop up box select Custom Policies and click on the upload button.
  • Select lambda-proxy.xml file and upload it.
  • On the pop up box click on the Select button.
  • Click on Save button at the bottom of the page.
  • Deploy a new revision.

for 4.1.0

  • Goto API Configurations > Policies.
  • Under Policy List click on Add New Policy button.
  • Fill Name, Version and Description with proper values.
  • Select Response as the Applicable Flows.
  • Select lambda-proxy.xml file and upload it.
  • Click on Save button.
  • Drag and drop the policy that you newly created from Response Policy List to response flow of the resource.
  • Click on Save button at the bottom of the page.
  • Deploy a new revision.

Note: Custom mediation affects only the API that you define the mediation policy. So, if you need the same behavior in other APIs you have to follow Step 3 again for them.

Step 4: Test invoking Lambda function

Try invoking Lambda function/s with different http status codes in the response. You can see the same status code is set to the response coming from WSO2 API Gateway as well. Also, the payload will be as same as the content of body attribute.

without the mediator:

with the mediator:

That’s it! Thanks for reading my article.

--

--

Binod Karunanayake
Binod Karunanayake

Written by Binod Karunanayake

PhD Candidate @RMIT University | Former Software Engineer @WSO2 | BSc Engineering (Hons) University of Moratuwa

Responses (1)