DEV Community

Cover image for How to Create a FREE Custom Domain Name for Your Lambda URL - A Step by Step Tutorial
Omar Omar
Omar Omar

Posted on • Updated on

How to Create a FREE Custom Domain Name for Your Lambda URL - A Step by Step Tutorial

Introduction:

On the 6th of April 2022, AWS announced Lambda Function URLs with a Built-in HTTPS Endpoint for Single-Function Microservices. The new feature allows users to configure an HTTPS endpoint for a Lambda function without employing any additional resources such as AWS API Gateway or Application Load Balancer. AWS describes it as a highly available, scalable and secure HTTPS service. The newly added feature was well received by the community. I called it, "one small step for FaaS, one giant leap for Serverless".

There are several articles published to discuss and introduce the newly released feature. Aj Stuyvenberg, an AWS Community Builder, posted a well written article about the Lambda Function URLs titled, Introducing Lambda Function URLs. Another worth reading article was written by Serverless Framework. Moreover, AWS documentation at your fingertips to dig a bit deeper.



Image description

With that being said, it's known that the Lambda Function URLs feature does not support custom domain names out of the box; therefore, I have decided to write this step by step tutorial to walk you through how to create a free custom domain name for your Lambda URL. The solution utilizes AWS Route 53 hosted zone, AWS CloudFront distribution, AWS Certificate Manager and Freenom for a free custom domain name registration. In addition, the Lambda function is to host a static page for illustration purposes. To reiterate, the goal of the tutorial is to provide a step by step tutorial on how to create a custom domain name for a Lambda Function URL. Alright enough babbling, let's get at it.

NOTE: AWS Route 53 hosted zone costs $0.50/month.


The Solution Architecture Diagram:

Image description


Tutorial Steps:

  1. Obtain a free domain name from Freenom
  2. Create a Lambda function to host a static page
  3. Create a hosted zone in AWS Route 53
  4. Request a Public SSL Certificate from AWS Certificate Manager
  5. Create an AWS CloudFront distribution for the Lambda URL
  6. Create Route 53 A-record for the CloudFront distribution and confirm the custom domain is functioning
  7. Do your victory dance πŸ˜‰

NOTE: throughout the tutorial, leave default options as is unless instructed otherwise.


Step 1: Obtain a free domain name from Freenom

1- Navigate to Freenom and create a free account.
2- Select Register a New Domain as shown below.

Image description

3- Check the availability of a domain name of your choosing. In my case, it’s lambda.cf as it's available and free πŸ˜‰. Now, We are ready to Checkout.

Image description

4- Choose the Period for your selected domain. It ranges from 1 to 12 months. Then, click Continue.
5- Read the Terms & Conditions and select it once you're done. Finally, click Complete Order to register the free domain name.

Congratulations. You have successfully registered a free domain name.

NOTE: it might take an hour or so for the domain registration to take effect.


2 Create a Lambda function to host a static web page

1- From the AWS Lambda console, click Create function.
2- Give your function a name and select Python 3.9 for a Runtime. Under Advanced settings, check Enable function URL and select None for Auth type. Then, click Create function.

Image description

3- Copy and paste the below Python code into the body of the lambda_function as shown below and then click Deploy.


#******************************************************************************************
# Author - Omar A Omar
# This lambda function will act as a static web page
#******************************************************************************************


def lambda_handler(event, context):
    response = {
        "statusCode": 200,
        "statusDescription": "200 OK",
        "isBase64Encoded": False,
        "headers": {
        "Content-Type": "text/html; charset=utf-8"
        }
    }

    response['body'] = """
    <html>
        <head>
            <title>Lambda URL</title>
            <style>
                html, body {
                background-color:rgb(22, 30, 43);
                margin: 10; padding: 10;
                font-family: arial; font-weight: 10; font-size: 1em;
                text-align: center;
                }
                html, h1 {
                color: white;
                font-family: verdana;
                font-size: 150%;
                }
                html, p {
                color: white;
                ont-size: 50%;
                }
            </style>
        </head>
        <body>

            <h1>Hello Friend!</h1>
            <p style="color:White;">I'm a static web page running on a Lambda function</p>
            <img src="https://media.tenor.com/YhKAJhNKFeoAAAAC/dance-dancing.gif" width="450" />
        </body>
    </html>
    """

    return response
Enter fullscreen mode Exit fullscreen mode

Image description

4- Click on the Function URL to open the lambda static page.

Image description

Congratulations. Now, you have a Lambda function hosting a static page.

NOTE: If you're planning to implement weight traffic shifting and safe deployments, AWS recommends associating an alias with your Lambda function. Please, refer to Announcing AWS Lambda Function URLs: Built-in HTTPS Endpoints for Single-Function Microservices for more information.


3 Create a Hosted Zone in AWS Route 53

1- From AWS Route 53 console, click on Hosted zones. Then, click Create hosted zone.

Image description

