Thomas Step

<- Blog

Interested in learning more about serverless AWS? Check out my Guide to Building with Serverless AWS (only pay what you want)

Stripe Webhook Integration with AWS Lambda

I have worked with Stripe for the last month or so and one topic that I stumbled upon that seemed to stump people was handling Stripe webhooks in a Lambda function. I initially stumbled on this issue because I misconfigured my code but it had nothing to do with the API Gateway and Lambda proxy configuration set up. Specifically, I am referencing problems that others came across in this GitHub issue.

I have my endpoint set up as an API Gateway and Lambda proxy integration. I configured the generated endpoint URL in Stripe and passed my Stripe secret key and webhook secret into my Lambda as environment variables held in Secrets Manager. I tried using the “Test Webhook” events in the Stripe dashboard, but they send dummy customer and price IDs which did not play well with what I needed to accomplish. Either way, I was quickly able to get everything up and running by simply using my test environment to play through the whole integration and onboarding sequence.

Verifying the webhook is easy enough, and thanks to the stellar documentation it’s a fun integration to go through. Here’s a link to what I followed from their side. This was the sticking point for most people in the aforementioned GitHub issue.

There were quite a few mentions about needing to parse a raw body or stringify the request. For me, all I needed to do was pass the event.body into the stripe.webhooks.constructEvent function. That’s it.

After constructing the webhook event, the only thing left to do it parse out the information I needed and then act on it. My need was simply to grab the subscription and price IDs and store them in my database for retrieval by my front end. This is what that Lambda handler looks like.

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const {
} = require('./database');

exports.handler = async function (event, context, callback) {
  const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
  try {
    const requestId = event?.requestContext?.requestId;
    const sig = event?.headers['Stripe-Signature'];

    const stripeEvent = stripe.webhooks.constructEvent(event.body, sig, webhookSecret);
    const eventType = stripeEvent.type ? stripeEvent.type : '';
    const jsonData = JSON.parse(event.body);

    console.log(`Event Type: ${eventType}`);

    const subscriptionId =;
    const customerId =;
    const priceId =;

    let customerEmail;
    customerEmail =['customer_details']?.email;
    if (!customerEmail) {
      const customer = await stripe.customers.retrieve(customerId);
      customerEmail =;

    switch (eventType) {
      case 'customer.subscription.created':
      case 'customer.subscription.updated':
        await updateUserSubscription(
      case 'customer.subscription.deleted':
        await deleteUserSubscription(
        console.log('Unhandled event type');

    const data = {
      statusCode: 200,
      body: JSON.stringify({
        received: true,
    return data;
  } catch (uncaughtError) {
    throw uncaughtError;
Categories: aws | dev | javascript | ops | serverless