Skip to content

Conversation

ChandlerTayek
Copy link

@ChandlerTayek ChandlerTayek commented Jul 27, 2022

Explination

So if you follow along with these instructions for the "Create a simple microservice using Lambda and API Gateway" tutorial and actually try to test the endpoint via postman or curl you will realize that you will get a server error because the lambda function is failing when trying to parse the event json data for "httpMethod". The "httpMethod" key will only be found in events generated by API gateway when it is configured as a REST API instead of an HTTP API. You can see below the differences. Basically, the microservice lambda template code which performs actions on the DynamoDB resource is set up to work out of the box for REST API gateway body format and not HTTP API's. See below for comparing the body of the HTTP API vs REST API.

HTTP API Event Data Payload structure:

"requestContext": {
        "accountId": "391741500388",
        "apiId": "lh8rxrmng8",
        "domainName": "lh8rxrmng8.execute-api.us-east-1.amazonaws.com",
        "domainPrefix": "lh8rxrmng8",
        "http": {
            "method": "GET",
             .
             .
             .

API Event Data Payload structure:

"resource": "/lambda-microservice2",
"path": "/lambda-microservice2",
"httpMethod": "GET",
"headers": {
.
.
.

Issue #, if available:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

So if you follow along with these instructions and actually try to test the endpoint via postman or curl you will realize that you will get a server error because the lambda function is failing when trying to parse the event json data for "httpMethod". The "httpMethod" key will only be found in events generated by API gateway when it is configured as a REST API instead of an HTTP API. You can see below the differences but basically, the microservice lambda template for acting on the DynamoDB resource is set up to work out of the box for REST API gateways and not HTTP APIs. See below for comparing the body of the HTTP API vs REST API.

HTTP API Event Data Payload structure:
=============================================================
```
"requestContext": {
        "accountId": "391741500388",
        "apiId": "lh8rxrmng8",
        "domainName": "lh8rxrmng8.execute-api.us-east-1.amazonaws.com",
        "domainPrefix": "lh8rxrmng8",
        "http": {
            "method": "GET",
             .
             .
             .
```
=============================================================


API Event Data Payload structure:
=============================================================
```
"requestContext": {
        "resourceId": "q9c11x",
        "resourcePath": "/lambda-microservice2",
        "httpMethod": "GET",
         .
         .
         .
```
 =============================================================
@mwunderl
Copy link
Member

Thanks for the PR, and apologies for the delay. This tutorial has since been retired. Please take a look at the updated tutorial--
https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway-tutorial.html

@mwunderl mwunderl closed this Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants