Skip to content

Instantly share code, notes, and snippets.

@lukehoban
Last active September 25, 2023 07:41
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lukehoban/94198756e3a9f111af35988e166b2696 to your computer and use it in GitHub Desktop.
Save lukehoban/94198756e3a9f111af35988e166b2696 to your computer and use it in GitHub Desktop.
API Gateway Domain Mapping in Pulumi
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// The services we want to host on our domain...
const api1 = new aws.apigateway.x.API("api1", {
routes: [
{method: "GET", path: "/", eventHandler: async(ev) => {
return {
statusCode: 200,
body: JSON.stringify({hello: "world"}),
}
}}
]
});
const api2 = new aws.apigateway.x.API("api2", {
routes: [
{method: "GET", path: "/", eventHandler: async(ev) => {
return {
statusCode: 200,
body: JSON.stringify({goodbye: "world"}),
}
}}
]
});
// We have registered a domain, and a cert already (we could do most of this with Pulumi as well if we wanted!)
let domainName = "api.lukestestapp.net"; // `lukestestapp.net` is regitered with Route53
let route53DomainZoneId = "Z2ARB0000EFMOW"; // The Hosted Zone I got when I registered `lukestestapp.net`
let certARN = "arn:aws:acm:us-east-1:000052950000:certificate/b27b1f1a-189d-4beb-be5d-34f056e7b2d1"; // ACM cert for `*.lukestestapp.net`
// API Gateway requires we register the Domain with it first
const domain = new aws.apigateway.DomainName("domain", {
certificateArn: certARN,
domainName: domainName,
});
// Then we can map a REST API to a domain with a BasePathMapping
const mapping = new aws.apigateway.BasePathMapping("mapping", {
restApi: api1.restAPI,
basePath: "api1", // We map our API into the "/api1" base path
stageName: api1.stage.stageName, // We map the stage we got for free with `.x.API` above
domainName: domain.domainName, // We map it into the domain we registered above
});
const mapping2 = new aws.apigateway.BasePathMapping("mapping2", {
restApi: api2.restAPI,
basePath: "api2",
stageName: api2.stage.stageName,
domainName: domain.domainName,
});
// Finally, we need a DNS reocrd to point at our API Gateway
const record = new aws.route53.Record("record", {
type: "A",
zoneId: route53DomainZoneId,
name: domainName, // Write a record for `api.lukestestapp.net` into the zone for `lukestestapp.net`
aliases: [{
name: domain.cloudfrontDomainName, // APIGateway provides it's own CloudFront distribution we can point at...
zoneId: domain.cloudfrontZoneId,
evaluateTargetHealth: true,
}],
});
// It might take a while after we deploy the record above before the DNS propagates and allows us to resolve these URLs...
export let url1 = pulumi.interpolate`https://${record.name}/api1`;
export let url2 = pulumi.interpolate`https://${record.name}/api2`;
@M1kep
Copy link

M1kep commented May 15, 2021

I've been looking for an example of this for waaay too long. Thanks for putting this online.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment