Amazon API Gateway Analytics Integration

This guide walks you through how to integrate Amazon API Gateway with your Enterprise Hub, allowing you to display usage analytics for the API(s) on Amazon API Gateway in Studio and the Developer Dashboard.

📘

APIs that do not use the Rapid Runtime

This guide applies for APIs that do not use the Rapid Runtime.

Installation

There are three main parts to this installation:

  1. Create an API Project that has a base URL pointing to Amazon API Gateway (skip if you already have this).
  2. Forward access logs from Amazon API Gateway to CloudWatch.
  3. Install a Lambda function that forwards these analytics to Rapid.

Create an API Project that has a base URL pointing to Amazon API Gateway

The Platform Analytics API supports using the base URL as an API identifier (as opposed to the Rapid API ID); however, for this to work the base URL on the API Project listing must match with the one being passed in from the gateway.

As an enterprise customer, you can set up external gateways through the Admin Panel. For this setup to work, the base URL you will add to the API must contain the entire request URL except for the path. AWS adds the stage of the deployment to the path. In order to not add this in the base URL you will need to create a Gateway template that ignores it.

Example

If your API request URL looks something like this:

https://apidid.execute-api.us-east-1.amazonaws.com/stage/endpoint

The only part you want to include in the base URL is the bold one:

https://apidid.execute-api.us-east-1.amazonaws.com/stage/endpoint

In order to achieve that your gateway template “Request URL” field for APIs using Amazon API Gateway should look like this:

3450 ⚠️ Warning: If you have a custom DNS your setup might be slightly different, please contact your technical account manager if you are not sure how to proceed.

Forward access logs from Amazon API Gateway to CloudWatch

When writing API access logs to CloudWatch, you can choose how much information to write. In addition to that, you can customize how long that information should be retained (depending on your needs).

The CloudWatch log group ARN tells Amazon API Gateway where to write the log, you will need this ARN for the next steps. If you are not sure how to get it, read this amazon guide.

Your ARN should have the following format:

arn:aws:logs:{region}:{account-id}:log-group:log-group-name

You will now need to Enable CloudWatch Logs and select the “Full request and Response logs” option.

You can configure logs for each API by selecting the stage and the “Logs/Tracing” tab configuration is available for each API under the stages tab

1723

Copy and paste the following log format:

{
	"requestId": "$context.requestId",
	"ip": "$context.identity.sourceIp",
	"requestTime": "$context.requestTimeEpoch",
	"httpMethod": "$context.httpMethod",
	"resourcePath": "$context.resourcePath",
	"status": "$context.status",
	"protocol": "$context.protocol",
	"responseLength": "$context.responseLength",
	"responseLatency": "$context.responseLatency",
	"accountId": "$context.accountId",
	"host": "$context.domainName",
	"awsApiId": "$context.apiId",
	"awsAccountId": "$context.accountId",
	"awsResourceId": "$context.resourceId"
}
⚠️ Warning: If you API already has access logs in a different format your setup might be slightly different, please contact customer support if you are not sure how to proceed.

Install a Lambda function that forwards these analytics to Rapid.

In this step, you will create the Lambda function that forwards the access logs written to CloudWatch to Rapid through the Platform Analytics API.

Creating the new Lambda

  1. Select any name you see fit.
  2. Select runtime “Node.js 16.x”
  3. Select architecture “arm64”
1723

Upload ZIP File

  1. Download the Lambda code from RapidAPI GitHub forwarder-lambda repo.
  2. Unzip the folder.
  3. Run the NPM “yarn” command inside the folder (this requires Node.JS + NPM).
  4. Zip the contents of the folder. (Important: select all the files and zip them, do not zip the containing folder)
  5. Upload the new Zip file into your Lambda function.
1723

Update the environment variables

There are three new environment variables you will need to add and populate:

  1. RAPID_PLATFORM_ANALYTICS_URL: this would be the request URL to the Platform Analytics API - Upload Analytics endpoint. It should be: “https://platform-analytics.p.rapidapi.com/calls”
  2. RAPID_CONSUMER_KEY: the key must belong to a Rapid account with an environment admin role.
  3. RAPID_PLATFORM_ANALYTICS_HOST: You can find this by opening the Platform Analytics API in the hub, it should have the following format: platform-analytics.{tenant-name}.rapidapi.xyz
1723

Create the Lambda trigger

Once the lambda function is installed, manually add a trigger, choose CloudWatch and choose the Log group that contains your API Gateway logs. add a filter name and save the trigger.

Validating everything works

  1. Create a few requests against your API.
  2. Open CloudWatch and ensure the logs were correctly printed into the log group.
  3. Open the Lamda function and view the logs under monitoring, make sure there are no errors.
  4. Open Platform Analytics API and make sure that the Lambda function called it.
  5. Open the API in the hub for which you made the original call, it should now have a new log!