Skip to main content

Install with AWS CloudFront

This adaptor is currently in beta. Please contact us at [email protected].

Our Observability Adapter for AWS CloudFront networks allows you to monitor your live API traffic from AWS CloudFront, without configuring monitors manually in the APImetrics platform. By deploying this adapter, APImetrics can read monitors that are routed through AWS CloudFront, and automatically build your API Catalog. Once integrated, you can compare your live API traffic against your OAS spec, and set up synthetic endpoint monitoring automatically.

This integration is part of our Observation offering.

Prerequisites

There are a few things that need to be set up or in use before integrating APImetrics into your development process. You will need the following:

  • An AWS account.
  • A CloudFront distribution that manages content delivery for the service you wish to monitor.
  • The ability to deploy a Lambda@Edge function.

There are two options for adding APImetrics to your CloudFront network, due to the nature of Lambda@Edge functions, only one function is allowed.

Create a new API Key for APImetrics by following the API Key tutorial and choosing Ingress as your Access Level.

Note: We recommend frequently rotating your API Keys for added security. To do this, simply delete your current API Key and follow the instructions for generating an API Key.

Architecture

Below is a diagram of how this integration works:

Architecture diagram of CloudFront Lambda@Edge function communicating with Darkspark via HTTP

Before Starting

Note: Do not use your AWS root user privileges for these operations, as you could accidentally make an unrecoverable error.

As a best practice, always use the principle of least-privileged access when deploying new functionality into your environment, including for APImetrics. To analyze your API data, APImetrics requires access to monitor monitors.

Library for Existing Lambda@Edge Function

If you are already using an existing Lambda@Edge function, add the package as a dependency to your Lambda's source directory with the following command:

npm install darkspark-aws-lambda-edge-plug

Once you have installed the package, you can import it into your project like so:

const { useDarkspark } = require("darkspark-aws-lambda-edge-plug");

Integrate it into your handler function, by calling useDarkspark with the incoming event:

exports.handler = async function(event, context, callback) {
await useDarkspark(event, "<your APImetrics API key>");

// other lambda logic
...

Handler

Create a Lambda Function

Note: When creating the Lambda@Edge function, the region must be set as us-east-1.

Firstly, search for Lambda in AWS.

AWS console search results for Lambda with the Lambda service highlighted

From here, create a new Lambda function.

AWS Lambda console with the orange Create function button highlighted

Name your Function, confirm the runtime is set as Node.js 18.x and ensure the Architecture is set to arm64.

Lambda Basic information form with function name, Node.js 18.x runtime, and arm64 architecture selected

Next, expand the Change default execution rule option.

Lambda Permissions section with the Change default execution role option highlighted

Select Create a new role from AWS policy templates.

Execution role options with Create a new role from AWS policy templates selected

Select Policy Templates.

Policy templates dropdown field highlighted in the Lambda execution role settings

A search bar will pop up and you can now search for CloudFront. There will only be one option for Basic Lambda@Edge permissions.

Policy templates search for cloudfront showing the Basic Lambda@Edge permissions option

Name this Role and Create function.

Role name field filled in and the Create function button highlighted

Upload & Set Darkspark as Handler

You'll automatically be taken to an overview of the new Lambda function. Select Upload from in the Code source section and choose to upload for a .zip file.

Code source section with Upload from menu open and .zip file option highlighted

The file you need to upload can be downloaded as the Lambda@Edge package. In the upload popup, select Upload and navigate to where you downloaded the zip file and then upload.

Upload a .zip file dialog with the Upload button highlighted

Once this file has been uploaded, select Save.

Upload dialog showing the selected darkspark zip file with the Save button highlighted

Next, go to Runtime settings further down the page and select Edit.

Runtime settings panel with the Edit button highlighted

From here, change the handler to darkspark.handler and Save.

Edit runtime settings with Handler set to darkspark.handler and the Save button highlighted

Next, go to Actions at the top of the page and select Publish new version.

Function page with the Actions menu open and Publish new version highlighted

You can add an optional description if you want and select Publish.

Publish new version from LATEST dialog with optional description field and Publish button highlighted

Copy the new function ARN, ensuring it has the version number at the end, i.e. :1 at the end.

Function ARN ending in :1 with the copy-to-clipboard icon highlighted

Go to CloudFront & Configure the Behaviours

Search for CloudFront in AWS.

AWS console search results for CloudFront with the CloudFront service highlighted

Select whichever Distribution you wish to attach APImetrics to. Select Behaviours from the tabs.

CloudFront distribution tabs with the Behaviors tab highlighted

Select the behaviour you want to attach APImetrics to and press Edit.

CloudFront Behaviors toolbar with the Edit button highlighted

Scroll to the bottom of the options to Function Associations. Go to Origin response and then select Lambda@Edge from the dropdown and paste the copied function ARN. Finally, Save changes.

Function associations with Origin response set to Lambda@Edge, the ARN pasted, and Save changes highlighted

Create API Key

You will need to create a new APImetrics API Key, see the API Key tutorial. Copy the key for use later.

Add APImetrics as a Header

Return to CloudFront then select Origins.

CloudFront distribution tabs with the Origins tab highlighted

Select the Origin you have chosen to attach APImetrics to and press Edit.

CloudFront Origins toolbar with the Edit button highlighted

Scroll to Add custom header and select the Add header button.

Add custom header section with the Add header button highlighted

Paste x-darkspark-api-key in the Header name field and your newly generated API Key in the Value field. Then, Save changes.

Custom header with X-DARKSPARK-API-KEY name and API key value entered, Save changes highlighted

This is the process complete. It will take a couple of minutes to deploy fully.

Maintenance

With some updates to Darkspark, we may need to release a new version of the CloudFront plug. In this case, you will need to download the newest version of the plug and redeploy using the steps on this page.

Want to know when we make these newest versions? Check the Changelog.

FAQ

There is an issue with the downloaded package. What do I do?


First, try and redownload the package as there might be a newer version that you missed. If this doesn't work, please feel free to contact us at [email protected].

Can I run the handler using another language?


Sadly, no. We currently only support Node.js. If you are interested in another language, please feel free to contact us at [email protected].