Skip to content

RestApi

Source: src/AWS/ApiGateway/RestApi.ts

An Amazon API Gateway REST API (v1).

RestApi is the root of an API Gateway v1 stack. Every other ApiGateway resource — Resource, Method, Authorizer, Deployment, Stage — hangs off a RestApi. The only identity you need to thread through your stack is the RestApi value itself: child resources accept restApi: api and register themselves back onto the API so that deployments and stages wait for them without any user-authored dependency lists.

A minimal API Gateway stack is four pieces: the RestApi, one or more Methods, a Deployment that snapshots those methods, and a Stage that exposes the deployment at a URL.

import * as ApiGateway from "alchemy/AWS/ApiGateway";
const api = yield* ApiGateway.RestApi("Api", {
endpointConfiguration: { types: ["REGIONAL"] },
});
yield* ApiGateway.Method("GetRoot", {
restApi: api,
httpMethod: "GET",
authorizationType: "NONE",
integration: { type: "MOCK" },
});
const deployment = yield* ApiGateway.Deployment("Release", {
restApi: api,
});
const stage = yield* ApiGateway.Stage("Prod", {
restApi: api,
stageName: "prod",
deploymentId: deployment.deploymentId,
});

Writing restApi: api on a child (rather than restApiId: api.restApiId) does two things: it threads the restApi id through, and it registers a RestApiBinding back onto the API. The Alchemy scheduler sees those bindings as reverse edges from children into the API, and Deployment reads them to express a transitive dependency on every child. You never have to write a DependsOn list or a triggers hash — adding a new Method automatically orders it before the next Deployment.

const api = yield* ApiGateway.RestApi("PrivateApi", {
endpointConfiguration: {
types: ["PRIVATE"],
vpcEndpointIds: [endpoint.vpcEndpointId],
},
policy: JSON.stringify({
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: "*",
Action: "execute-api:Invoke",
Resource: "*",
}],
}),
});
const api = yield* ApiGateway.RestApi("BinaryApi", {
binaryMediaTypes: ["application/octet-stream", "image/png"],
minimumCompressionSize: 1024,
});
const api = yield* ApiGateway.RestApi("CustomDomainOnlyApi", {
endpointConfiguration: { types: ["REGIONAL"] },
disableExecuteApiEndpoint: true,
});