Jozef
Jozef

Reputation: 493

AWS: How to configure Cloudfront for Custom Domain Names

My setup:

API Gateway - 10 APIs (api1, api2,...), all mapped to one custom domain name (api.xxx.com)

Route53 - api.xxx.com pointed to my Cloudfront distribution

Cloudfront - distribution created, api.xxx.com set as a CNAME

What I need to know - I would like to set Origin of this Cloudfront to this custom domain name, so I can call APIs like api.xxx.com/api1/endpoint, api.xxx.com/api2/endpoint. But how? I used API Gateway Name of my api.xxx.com Custom Domain name (xxxxxxx.execute-api.us-east-1.amazonaws.com) for default behavior Origin name and assumed that requests to all 10 APIs will be routed correctly, but it´s not happening,

What works: I created Origin name using the Invoke Url of api1 and assigned it to the Default behavior. So now, when I call "https://api.xxx.com/endpoint", api1 gets called. That makes sense, but the problem is - I need the path to the API to be the part of the URL, such as "https://api.xxx.com/api1/endpoint" so I can differentiate between them.

What doesn't work: But I need several APIs set in the distribution so I can call them like "https://api.xxx.com/api1/endpoint" and so on. And if I use Invoke URL as the Origin name for the API, I cannot attach this API name also to the URL, that returns 403. I was hoping that if I used "API Gateway domain name" of "Custom Domain Names" (after all, it has a format of xxxxx.execute-api.us-east-1.amazonaws.com), I could then use APIs in the URL, but that doesn't work. I cannot even use this "API Gateway domain name" to call individual apis through Postman. Could someone advise me on how to do it? How can I configure Cloudfront so it can call various APIs and use their routes in URL?

Upvotes: 3

Views: 2003

Answers (2)

Max Visser
Max Visser

Reputation: 647

My setup is a bit different then yours but it seems we want to accomplish the same goal.

I have four S3 buckets which I serve through cloudfront. One bucket is the root website; 3 other buckets contain 3 different admin panels

For each s3 bucket I created an seperate origin; I believe you should create an origin for each seperate api.

I added for each origin group two path patterns; I believe for your api you can have one pattern per api. A path pattern could look like /api1/* which points to the origin of api1

Not sure if you tried adding origins for all your api's.

Upvotes: 0

Jozef
Jozef

Reputation: 493

Finally found a solution, described in more detail in this discussion thread. My problem was that I was trying to use link to custom domain name (xxxxxxxxxxxx.execute-api.us-east-1.amazonaws.com) directly from Cloudfront, but I should have used "nice", readable address as Origin name and do the redirect in Route53

Working setup:

  1. In API Gateway, Custom Domain Name regional-api.xxx.com is created, endpoint type Regional (xxxxxxxxxxxx.execute-api.us-east-1.amazonaws.com).
  2. In Route53, A and AAAA records map regional-api.xxx.com to the Regional endpoint target domain name.
  3. Cloudfront distribution created that uses regional-api.xxx.com as the Origin Domain Name and api.xxx.com as a CNAME.
  4. In Route53, A and AAAA records map api.xxx.com to the Domain name of a newly created CF distribution.

Upvotes: 1

Related Questions