2- Add the Domain name that we have previously obtained from Freenom. In my case, it's lambda.cf. Then, click Create hosted zone.

Image description

3- Now, we will need to copy the name servers (type NS) to Freenom for our domain name to point to this hosted zone. Let's copy the four name servers, as shown below, to Freenom.

Image description

4- On Freenom, click on Manage Domain and then on nameservers.

Image description

Image description

5- Select Use custom nameservers (enter below) and paste the four name servers from our Route 53 hosted zone one at a time. Finally, click Change Nameservers.

Image description

NOTE: Don't copy the period at the end of the name servers from our Route 53 hosted zone. At this point of time, we are done with Freenom, you can signout and close the tab.


4 Request a Public SSL Certificate from AWS Certificate Manager

1- From the AWS Certificate Manager console, click Request. Keep the default, Request a public certificate selected and click Next.

Image description

2- Under Fully qualified domain name, type in your domain name . Then, click Add another name to this certificate and add an asterisk and period *. as a wildcard to your domain name.

NOTE: Adding the wildcard to the second qualified domain name allows us to use the same certificate for other CNAMEs and A-records. If you don't know what a CNAME is and can't differentiate between types of DNS records, it's a great time to fill in the knowledge gap. I would highly recommend taking a look at the AWS documentation for more information.

Image description

No other changes are needed. Click on Request to submit the certificate request. Notice and as shown below, the Status is Pending validation. We still need to complete one more step which is validation.

Image description

3- Let's validate the certificate. Under Certificate ID, Click on the newly created certificate ID. We will be directed to the certificate status page and under Domains section, click Create records in Route 53.

Image description

4- I believe we had to add the validation manually but AWS added this easy to use feature. Now, we automatically add the DNS records to our hosted zone in Route 53 by clicking Create records.

Image description

NOTE: If you faced any hiccups adding the validation automatically, copy the CNAME name and CNAME value from under Domains section and use them to manually create a validation CNAME record in our hosted zone.

5- To confirm that the validation CNAME record has been added correctly, head back to our hosted zone and we should see a CNAME record added to the list of DNS records in our hosted zone as shown below.

Image description

NOTE: Don't forget to refresh the records in the hosted zone if the newly added validation CNAME record has not showed up . Also, the Status of the certificate in AWS certificate Manager might take a few minutes to become Issued. Therefore, take a break, grab a coffee, or even do a flip πŸ˜„

Image description


5 Create an AWS CloudFront Distribution for the Lambda URL

1- From the CloudFront console, click Create distribution.
2- The Origin domain is your Lambda Function URL.
3- Protocol: HTTPS only
4- Leave Origin path empty.

Image description

5- For the Name, you can change it to a name of your choosing instead of the default name created by CloudFront.
6- For Viewer, select Redirect HTTP to HTTPS.

7- Under Settings, click Add item and add your domain name. In my case, it's lambda.cf.

8- From Custom SSL certificate-optional, let's select our certificate from the drop down menu. Then, click Create distribution.

Image description

NOTE: Note, as shown below, CloudFront distribution might take a few minutes to propagate the deployment around the globe; therefore, take a break, grab a cup of coffee, or even do a flip, why not πŸ˜„

Image description

9- Once the deployment is completed, click on your distribution ID. Copy and paste the Distribution domain name into your browser. We should see our static page.

Image description

Image description

Congratulations. We now have our Lambda URL distributed to all AWS edge locations using the world's highest performant, secured and convenient content delivery network. Yep, it's CloudFront.


6 Create a Route 53 A-record for the CloudFront distribution

1- Let's head once more to Route 53 console and add an alias record. From our hosted zone, click Create record.

Image description

2- Keep Record types as an A-record. But, we need to toggle the Value to Alias. Then from the drop down menu, select Alias to CloudFront distribution. Here, we will paste our Distribution domain name from CloudFront console as shown below. Bear in mind, the Distribution domain name should show up as a selectable option. This is the distribution we have created for our Lambda URL in CloudFront. Finally, click Create records.

NOTE: If you're copying and pasting the CloudFront Distribution domain name, remove the protocol from the URL as shown below (remove https://).

Image description

Let's check whether or not our custom domain name for the Lambda URL works. It's the moment of truth πŸ™Š Let's paste our custom domain name into our browser. Wait for it...Wait for it...I hope you're doing your victory dance by now πŸ₯³

Image description


Conclusion:

I might have congratulated you multiple times during the tutorial but it does not hurt to say it one more time, CONGRATULATIONSπŸ’₯.

We have successfully implemented a solution that employs an AWS Route 53 hosted zone, AWS CloudFront distribution, AWS Certificate Manager and Freenom for a free custom domain name registration. Utilizing this solution, we have given our Lambda function URL a custom domain name.

I hope this tutorial adds value to your learning curve, and I can't wait to see what the future unpacks for Lambda Function URLs. Great things indeed.

Latest comments (0)