AWS API Gateway
Configuring Revenium Metering for the AWS API Gateway
Last updated
Configuring Revenium Metering for the AWS API Gateway
Last updated
© Revenium - www.revenium.io
Revenium is capable of monitoring and mediating AWS API Gateway traffic by leveraging CloudWatch and Lambda functions.
To configure the AWS Gateway for metering you'll need to do the following:
Create a CloudWatch log group for API Gateway access logs
Enable and configure access logs on all metered API stages
Create and attach the Metering Lambda Function to the CloudWatch log group
The following guide outlines the process of configuring Revenium metering using the AWS Console. Additionally, this configuration can be automated using CloudFormation, Terraform, or similar Infrastructure as Code (IaC) tools. Here is a starter CloudFormation template to help you get going.: https://github.com/revenium/revenium-metering-aws/blob/main/revenium_lambda_metering_template.yaml
Follow these steps to create a log group in AWS CloudWatch for capturing API Gateway access logs:
Navigate to the CloudWatch Service
Log in to the AWS Management Console.
Open the CloudWatch service by searching for it in the search bar or by finding it under the "Services" menu.
Access Log Groups
In the CloudWatch dashboard, locate the "Logs" section on the left sidebar.
Click on "Log groups" to view existing log groups or create a new one.
Create a New Log Group
Click the "Create log group" button.
Enter a name for your new log group, such as APIGatewayAccessLogs
.
(Optional) Set retention settings according to your log retention policy. If unsure, you can leave it as the default setting.
Click the "Create" button to create the log group.
This log group will capture and store access logs from your API Gateway, allowing you to monitor and troubleshoot your API's usage and performance.
To choose an API Gateway deployment stage for metering and activate Access Logs, follow these steps:
Navigate to the API Gateway Console: Log in to your AWS Management Console and go to the API Gateway service.
Select Your API: Identify and click on the API you wish to configure.
Stages: In the left navigation pane, click on Stages under your selected API.
Choose a Stage: Select the deployment stage you want to meter and monitor.
Enable Access Logging: Within the stage editor, click on the Logs/Tracing tab. Then, find the Access Logging section and click on the Edit button.
Specify Log Destination: Enter the ARN (Amazon Resource Name) of the CloudWatch Logs log group where access logs will be stored.
Save Changes: After configuring the settings, click on the Save Changes button to enable access logging for the selected API Gateway deployment stage.
To proceed, link the Amazon Resource Name (ARN) of your CloudWatch log group to serve as the log destination. Additionally, specify the log format to structure the data accordingly.
Now we'll define the Lambda Function and associate a CloudWatch trigger to it from our log group. Select "Python 3.12" as the Runtime.
Now add a CloudWatch trigger:
Next, select the log group you created earlier, and assign a name to your filter:
Now copy and paste the metering function into the Code Source:
Finally, configure your environment by setting the Revenium API Key
as an environment variable.
API traffic passing through this stage will now be metered by Revenium.
Lambda functions can serve as powerful authorizers, ensuring that client requests meet the required entitlements for accessing specific APIs managed by Revenium. This method provides a secure and efficient way to validate and control access to productized APIs, leveraging the flexible programming model of AWS Lambda to perform authentication and authorization tasks.
To configure an AWS API Gateway route to validate requests using Revenium, complete the following steps:
Create an authorizer lambda function
Configure the authorizer function on the API route
This method facilitates authorization, presuming that authentication occurs before reaching the API Gateway. However, the function can be adapted to handle authentication as needed.
Create the Lambda function from scratch, set the name and select "Python 3.12" as the runtime:
Now copy and paste the authorization function into the Code Source:
Next, configure your environment by setting the Revenium API Key
as an environment variable.
Finally, you can set up the authorizer function for your API route. You will need to configure the following:
Authorizer Type: Lambda
Name: The name of the authorizer function (ie, "revenium_client_id_authorizer")
Lambda Function: The ARN of the previously created function
Response Mode: IAM Policy
Authorizer Caching: Enabled (optional but recommended)
Authorizer Cache Duration: 30s (or whichever is most appropriate)
Identity Sources: $request.header.clientId
Click on "Create and Attach" and requests going through your API will now be authorized by Revenium.