How to Log API Calls for AWS Lambda Go functions
AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. Amazon API Gateway is a managed service that allows developers to define the HTTP endpoints of a REST API and connect those endpoints with the corresponding backend business logic. API Gateway sits between the backend services of your API and your API’s users, handling the HTTP requests to your API endpoints, any authentication need, and routing them to the correct backends. In the serverless environment, we have lesser control over the underlying infrastructure and logging is an essential part of building backends and it is no different for a serverless API. Tracking API requests is a way to understand not only how the APIs are performing and where optimization is needed, but also gain visibility into API usage and which functions are being used the most.
In this guide, we’ll show you how to set up Moesif API analytics in a few minutes to track your APIs that are hosted on AWS Lambda using API Gateway as a trigger.
Create an AWS Lambda function handler
In our lambda function written in Go, we need to include the aws-lambda-go package, which implements the Lambda Programming model for Go. In addition, we need to implement handler function and a main() function.
The following is a sample class and Lambda function that receives Amazon API Gateway event record data as an input, and responds with a 200 status, headers, and the same body as the request.
package main
import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
"context"
)
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
Body: request.Body,
StatusCode: 200,
Headers: map[string] string {
"RspHeader1": "RspHeaderValue1",
"Content-Type": "application/json",
"Content-Length": "1000",
},
}, nil
}
func main() {
lambda.Start(handleRequest)
}
We need to maintain a history of requests, in the order they took place. Each log entry contains the information about request, including client IP address, request date/time, request path, HTTP Code, bytes served, user agent, etc. With Moesif AWS Lambda Middleware for Go we would log all the API calls and sends to Moesif.
We’ll define the configuration options for our function. For more detail, please refer to Moesif Configuration Options.
func MoesifOptions() map[string]interface{} {
var moesifOptions = map[string]interface{} {
"Log_Body": true,
}
return moesifOptions
}
Now, we will add Moesif Middleware
to our lambda function.
func main() {
lambda.Start(moesifawslambda.MoesifLogger(HandleLambdaEvent, MoesifOptions()))
}
After the integration, we’ll see all the API calls are captured and sent to Moesif.
Securely Log Data
An authentication request contains credentials and a response contains a security token, it would have been insecure to record credentials & tokens in the logs. With Moesif, we could securely extract the information we need, while maintaining security and not requiring a whole lot of changes to our codebase. With Mask_Event_Model
configurations option, we could mask the request/response header fields or request/response body to preserve the confidentiality of authentication information. Additionally if we want to remove logging request and response body to Moesif, we could set the Log_Body
configuration option to false
. We could also capture all outgoing API calls from our app to third parties like Stripe, Github or to our own internal APIs.
To understand how your APIs are used, capture API requests and responses and log to Moesif for analytics and real-time debugging of your API traffic via AWS Lambda. For more detail about how to integrate Moesif, you can git clone and run this example app from GitHub.
Meanwhile, if you have any questions, reach out to the Moesif Team