How to Set Up a Custom Domain for Your AWS API Gateway Using AWS CLI

If you’re using Amazon Web Services (AWS) API Gateway, you might find that the default endpoint URL doesn’t look very professional and doesn’t match your branding. In this guide, we’ll show you how to set up a custom domain for your AWS API Gateway using the AWS Command Line Interface (CLI). This will allow you to replace the default URL with a more professional and brand-friendly one.

Before you begin, it’s important to note that you’ll need to have your own domain registered. It doesn’t necessarily have to be on the same AWS account that you’re working in, but it certainly makes things easier. If you’re unsure how to register a domain, you can find a helpful guide on registering a domain with Route 53, which is AWS’s DNS and domain registration service.

Here are the main steps we’ll be covering:

  1. Create an ACM Certificate: ACM stands for Amazon Certificate Manager. It allows you to create certificates to support TLS (Transport Layer Security) for secure connections. You need to create a certificate that matches your domain name.
  2. Set Up a Custom Domain Name: We’ll set up a custom domain name for your API Gateway using the AWS CLI.
  3. Map the API Gateway to the Custom Domain: You’ll map your API stages to the custom domain so that it routes correctly.
  4. Update DNS Records: Finally, you’ll update the DNS records in AWS Route 53 to connect your domain to the API Gateway.

Prerequisites

Before you begin, ensure you have the following prerequisites in place:

  • AWS CLI installed and configured with necessary permissions.
  • A registered domain or access to domain settings (Route 53).

Now, let’s walk through each step using the AWS CLI:

Step 1: Create an ACM Certificate

First, create a certificate in the ACM for your domain. Use the following AWS CLI command, replacing <your-domain> with your actual domain:

aws acm request-certificate --domain-name <your-domain>

Step 2: Set Up a Custom Domain Name

Now, create a custom domain name for your API Gateway using the AWS CLI. Replace <your-domain> and <prefix> with your domain name and the desired API prefix:

aws apigateway create-domain-name --domain-name <your-domain> --security-policy TLS_1_2 --endpoint-configuration types=REGIONAL

Step 3: Map the API Gateway to the Custom Domain

Map your API Gateway to the custom domain you just created. Replace <your-api-id> with your API ID and <your-stage-name> with the stage you want to map to the custom domain (e.g., “dev”):

aws apigateway create-base-path-mapping --domain-name <your-domain> --rest-api-id <your-api-id> --stage <your-stage-name>

Step 4: Update DNS Records

To complete the setup, you’ll need to update the DNS records in AWS Route 53. This step is essential to connect your domain to the API Gateway. The AWS ACM will provide you with specific DNS records to add. Use the following commands, and replace the placeholders with the actual values:

# Add a DNS record to Route 53
aws route53 change-resource-record-sets --hosted-zone-id <your-hosted-zone-id> --change-batch '{
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "_57<your-domain>",
        "Type": "CNAME",
        "TTL": 300,
        "ResourceRecords": [
          {
            "Value": "<provided-record-value>"
          }
        ]
      }
    }
  ]
}'

Be sure to check that the record you just added has propagated by running the following command:

# Verify DNS record propagation
nslookup my-apis.<your-domain>

In this command, replace <your-domain> with your domain and <your-hosted-zone-id> with your Route 53 hosted zone ID.

After completing these steps, you’ll have set up a custom domain for your AWS API Gateway using the AWS CLI. Remember that DNS changes may take some time to propagate, so be patient if the custom domain doesn’t work immediately. Once propagation is complete, your API will be accessible via the custom domain, providing a more professional and branded experience for your users.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top