Bootstrap FreeKB - Amazon Web Services (AWS) - API Gateway Lambda Function and Cloudwatch Logs
Amazon Web Services (AWS) - API Gateway Lambda Function and Cloudwatch Logs

Updated:   |  Amazon Web Services (AWS) articles

Let's say you have a Lambda Function, perhaps something like this (Python in this example). In this example, the print(event) statement will append logs to Cloudwatch.

import json

def lambda_handler(event, context):

    print(event)

    return {
        'statusCode': 200,
        'body': json.dumps({
            "greeting":"hello",
            "foo": event['queryStringParameters']['foo']
        })
    }

 

An API Gateway HTTP API with a route, such as a GET route or POST route, can be used so that a GET or POST request can be made to your Lambda Function over HTTP.

The aws apigatewayv2 get-apis command can be used to list the API Gateway HTTP APIs you have created.

~]$ aws apigatewayv2 get-apis
{
    "Items": [
        {
            "Name": "foo",
            "Tags": {},
            "ProtocolType": "HTTP",
            "RouteSelectionExpression": "$request.method $request.path",
            "ApiId": "abcdefg123",
            "ApiEndpoint": "https://abcdefg123.execute-api.us-east-1.amazonaws.com",
            "ApiKeySelectionExpression": "$request.header.x-api-key",
            "CreatedDate": "2023-12-06T02:12:32Z",
            "DisableExecuteApiEndpoint": false
        }
    ]
}

 

And here is how you could GET using curl with data.

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

 

Then, in Cloudwatch > Log groups > /aws/lambda/myapi > yyyy/mm/dd/[$LATEST]abcdefg123456789abdefg123456789, you should have something like this.

{
   "version":"2.0",
   "routeKey":"GET /myendpoint",
   "rawPath":"/myendpoint",
   "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":"/myendpoint",
         "protocol":"HTTP/1.1",
         "sourceIp":"10.11.12.13",
         "userAgent":"curl/8.0.1"
      },
      "requestId":"PgBK5jaDIAMEJsQ=",
      "routeKey":"GET /myendpoint",
      "stage":"$default",
      "time":"06/Dec/2023:02:53:57 +0000",
      "timeEpoch":1701831237455
   },
   "isBase64Encoded":false
}

 

You may also want to create a Cloudwatch Alarm so that you are notified when your Lambda Function returns an error. Check out my article Create Lambda Function Cloudwatch Alarm using the AWS CLI.




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 9b296a in the box below so that we can be sure you are a human.