Bootstrap FreeKB - Amazon Web Services (AWS) - GET Request to a Lambda Function using API Gateway
Amazon Web Services (AWS) - GET Request to a Lambda Function using API Gateway

Updated:   |  Amazon Web Services (AWS) articles

Let's say you have created an API Gateway that contains a HTTP GET route that will forward a GET request onto a Lambda Function, perhaps something like this.

 

Let's say you have a Lambda Function, perhaps something like this (Python in this example). In this example, the Lambda Function has an if statement that contains the API Gateway GET route, which is /getSendgrid in this example.

import json

def lambda_handler(event, context):
    print(event)
    if event['rawPath'] == '/getSendgrid':
        return {
            'statusCode': 200,
            'body': json.dumps({
                "foo": event['queryStringParameters']['foo']
            })
        }

 

Here is how you could submit a GET using curl with data. Notice in this example that the foo key in the response JSON include a value of bar.

~]$ curl --request GET --url https://abcdefg123.execute-api.us-east-1.amazonaws.com/getSendgrid?foo=bar
{"foo": "bar"}

 

Notice also in this example that the Lambda Function includes print(event). This is super helpful when testing/debugging, but you probably wouldn't want to include print(event) after your testing/debugging is done, as this could add a bit of additional cost each time your Lambda Function is called.

If you have print(event), then you can scoot over to Cloudwatch and Cloudwatch > Log groups > /aws/lambda/myapi > yyyy/mm/dd/[$LATEST]abcdefg123456789abdefg123456789 should have something like this. For more details on this, check out my article Amazon Web Services (AWS) - API Gateway Lambda Function and Cloudwatch Logs.

{
   "version":"2.0",
   "routeKey":"GET /getSendgrid",
   "rawPath":"/getSendgrid",
   "rawQueryString":"foo=bar",
   "headers":{
      "accept":"*/*",
      "content-length":"0",
      "host":"abcdefg.execute-api.us-east-1.amazonaws.com",
      "user-agent":"curl/8.0.1",
      "x-amzn-trace-id":"Root=1-656fe245-0f4b84cd29eb21af126bd7c8",
      "x-forwarded-for":"10.11.12.13",
      "x-forwarded-port":"443",
      "x-forwarded-proto":"https"
   },
   "queryStringParameters":{
      "foo":"bar"
   },
   "requestContext":{
      "accountId":"123456789012",
      "apiId":"cl88zvqi38",
      "domainName":"abcdefg.execute-api.us-east-1.amazonaws.com",
      "domainPrefix":"abcdefg",
      "http":{
         "method":"GET",
         "path":"/getSendgrid",
         "protocol":"HTTP/1.1",
         "sourceIp":"10.11.12.13",
         "userAgent":"curl/8.0.1"
      },
      "requestId":"PgBK5jaDIAMEJsQ=",
      "routeKey":"GET /getSendgrid",
      "stage":"$default",
      "time":"06/Dec/2023:02:53:57 +0000",
      "timeEpoch":1701831237455
   },
   "isBase64Encoded":false
}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 7d5649 in the box below so that we can be sure you are a human.