3

Why would I want to only keep the body content?

One of the lambdas (let's call it X) in our project is being invoked by another lambda, and this is the only organic way we'd like X to be executed.

Input passed to X through its lambda invoker:

{
   "foo" : "foo_value",
   "bar" : "bar_value"
}


-- Writing general integration tests, we can simply invoke X with the expected event, and it works great.
-- However, for our acceptance tests, we need to invoke X in isolation from AWS rather than locally, and this can only be done via API Gateway
-- ==> so we created a POST event source for X that will only be used for test purposes.

The event sent by API Gateway to X:

{
    headers : {
       "some-headers1": "some-value1",
       ....
       "some-headersn": "some-valuen",
    },
    body : {
       "foo" : "foo_value",
       "bar" : "bar_value"
    },
    .....
    .....
}

The problem that we're looking to solve:

Is there a way to convert the API input to exactly what we're expecting from the lambda invoker?

In other words, Is it possible to write a custom request integration mapping template for the POST endpoint in order to only keep event.body ?
And by that I mean : $util.escapeJavaScript($input.json('$')) and not { body: $util.escapeJavaScript($input.json('$')) }

Why?

We can simply add a few lines of code into X to ignore the extra information generated by the API Gateway, but since API Gateway is not part of the organic execution events, we don't wish to alter X to transform the received event.



How would it be possible?

johndoe
  • 4,387
  • 2
  • 25
  • 40
user2517028
  • 784
  • 1
  • 11
  • 25
  • 1
    From locally also, invoke the Lambda using AWS SDK. Then you don't need the API Gateway for running tests. https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property – Ashan Jul 29 '18 at 03:04
  • Thank you. I still would like an answer to the question bove. but your comment may lead me to another question. when you use invoke with InvocationType: "Event", which role permissions does it assume to run? and the same question for you use InvokationType: "RequestResponse"? I've already thought of using Invoke but couldn't find an answer to these 2 questions, does it use its own assigned role or the local role? Having in mind that my local user role is with admin access – user2517028 Jul 29 '18 at 04:30
  • 1
    You have to create an IAM user with Lambda invocation permission to invoke the Lambda function from local machine. The code inside Lambda will use its assigned role to execute code. – Ashan Jul 29 '18 at 04:36
  • Answering the question you asked, its difficult to come up with a standard transformation to invoke the Lambda through API Gateway as the same since its a different event source. Though using Lambda proxy integration you can come closer to the request without any alterations happening in API Gateway. Also note that there are time out limits, payload limits when invoking through API Gateway which make this approach more challenging rather invoking through SDK. – Ashan Jul 29 '18 at 04:42

1 Answers1

1

I would suggest you decouple your lambda function logic, so it can be invoked from multiple services. Basically, you would create a separate logic in your handler for each service that invokes your lambda function to massage the provided event. So the business logic in your lambda function became resource agnostic and always gets the expected input, that was previously massages by the corresponding handler condition. For the inspiration check the following article regarding code structure https://medium.com/@mitch.zorze/2-years-with-aws-lambda-f835bfedec9f

b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57
  • I think the referenced post is Interesting, and it seems this is how we're going to handle our work. (+1 but not accepted answer since it is not a direct answer to the question) – user2517028 Jul 31 '18 at 12:40