Api gatway with Lambda function

0

Hello, I'm facing an issue with AWS API Gateway and Lambda. My goal is to receive a Shopify webhook, process it in a Lambda function, and store the data in an RDS MySQL database. However, when Shopify sends the POST request, my Lambda function does not receive the body in the event.

Context: I created an API Gateway (HTTP) with a POST method linked to a Lambda function. The API URL is correctly set as a webhook in Shopify. In CloudWatch logs, the function is triggered, but event.body is undefined.

What I have tried: Added logs to inspect event → body is always undefined. ✔️ Set Content-Type: application/json in requests → No change. ✔️ Enabled API Gateway logging → The request is received, but the body does not reach Lambda Tested with Postman and cURL → Same issue, event.body is empt

2 Answers
1
Accepted Answer

The issue you're experiencing with AWS API Gateway and Lambda, where the event.body is undefined when receiving a Shopify webhook, could be due to a few potential reasons:

  1. API Gateway Configuration: Make sure your API Gateway is set up correctly to pass the request body to Lambda. For HTTP APIs, you should use the Lambda proxy integration, which automatically passes the entire request to your Lambda function.

  2. Lambda Function Code: Ensure your Lambda function is properly parsing the event object. With Lambda proxy integration, the body is typically available as a string in event.body. You may need to parse this string if it's JSON data.

  3. Content Type and Encoding: Shopify webhooks typically send data as application/json. Ensure your API Gateway is configured to handle this content type. Also, check if the body is base64 encoded, which might require decoding in your Lambda function.

  4. API Gateway Payload Format: For HTTP APIs, check the payload format version in your API Gateway settings. Version 2.0 structures the event differently from version 1.0.

  5. Request Size Limits: Verify that the webhook payload isn't exceeding API Gateway's size limits.

  6. CORS Settings: If applicable, ensure CORS settings are properly configured in API Gateway.

To troubleshoot:

  1. Double-check your API Gateway configuration, ensuring it's set to proxy all requests to Lambda.
  2. In your Lambda function, log the entire event object to see what's being received.
  3. Test with a simple payload using tools like Postman or cURL to isolate if it's a Shopify-specific issue.
  4. Review your Lambda function's IAM role to ensure it has necessary permissions.
  5. Consider using API Gateway's request mapping templates to explicitly map the body if proxy integration isn't working as expected.

If the issue persists after these checks, you may need to review your API Gateway and Lambda configurations in detail or consider reaching out to AWS support for further assistance.

Sources
AWS Gateway: API does not respond in Postman | AWS re:Post
Community | Demystifying AWS Lambda: Deep Dive into Serverless Computing
Creating api gateway api from openapi specification with lambda integration does not create triggers | AWS re:Post

profile picture
answered 2 months ago
profile pictureAWS
EXPERT
reviewed 2 months ago
profile picture
EXPERT
reviewed 2 months ago
1

The most likely causes of this issue are:

  1. Missing request body mapping template in API Gateway
  2. Incorrect content handling configuration
  3. Payload format version mismatch between API Gateway and Lambda

Verification Steps

1. Check your API Gateway Integration Type

For HTTP APIs:

  • Ensure you're using AWS_PROXY integration type
  • Verify payload format version is set to 2.0 (default for HTTP APIs)

2. Update Lambda Function to Handle HTTP API Format

exports.handler = async (event) => {
    console.log('Event:', JSON.stringify(event));
    
    // For HTTP API (payload v2.0)
    let body;
    try {
        // Body might be a string that needs parsing
        body = event.body ? JSON.parse(event.body) : null;
    } catch (e) {
        body = event.body; // Use as-is if parsing fails
    }
    
    console.log('Parsed body:', JSON.stringify(body));
    
    // Your processing logic here
    
    return {
        statusCode: 200,
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ message: 'Webhook received' })
    };
};

3. API Gateway Configuration Check

For HTTP APIs:

  1. Go to API Gateway console
  2. Select your API
  3. Go to Routes → Select your POST route
  4. Check the Integration details
  5. Ensure "Content handling" is set appropriately (if available)

4. Enable Binary Media Types (if needed)

If Shopify is sending data in a format that API Gateway treats as binary:

  1. Go to API Gateway console
  2. Select your API → Settings
  3. Under "Binary media types", add: application/json
  4. Deploy your API again

5. Test with a Simple cURL Command

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"test":"data"}' \
  https://your-api-gateway-url
AWS
answered 2 months ago
profile picture
EXPERT
reviewed 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions