0

So I am making an app and need AWS API Gateway. I want to use HTTP API instead of REST API. My code looks like this

package main

import (
    "database/sql"
    "fmt"
    "strings"

    "github.com/aws/aws-lambda-go/lambda"
    _ "github.com/lib/pq"
)

here I make a connection to the database


func fetch(inte string, input string) string {
    if err != nil {
        panic(err)
    }
    switch inte {
    case "00":
{
            res = append(res, response)
        }

        switch len(res) {
        case 0:
            return "401"
        }
        
    case "01":
        
        }

        switch len(res) {
        case 0:
            return "402"
        }
        
    }

    return "404"
}

type LambdaEvent struct {
    Req string `json:"req"`
    Num string `json:"num"`
}

type LambdaResponse struct {
    Res string `json:"res"`
}

func LambdaHandler(event LambdaEvent) (LambdaResponse, error) {
    res := fetch(event.Num, event.Req)
    return LambdaResponse{
        Res: res,
    }, nil
}

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

So as you see this is not the full code. I make a connection to the database and and work with the requests string query. so I tried the same with http api but it just gives me the 404 meaning the http api doesn't pass the query string to the lambda so how do I make my api pass the data to the lambda. Rest api works HTTP doesn't. Thanks for any help.

Thomas Kiml
  • 13
  • 1
  • 2

3 Answers3

1

I'm not familiar with Serverless Frameworks for APIGW, but manipulating QueryString parameters is built into the APIGW Console. Just login to AWS and search for APIGateway. Edit your HTTP API and select Integrations from the menu on the left. Select the integration that maps to your Lambda function and Edit the Parameter Mappings on the right

Screenshot of APIGateway showing how to override query string parameters.

Chris Smith
  • 592
  • 3
  • 9
0

If you are deploying your lambdas and api-gateway with serverless framework you can do something like this:

hello:
  handler: src/hello.handler
  name: hello
  events:
    - http:
        path: car/{id}/color/{color}
        method: get
  • ok but how do I make my http api pass data to the lambda – Thomas Kiml Apr 06 '21 at 21:56
  • The variable event.Req internally has an attribute called path Parameters, and for the example I gave you it could be accessed as follows: request.pathParameters.id request.pathParameters.color ** This is an example in NodeJS, it tries to show by console what is the content of the event.Req object, among its attributes or methods it must have the option of obtaining the pathParameters Here's an example of how to do it with Go: https://csdcorp.com/blog/coding/lambda-go-api-gateway-integration-and-parameters/ – Jordi Tanta Diaz Apr 06 '21 at 23:11
  • thanks I did what the tutorial said and found it the simplest solution, THANKS – Thomas Kiml Apr 07 '21 at 10:52
0

Assuming you are planning to use Lambda Proxy Integration in API Gateway, here are the changes that needs to be done to access the query parameters.

  • import github.com/aws/aws-lambda-go/events (This has all the relevant structs)
  • Change the lambda handler to func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
  • Now you can access the query parameters as a Map at request.QueryStringParameters and execute your selection logic
  • When you return a response for API Gateway, ensure you follow the events.APIGatewayProxyResponse struct i.e. at least return a status code along with optional body, headers etc.
  • No changes/config required at the API Gateway to pass the query parameters with Lambda proxy integration

You can use your own structs for request and response, but they need to use the appropriate keys as defined in the events.APIGatewayProxyRequest and events.APIGatewayProxyResponse.

e.g. add the following in LambdaEvent struct to access the query string parameters.

QueryStringParameters map[string]string `json:"queryStringParameters"`

If you are getting started with AWS Lambda, have a look at AWS SAM to keep things simple.

GSSwain
  • 5,787
  • 2
  • 19
  • 24