4

I have written a bunch of Lambda functions that are exposed as Rest endpoints through API Gateway. I have chosen the "Lambda Proxy Integration" since it seemed like a straightforward way to get started.

Now I want to chain together 2 of these functions via AWS Step Functions. The general integration and configuration works fine except how to create the proper inputs for each task.

Using the console I can start an Execution and give the following JSON:

{
    "headers": {
        "Authorization": "Bearer 12345"
    },
    "body": "\"some\": \"json\"",
    "queryParameters: {
        "more": "here"
    }
}

This is how the inputs to my Lambda functions look like since I'm using the Lambda Proxy Integration everywhere.

The output looks something like this:

{
  "isBase64Encoded": false,
  "statusCode": 200,
  "headers": {
    "Access-Control-Allow-Origin": "*"
  },
  "body": "{\"message\":\"Great\"}"
}

This is also fine stand-alone, API Gateway maps these infos back to proper HTTP return codes and responses and all.

Now: how do I create these input JSONs when using Step Functions. The very first input is easy using the console, of course. But how do I create the next input and mix in a part of the previous output? Some of the problems in bullet points:

  • Using InputPath, ResultPath and OutputPath I can only seem to use the "whole" output of a previous step as input or part as the input for the next step. But I can't use only a part of the output, in my case the element "body" of the response.
  • This element "body" is escaped anyways so I guess I would need to un-escape it before using it somehow for the next input? But how?
  • The input JSON needs to consist of elements like "headers", "body" or "queryParameters" that don't appear at all in the previous output. How do I create those?

I'm wondering whether Step Functions just don't really work with Lambdas built for the Lambda Proxy Integration. Is that the case? How are people using Step Functions without running into these problems?

hendrikbeck
  • 630
  • 6
  • 13

1 Answers1

0

Step function is designed to integrate directly with lambda and not via api gateway, that’s the reason why step functions doesn’t handle it escaped Jain naturally.

If you want to have your lambda code accessible via both api gateway and step function, I would recommend following: Split the lambda code logic into 2 parts I.e. core logic and the wrapper over core logic that basically performs the functionality of extracting out the fields from body and unescaping it. This way your api gateway can invoke the wrapper lambda and you step function can invoke the core logic lambda. With this design you would be able to achieve your goal. Moreover, you can define the lambdas as part of one cfn and code package which will help in easy maintenance.

I hope this answers your questions.

Thanks

Udit Bhatia
  • 525
  • 1
  • 5
  • 14
  • Interesting suggestion. So you're saying the Lambda function should be able to deal with both, integration with API Gateway and direct integration into something like Step Functions? From a code point of view that is doable, I guess. It just doesn't feel 100% right that the Lambda function needs to be aware of how it is called. Feels like I should be looking into not doing the Lambda Proxy Integration on the API Gateway anymore and instead look at rewrite rules and parameter mappings instead to forward requests from API Gateway to Lambda functions properly... – hendrikbeck Sep 18 '18 at 08:46
  • 1
    I the above proposal Lambda function is not aware of the caller. Basically there are 2 lambda function X and Y, X -> X contains the core logic receives the parameters and perform its functionality and returns the result Y -> Wrapper function over X (following the standard decorator pattern), translates the input received from API Gateway to the input taken by Lambda X. [1] – Udit Bhatia Sep 18 '18 at 12:43
  • 1
    But if you have an option in your use case of getting rid of the lambda Y, either by mapping fields directly from API Gateway instead of proxy integration or removing API gateway totally from picture and just relying on direct integration of SFn with Lambda. I would agree, that would be the right thing to do :) – Udit Bhatia Sep 18 '18 at 12:48