35

I have created an AWS Lambda with the new function URL feature enabled.

Since the URL isn't that easy to remember, I would like to create a Route 53 alias like lambda.mywebsite.com.

There is no Route 53 alias for Lambda function URLs in the drop-down menu for aliases in Route 53.

How can I call my AWS Lambda function URL via a custom domain?

Is a CNAME record the way to go?

3 Answers 3

39

How can I call my AWS Lambda function URL via a custom domain?

If you must use an AWS Lambda function URL, fronting it with a CloudFront distribution with your desired custom domain name is the only way currently.

There is no support currently for a Route 53 alias record, as the Host header must be set to the Lambda function URL domain. If the Host header does not exist, HTTP 400 Bad Request is returned & if it does exist but is not the correct value, HTTP 403 Forbidden is returned.

They are meant to be the simplest & fastest way to invoke your Lambda functions via a public endpoint without using other AWS services like API gateway, so a native lack of support for a custom domain name makes sense.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the answer! do you know why invoking the lambda function URL via the browser seems to invoke the function, but invoking the same URL via postman does not invoke the function and gets an error saying the server cannot be reached?
Have you actually tried this or are you guessing? Reason I ask is that I tried it and accessing via the on.aws URL works fine, but going via my CNAME gets a 403.
No. It's a private Route 53 zone entry. But other than being private, it's just a CNAME to the function URL. Edit: Now that I think about it, it's technically a CNAME created via CloudMap. Not sure if that makes a difference.
I contacted AWS Support. The issue is that you have to include a Host header with the hostname from the function URL which really defeats the entire purpose of creating the CNAME in the first place.
@JasonK Thank you for flagging this, I've now manually tested a few options and you're correct. I've updated my answer to reflect part of your findings and a solution that actually works (which I've manually tried)
|
10

If you use CloudFlare for your DNS services you can use Redirect Rules under the Rules tab. For simple projects the DNS management is free, and you get up to 10 free Redirect Rules.

More info here: https://developers.cloudflare.com/rules/url-forwarding/single-redirects/create-dashboard/

For my purposes in CloudFlare I set up a Static redirect to my Lambda URL provided in the AWS Console, with a 307 Status Code: enter image description here

5 Comments

Do you need to add a record to your DNS ?
@MervinHemaraju, you need to use CloudFlare as your DNS provider to do this. The Redirect Rules don't require any additional manual configuration to your DNS records. If you are just looking to setup basic DNS services with ClouldFlare view their general setup guide. developers.cloudflare.com/learning-paths/get-started/…
I am already using cloudflare for my main domain something.com. But i want my subdomain let's call it test.something.com to call my lambda function. Should i add a record for test.something.com ?
Note that this approach does not preserve the payload. It doesn't work in my case because the authorization header is missing. See community.cloudflare.com/t/…
If you return a 307 (not 302), the client should follow the redirect using the original method (e.g. POST). I'm using this method today, with CF and function urls, and the POSTed data is getting there.
9

I tried the same once lambda url feature was out. Following are the steps:

  1. Create a cloudfront distribution with lambda url as https endpoint, add the domain name in the alternate domain name field.
  2. Create a A record for the domain in Route53 and point it to the cloudfront distribution.

Note: If in case you haven't added domain certificate in step1 then the cloudfront option in step2 wont show any value to select.

3 Comments

do you know why invoking the lambda function URL via the browser seems to invoke the function, but invoking the same URL via postman does not invoke the function and gets an error saying the server cannot be reached?
@Crashalot Strange. Perhaps Cloudfront has some WAF/bot-check. A browser request will pass that (it has a user agent etc) but perhaps it thinks your Postman call is a bot and so rejecting it before it even hits the function
super helpful, thanks. one thing to note is when you create the A record, select the type "Alias" THEN you can specify "Cloudfront", and the cloudfront you created should be listed. (Normal A record requires an IP address)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.