2

My Architecture:

  • AWS HTTP API w/ reverse proxy integration
  • Plain Lambda function
  • Postman or browser

I'm trying to check the request method to handle actions, based on this answer they recomm

'use strict';

const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();

exports.handler = async (event) => {

    switch (event.httpMethod) {
        case 'GET':
            break;
        default:
            throw new Error(`@@@@ Unsupported method "${event.httpMethod}"`);
    }
    return {
        statusCode: 200,
        body: JSON.stringify({message: 'Success'})
    };
};

I pasted that code just like that in my lambda and it doesn't work, I get this error in the logs:

"errorMessage": "@@@@ Unsupported method \"undefined\"",

That lambda is triggered by my HTTP API and the route has GET method.

If I return the event, I can see that the method is GET or POST, or whatever, look:

enter image description here

Anyone has any idea what's going on?

1 Answer 1

3

Input Object Schema for HTTP api (v2) is different to REST api (from your link).

For a Http api, method can be obtained from event.requestContext.http.method

so, it will look like this.

exports.handler = async (event) => {
    console.log('event',event);
    switch (event.requestContext.http.method) {
        case 'GET':
            console.log('This is a GET Method');
            break;
        default:
            throw new Error(`@@@@ Unsupported method "${event.httpMethod}"`);
    }
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};
Sign up to request clarification or add additional context in comments.

3 Comments

I need some sleep, I swear at some point I tried that and didn't work. Thanks Balu
urw. must have been some typo! :)
This solved it. So, the lambda function test event is working perfectly, but when I call my public endpoint I get this error Unsupported method \"undefined\"" unless I switch to the requestContext.http.method. Question: What's the correct way to at least console.log the event object to inspect it or to test /debug the HTTP API (v2) in Lambda? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.