show me how to create an apigatewayv2 REST API and set the default route
TypeScriptSure, here is a simple Pulumi program written in TypeScript that demonstrates how to create an API Gateway Version 2 and set the
$default
route. This example uses theaws.apigatewayv2.Api
andaws.apigatewayv2.Route
resources.This program creates an HTTP API Gateway and a default route. This route would forward all requests to an HTTP integration, which could be your application load balancer, another HTTP endpoint, a Lambda function, and so on. In this program, we are simply making a mock integration.
Here's the TypeScript code:
import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; // Create an API let myApi = new aws.apigatewayv2.Api("myApi", { protocolType: "HTTP", target: "https://example.com", // a mock target }); // Create a route let defaultRoute = new aws.apigatewayv2.Route("defaultRoute", { apiId: myApi.id, routeKey: "$default", target: "integrations/000000", }); // Export the URL of the API export const url = myApi.apiEndpoint;
Here's what the code does:
-
The
aws.apigatewayv2.Api
instancemyApi
represents your HTTP API. Thetarget
property is an HTTP endpoint that the API will proxy. Note that in this example, the HTTP API does not integrate with a real target; it simply points to a placeholder URL. You'd replacehttps://example.com
with the URL of your own backend service. -
The
aws.apigatewayv2.Route
instancedefaultRoute
represents a route associated with your API. TherouteKey
property is set to$default
, which means this route will match any request that is not matched by other routes. -
We use a
target
of the formintegrations/000000
- a mock integration, as a stand-in for your actual integration. In a real application, this could e.g., refer to a Lambda function ("integrations/${myLambdaIntegration.id}"
) or refer to an HTTP endpoint ("integrations/${myHttpIntegration.id}"
).
For more details:
- Check Pulumi Registry for aws.apigatewayv2.Api
- Check Pulumi Registry for aws.apigatewayv2.Route
-