Lambda function receiving empty headers when using APIGatewayV2HTTPRequest

Viewed 1025

I'm using APIGatewayV2HTTPRequest in Go for making a request as follows from my lambda function:

"github.com/aws/aws-lambda-go/events"

func (mh *MyHandler) HandleRequest(ctx context.Context, gatewayRequest events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {...}

My API Gateway settings for this lambda function looks like this:

Integration type : Lambda Function
Mapping Template: When there are no templates defined (recommended)
Content-Type: application/json
General Template: Method Request Passthrough

##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
    "account-id" : "$context.identity.accountId",
    "api-id" : "$context.apiId",
    "api-key" : "$context.identity.apiKey",
    "authorizer-principal-id" : "$context.authorizer.principalId",
    "caller" : "$context.identity.caller",
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
    "cognito-identity-id" : "$context.identity.cognitoIdentityId",
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
    "http-method" : "$context.httpMethod",
    "stage" : "$context.stage",
    "source-ip" : "$context.identity.sourceIp",
    "user" : "$context.identity.user",
    "user-agent" : "$context.identity.userAgent",
    "user-arn" : "$context.identity.userArn",
    "request-id" : "$context.requestId",
    "resource-id" : "$context.resourceId",
    "resource-path" : "$context.resourcePath"
    }
}

Although I see the HTTP Request headers in my API Gateway logs, I don't see these headers getting passed over to my lambda function.
Cloudwatch Log showing empty HTTP request headers

Note: I have previously attempted using Lambda Proxy Integration with APIGatewayProxyRequest which does work perfectly, but my application requires an Integration Response template as well which isn't available when using the Proxy option.

Where could I be going wrong?

1 Answers

Not sure what specifically you're looking for; I had issues that it was not obvious how to obtain the calling method when I hooked up a HTTP API to a Lambda function - after a little digging and experimenting, this worked:

func Handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayProxyResponse, error) {

    log.Info("method = " + req.RequestContext.HTTP.Method)

    if req.RequestContext.HTTP.Method == "GET" {
        return events.APIGatewayProxyResponse{
            StatusCode: http.StatusForbidden,
            Body:       `{"message": "HTTP GET method not supported on this endpoint."}`,
        }, nil
    }
}
Related