乐闻世界logo
搜索文章和话题

How to CNAME to Amazon API Gateway Endpoint

1个答案

1

In cloud computing and network architecture, using CNAME (Canonical Name) records is a common method for pointing one domain to another. Within Amazon Web Services (AWS), you can point a custom domain to an Amazon API Gateway endpoint using a CNAME record. Below are the steps:

Step 1: Create or Configure an API in Amazon API Gateway

First, ensure your API is configured in Amazon API Gateway. If you're new, create a new API via the AWS Management Console.

Step 2: Deploy Your API

Deploy the API to a stage, such as prod or dev. After deployment, API Gateway generates a default execution URL in the format:

shell
https://{api-id}.execute-api.{region}.amazonaws.com/{stage}

Step 3: Create a Custom Domain

  1. In the Amazon API Gateway console, select or create the API you want to use.
  2. Go to the 'Custom Domain' section.
  3. Click 'Create' or 'Add Custom Domain'.
  4. Enter the domain you want the API to use, such as api.example.com.
  5. Choose a security certificate. You can import an SSL/TLS certificate from AWS Certificate Manager (ACM) or upload your own certificate.
  6. Complete the configuration and save.

Step 4: Create Path-Based Routing (Optional)

If your API needs to support multiple paths, set up path mapping in the custom domain configuration. This allows different paths to route to different APIs or stages within API Gateway.

Step 5: Update DNS Records

  1. Go to your DNS provider (e.g., GoDaddy, Google Domains).
  2. Find the DNS management page and add a CNAME record for your custom domain.
  3. Set the CNAME value to the target domain provided by API Gateway for your custom domain, such as:
shell
api.example.com CNAME {custom-domain}.execute-api.{region}.amazonaws.com

Note that this points to the AWS endpoint for the custom domain configured in API Gateway, not directly to the API's default execution URL.

Example

Suppose you have a weather query API on API Gateway and want to access it via weather.example.com. You have created and deployed the API, and your default execution URL might be:

shell
https://123abc.execute-api.us-west-2.amazonaws.com/prod

You can set a custom domain weather.example.com for this API and add a CNAME record via your DNS provider as follows:

shell
weather.example.com CNAME weather.example.com.execute-api.us-west-2.amazonaws.com

This way, when users access weather.example.com, they are actually accessing the API configured on Amazon API Gateway.

2024年8月16日 00:24 回复

你的答案