5

I'm in the process of learning AWS Lambda. I have created a lambda that will act as a REST API (APIEvent in CloudFormation terms) and want to debug that Lambda locally using an event.

If I understand correctly, running sam local generate-event apigateway aws-proxy generates an event that is suitable for locally running/debugging my Lambda. This produces the following event (some nested values are abbreviated):

{
  "body": "eyJ0ZXN0IjoiYm9keSJ9",
  "resource": "/{proxy+}",
  "path": "/path/to/resource",
  "httpMethod": "POST",
  "isBase64Encoded": true,
  "queryStringParameters": {
    "foo": "bar"
  },
  "multiValueQueryStringParameters": {
    "foo": [
      "bar"
    ]
  },
  "pathParameters": {
    "proxy": "/path/to/resource"
  },
  "stageVariables": {
    "baz": "qux"
  },
  "headers": {
    ...
  },
  "multiValueHeaders": {
    ...
  },
  "requestContext": {
    "accountId": "123456789012",
    "resourceId": "123456",
    "stage": "prod",
    "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
    "requestTime": "09/Apr/2015:12:34:56 +0000",
    "requestTimeEpoch": 1428582896000,
    "identity": {
      ...
    },
    "path": "/prod/path/to/resource",
    "resourcePath": "/{proxy+}",
    "httpMethod": "POST",
    "apiId": "1234567890",
    "protocol": "HTTP/1.1"
  }
}

However, I do not understand the relation between resource (which is copied in requestContext.resourcePath) and path (which is copied in requestContext.path). What am I supposed to fill in for these values?

DieterDP
  • 4,039
  • 2
  • 29
  • 38

1 Answers1

1

The resource is the API resource you defined in the API gateway. For example in your case /{proxy+}.

The path is the actual path from the request.

So, when you make a request GET https://yourdomain.com/v1/pets :

  • the path is /v1/pets
  • the resource is /{proxy+}
wildnux
  • 408
  • 1
  • 7
  • 22
  • 1
    Two follow up questions: 1) is it then a mistake in the template that there is an additional `/prod` present in the `requestContext.path` compared to the `path`? 2) I don't understand the meaning of `{proxy+}`. Is it a simple placeholder? In your example it remains `{proxy+}` rather than the hostname (`yourdomain.com`)? In which case would a resource not be equal to `{proxy+}`? Thanks in advance! – DieterDP Sep 10 '22 at 18:05
  • I could be wrong here, but I believe the requestContext.path is including the stage name as well. The resource is [proxy placeholder](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-settings-method-request.html#api-gateway-proxy-resource). You can get the actual value matched with the placeholders from path parameters. Disclaimer: my answers are based on what I have seen so far, the docs lack the explanation, so please take them with grain of salt and reach out to AWS support for correct answers. – wildnux Sep 12 '22 at 17:08