Search code examples
amazon-web-servicesgoaws-lambdaaws-api-gateway

Path is missing inside Lambda


I've written a lambda function for testing, My intension is to do slightly different things based on the path (like /abc). Following is the code

package main

import (
    "context"
    "fmt"
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

type MyEvent struct {
    Name string `json:"name"`
}

func main() {
    lambda.Start(handler)
}

func handler(ctx context.Context, event events.APIGatewayProxyRequest) (*string, error) {

    fmt.Println(event.Path, " --- ", event.Body)
    fmt.Printf(
        "Path: %#v PathParams: %#v QSParams: %#v ReqContext: %#v",
        event.Path,
        event.PathParameters,
        event.QueryStringParameters,
        event.RequestContext,
    )

    //if event == nil {
    //  fmt.Println("Empty event")
    //  return nil, fmt.Errorf("received nil event")
    //}

    message := fmt.Sprintf("Hello %v!", "event.Name")
    fmt.Println("Message is:", message)
    return &message, nil
}

The function is being invocked using API Gateway Registered route /test Full url look like this: https://someid.execute-api.regionname.amazonaws.com/default/test

The issue is getting path as empty string, Following is the log

Path: "" // <------- path is missing
PathParams: map[string]string(nil) 
QSParams: map[string]string(nil) 
ReqContext: events.APIGatewayProxyRequestContext{
    AccountID:"xxxxxxxxxxx", 
    ResourceID:"", 
    OperationName:"", 
    Stage:"default", 
    DomainName:"xxxxxxxxxx.execute-api.xxxxxxx.amazonaws.com", 
    DomainPrefix:"xxxxxxxxxx", 
    RequestID:"xxxxxxxxx", 
    ExtendedRequestID:"", 
    Protocol:"", 
    Identity:events.APIGatewayRequestIdentity{
        CognitoIdentityPoolID:"", 
        AccountID:"", 
        CognitoIdentityID:"", 
        Caller:"", 
        APIKey:"", 
        APIKeyID:"", 
        AccessKey:"", 
        SourceIP:"", 
        CognitoAuthenticationType:"", 
        CognitoAuthenticationProvider:"", 
        UserArn:"", 
        UserAgent:"", 
        User:""
    }, 
    ResourcePath:"", 
    Path:"", 
    Authorizer:map[string]interface {}(nil), 
    HTTPMethod:"", 
    RequestTime:"", 
    RequestTimeEpoch:0, 
    APIID:"xxxxxxx"
}

The function is working fine except the mentioned issue. What is going wrong here?


Solution

  • The below is the config you need to do this if you're using API gateway. The whole endpoint in this example would look like this when used... https://<domain>/members/238471, where 238471 is the path parameter I want to get hold of in my Lambda...

    enter image description here

    And then also add it to the method request, request paths...

    enter image description here

    In this example the path parameter has been called membershipNumber. Finally you need your Lambda integration request config to be:

    enter image description here