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:
- Create an API Project that has a base URL pointing to Amazon API Gateway (skip if you already have this).
- Forward access logs from Amazon API Gateway to CloudWatch.
- 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:
โ ๏ธ 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
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"
}
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
- Select any name you see fit.
- Select runtime โNode.js 16.xโ
- Select architecture โarm64โ
Upload ZIP File
- Download the Lambda code from RapidAPI GitHub forwarder-lambda repo.
- Unzip the folder.
- Run the NPM โyarnโ command inside the folder (this requires Node.JS + NPM).
- Zip the contents of the folder. (Important: select all the files and zip them, do not zip the containing folder)
- Upload the new Zip file into your Lambda function.
Update the environment variables
There are three new environment variables you will need to add and populate:
- 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โ
- RAPID_CONSUMER_KEY: the key must belong to a Rapid account with an environment admin role.
- 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
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
- Create a few requests against your API.
- Open CloudWatch and ensure the logs were correctly printed into the log group.
- Open the Lamda function and view the logs under monitoring, make sure there are no errors.
- Open Platform Analytics API and make sure that the Lambda function called it.
- Open the API in the hub for which you made the original call, it should now have a new log!
Updated 10 months ago