AWS lambda webhook handler

185 views
Skip to first unread message

Bob Lund

unread,
Jul 20, 2022, 5:05:34 PM7/20/22
to api-d...@lists.stripe.com
Hi,

I am having trouble implementing a webhook handler in AWS lambda.

I have an Express handler example:
app.post('/webhook', bodyParser.raw({type: 'application/json'}), async (request, response) => {
where the bodyParse extracts the raw JSON into the request.body.

I cannot find how to do this with the AWS lambda interface: exports.lambdaHandler = async (event, context) => {
event.body is empty.

I found this that discusses how to extract the raw payload without bodyParser but it involves access to the raw HTTP stream which AWS doesn't provide access to.

I guess an alternative would be for stripe to send the webhook as stringified JSON but I don't see how to do that.

Any suggestions how to process webhook events in AWS lambda?

Thanks,
Bob Lund

Remi J.

unread,
Jul 20, 2022, 5:12:03 PM7/20/22
to api-d...@lists.stripe.com
Hello,

Accessing the raw body of the request with AWS lambda can be a bit tricky/confusing. My understanding is that you need to configure a "body mapping template" in the API Gateway to map `$input.body` as expected. This is something they document here [1].

I haven't use AWS Lambda myself recently but I have an old example that might be helpful to start though things might have changed since then:

{
  "method": "$context.httpMethod",
  "body": $input.json('$'),
  "rawBody": "$util.escapeJavaScript($input.body).replaceAll("\\'", "'")",
  "headers": {
    #foreach($param in $input.params().header.keySet())
    "$param": "$util.escapeJavaScript($input.params().header.get($param))"
    #if($foreach.hasNext),#end
    #end
  }
}

There's also a similar discussion in a Github issue on stripe-node that was discussed a while ago here [2] with some details too.

I hope this puts you in the right direction!
Best,
Remi


--
To unsubscribe from this group and stop receiving emails from it, send an email to api-discuss...@lists.stripe.com.

Igor Shmukler

unread,
Jul 20, 2022, 5:12:05 PM7/20/22
to Stripe API Discussion
I have successfully used https://github.com/vendia/serverless-express
a few years ago.

If you were to use "serverless framework," you could describe your
HTTP API function[s] with serverless.yml.
Reply all
Reply to author
Forward
0 new messages