Setup Custom Domain for an API in AWS API Gateway

Zeeshan BaigBlog

Overview

AWS API Gateway is a service that enables you to create, publish, maintain, monitor, and secure APIs at any scale. You can create APIs that access AWS or other web services such as Lambda functions or services running on EC2 instances.

By default API gateway generates a long and complex URL for your APIs (as shown in the slide), it supports HTTPs. Good news is that you can also create a custom domain name for these endpoints with full HTTPs support. This will allow you to access API using familiar and short URLs such as https://api.example.com.

In order to set up a custom domain for an API you need to generate SSL certificates, you can do this using AWS Certificate Manager (ACM) check this post to see how to generate SSL certificates using AWS certificate Manager (ACM)

You also need a custom domain name, in our case, we have a domain in Route 53 and wildcard certificate generated in ACM.

Setup Custom Domain

Log in to AWS API Gateway console using AWS console

Click on Custom Domain Names and enter details as follows

  • Domain name – this would be your main endpoint to access the API
  • Endpoint configuration – we selected Regional, Edge Optimized would use AWS CloudFront to serve the API endpoint
  • ACM Certificate – we have a wildcard certificate generated for our domain
  • Click Save

 

 

Next is to add base path mappings, an important concept to note here that this will replace your API stage name in the URL

For example, in the following slide, we have entered Path as “v1” and selected our API destination “my-api” and stage name as “test”

The default API endpoint would be as follows

https://{restapi_id}.execute-api.{region}.amazonaws.com/{stage_name}/

e.g. https://api-id.execute-api.us-east-1.amazonaws.com/test/

this would replace the {stage_name} with the Path mapping

https://api.example.com/v1

 

 

After entering Path mapping API gateway would display Target Domain Name, copy the Target Domain Name we need to enter to our DNS records.

 

Set up DNS Record

Now create a DNS record of type CNAME in Route 53, with the record name such as api.example.com

Note: You can also use your own DNS management systems, steps would be similar

 

 

Testing the API

Once these settings are set up test the API in POSTMAN or tool of your choice

Note: Make sure to use the modified URL with Base Path mapping

Hope you find this post useful, please leave a comment for any questions or suggestions.

@IamZeeshanBaig

About DataNext

DataNext Solutions is a US based system integrator, specialized in Cloud, Big Data, DevOps technologies. As a registered AWS partner, our services comprise of any Cloud MigrationCost optimizationIntegration, Security and Managed ServicesClick here and Book Free assessment call with our experts today or visit our website www.datanextsolutions.com for more info.