How do I build an AWS API Gateway domain name?
Overview
In this guide, you will learn how to create and configure a custom domain name in AWS API Gateway. This involves the creation of a domain name, an SSL certificate, and the configuration of the domain name mappings.
A custom domain name can be used to provide your APIs with more readable URL endpoints, which align with your existing applications and DNS records.
What Will Be Done
- Create an SSL Certificate: Using AWS Certificate Manager (ACM), we’ll request an SSL certificate for the custom domain.
- Create a Custom Domain Name: We will set up a custom domain name in API Gateway using the certificate.
- Domain Name Configuration: Finally, we will configure a base path mapping for the API and domain name.
Let’s get started with the following infrastructure configuration.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// SSL Certificate for the custom domain in AWS ACM
const cert = new aws.acm.Certificate("cert", {
domainName: "example.com",
validationMethod: "DNS",
subjectAlternativeNames: ["www.example.com"],
tags: {
Name: "example-cert",
},
});
// Custom Domain Name in AWS API Gateway
const customDomain = new aws.apigateway.DomainName("custom_domain", {
domainName: "api.example.com",
regionalCertificateArn: cert.arn,
});
// API Resources
const myapi = new aws.apigateway.RestApi("myapi", {
name: "myapi",
description: "My API",
tags: {
Name: "example-api",
},
});
// Base Path Mapping for the API and Custom Domain
const pathMapping = new aws.apigateway.BasePathMapping("path_mapping", {
restApi: myapi.id,
stageName: "prod",
domainName: customDomain.domainName,
});
export const customDomainName = customDomain.domainName;
export const certificateArn = cert.arn;
export const restApiId = myapi.id;
Key Points
- The SSL certificate is created in AWS ACM for the custom domain name.
- The custom domain name is configured in AWS API Gateway using the provided certificate.
- We use base path mappings to associate the custom domain with a specific API and stage.
Summary
In this guide, we created an SSL certificate with AWS Certificate Manager, set up a custom domain name in AWS API Gateway, and configured base path mappings for the API. This allows you to serve your API using a friendly and branded domain name.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.