1. Answers
  2. Building an AWS API Gateway Usage Plan

How do I build an AWS API Gateway usage plan?

In this guide, we will create an AWS API Gateway usage plan using Pulumi. A usage plan allows you to manage who can access your APIs and how much they can use them. It enables you to set throttling and quota limits for your APIs.

We will define the necessary resources for creating a usage plan, including an API Gateway REST API, a deployment, a stage, and the usage plan itself. We will also create an API key and associate it with the usage plan.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an API Gateway REST API
const api = new aws.apigateway.RestApi("myapi", {
    description: "My API Gateway",
});

// Create a deployment of the API
const deployment = new aws.apigateway.Deployment("mydeployment", {
    restApi: api.id,
    stageName: "dev",
});

// Create a stage for the deployment
const stage = new aws.apigateway.Stage("mystage", {
    restApi: api.id,
    deployment: deployment.id,
    stageName: "dev",
});

// Create a usage plan
const usagePlan = new aws.apigateway.UsagePlan("myusageplan", {
    apiStages: [
        {
            apiId: api.id,
            stage: stage.stageName,
        },
    ],
    throttleSettings: {
        burstLimit: 100,
        rateLimit: 50,
    },
    quotaSettings: {
        limit: 1000,
        period: "MONTH",
    },
});

// Create an API key
const apiKey = new aws.apigateway.ApiKey("myapikey", {
    description: "My API Key",
    enabled: true,
});

// Associate the API key with the usage plan
const usagePlanKey = new aws.apigateway.UsagePlanKey("myusageplankey", {
    keyId: apiKey.id,
    keyType: "API_KEY",
    usagePlanId: usagePlan.id,
});

// Export the URL of the API
export const apiUrl = pulumi.interpolate`${api.executionArn}/${stage.stageName}`;

Key Points

  • We created an API Gateway REST API using aws.apigateway.RestApi.
  • We deployed the API and created a stage using aws.apigateway.Deployment and aws.apigateway.Stage.
  • We defined a usage plan with throttle and quota settings using aws.apigateway.UsagePlan.
  • We created an API key using aws.apigateway.ApiKey and associated it with the usage plan using aws.apigateway.UsagePlanKey.

Summary

In this guide, we successfully created an AWS API Gateway usage plan with associated resources using Pulumi. This setup allows you to manage API access and usage limits effectively.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up