Step Functions with Lambdas using Lambda Proxy Integration

ⅰ亾dé卋堺 提交于 2019-12-11 06:37:25

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/52374780/step-functions-with-lambdas-using-lambda-proxy-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!