Image for post
Image for post
Photo by freestocks on Unsplash

Expose SOAP Service As REST API -The Serverless Way

In this post, we will go through the steps to expose a SOAP service as a RESTful API using AWS API Gateway and Lambda. The primary driver for such a solution is often incompatibilities of systems involved in the integration. For example: The service client supports REST and JSON, whereas the service provider works solely based on SOAP messages.

To keep things simple, we will refer to a publicly available SOAP based web service accessible here. This calculator service provides simple arithmetic operations like Add, Subtract, Multiply and Divide. We will expose the ‘Add’ operation via a RESTful API. However, this solution could be easily extended to the other operations in the calculator service or to other SOAP services altogether.

Here is a pictorial view of how the solution is perceived to work.

The API client places a REST API call to API gateway which has a Lambda back-end. The Lambda function, in turn, transforms JSON request into a SOAP message and interacts with the SOAP service. A reverse path is followed during the response flow.

Create the Lambda

We will use Node.js to implement the Lambda function and specifically use ‘soap’ library to transform JSON request/response, to/from SOAP. There are of course, other libraries that could be used to achieve the same result. However, this particular library seems to be very simple and yet powerful. The library accepts JavaScript objects and transforms them into SOAP requests, transparently. Needless to say, it also transforms SOAP responses back into plain JavaScript objects.

Working with SOAP requests and responses

Here is a sample source code that leverages the ‘soap’ library, to call the ‘Add’ operation of calculator service. We will refer to this code, in the subsequent sections.

Here’s the snippet of ‘Add’ operation along with input/output message structures from the WSDL. Take note of the elements highlighted. They play a significant role in how the corresponding JavaScript objects will have to be formed.

The trick is to understand the relevant operation and input/output structures appearing in WSDL and accordingly form the corresponding JavaScript object structures. We can clearly see that the ‘Add’ operation, uses ‘AddSoapIn’ as input message and ‘AddSoapOut’ as output messages. ‘AddSoapIn’ message leverages the ‘Add’ complex type which has two elements ‘intA’ and ‘intB’. Thus the corresponding JavaScript object structure would be:

The ‘AddSoapOut’ message leverages ‘AddResponse’ complex type which has ‘AddResult’ element and corresponding JavaScript object structure would be:

In request cycle, transformation of a Lambda event into a JavaScript object structure that can be used by the library to generate appropriate SOAP request message (for Add operation), is done by transformRequest function.

Similarly, in the response cycle, transformResponse function is used to work with JavaScript object returned by the library and transform it into response of Lambda function.

Working with SOAP bindings and execute the operation

By default, the ‘soap’ library doesn’t generate SOAP1.2 headers. As such, if an operation has a SOAP1.2 binding only, we will have to explicitly mention the same in code using an option that is subsequently passed to createClient call (Refer to source code for details). Also we have to explicitly call the ‘Add’ operation on soap client (Remember, ‘Add’ operation was defined in the WSDL) and pass target request returned by transformRequest function

Here’s a code snippet that creates a soap client and then calls the ‘Add’ operation. The soap client also forces SOAP1.2 headers.

Deploy and test the Lambda

Deploy the Lambda function and test it with the following sample event:

The Lambda should return a response like:

Note: We will not cover the deployment process of a Lambda function in this post, since there are several good resources available covering that area.

Create and configure the API

Exposing the Lambda function as a RESTful API is pretty straightforward.

  • Build a REST API from scratch and name it as ‘calculator’
  • Create necessary resources and a GET operation
  • Make sure to select Integration Type as Lambda Function and provide name of Lambda function and click Save. Here will we create a non-proxy Lambda integration.
  • Add necessary permissions to Lambda in the next popup and click OK.
  • In the Method Execution pane of the GET operation, click on Method Request and under ‘URL Query String Parameters’ section, add two query string parameters- ‘a’ & ‘b’ and mark them as required
  • Go back to the Method Execution pane and click on Integration Request.
  • Under ‘Mapping Templates’ section, select ‘Never’ as Request body pass-through mode and add mapping template for application/json content-type.
  • Add the following template in ‘Generate template’ section and click on Save.

Here, essentially we are extracting the values from query-string parameters and creating the necessary JSON request which will be passed as event to Lambda function.

That’s it, we are done. Now let’s deploy the API and test it.

Deploy and test the API

  • From API actions, select Deploy API.
  • Create a new stage and provide a new name to the stage and click on Deploy.
  • Note the invoke URL of the GET operation from Stages
  • Since we have created a simple GET operation, we can directly point our browser to the invoke URL along with required query string (‘a=<number>&b=<number>’)and check response:


This serverless solution could be used with any arbitrarily complex SOAP service. However, maintaining the simplicity, maintainability and security of APIs is a different ballgame and should be dealt with on its own merit.

Written by

Solutions architect by profession, programmer by passion and photographer by choice…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store