How to Create a FREE Custom Domain Name for Your Lambda URL - A Step by Step Tutorial
Tags: serverless, awscommunitybuilder, tutorial, aws
Published: 2022-04-10 05:42:44


## Introduction:

On the 6th of April 2022, [AWS announced Lambda Function URLs with a Built-in HTTPS Endpoint for Single-Function Microservices](https://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-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](https://dev.to/aws-builders/introducing-lambda-function-urls-4ahd). Another worth reading article was written by [Serverless Framework](https://www.serverless.com/blog/aws-lambda-function-urls-with-serverless-framework). Moreover, [AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html) at your fingertips to dig a bit deeper. 
<br />
<br />



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ogr4xcr4hslxw8uzozq.png)
<br />

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. 
<br />

> **_NOTE:_**  [AWS Route 53 hosted zone costs $0.50/month](https://aws.amazon.com/route53/pricing/#:~:text=Hosted%20Zones%20and%20Records). 

---


### The Solution Architecture Diagram: 
<br />
<br />


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysi562uvswj3fi9iv8lw.png)

---
<br />


## Tutorial Steps:

1. Obtain a free domain name from [Freenom](https://www.freenom.com/en/index.html?lang=en)
2. Create a Lambda function to host a static page 
3. Create a hosted zone in [AWS Route 53](https://aws.amazon.com/route53/)
4. Request a Public SSL Certificate from [AWS Certificate Manager](https://aws.amazon.com/certificate-manager/)
5. Create an [AWS CloudFront](https://aws.amazon.com/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 :wink:

> **_NOTE:_**  throughout the tutorial, leave default options as is unless instructed otherwise.

---
<br />
<br />

## Step 1: Obtain a free domain name from Freenom

1- Navigate to [Freenom](https://www.freenom.com/en/index.html?lang=en) and create a free account.
2- Select **Register a New Domain** as shown below.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ps47hsgg22gd1pet22lq.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d50371v0fhmihbbtpwke.png)


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. 

---
<br />
<br />

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzbgc5fyorl6uxagv399.png)

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


```python

#******************************************************************************************
# 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
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0og4ux061f0d75ir55y1.png)


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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ot4fvggn6ayjc3mu0kj4.png)

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

<br />

> **_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](https://aws.amazon.com/blogs/aws/announcing-aws-lambda-function-urls-built-in-https-endpoints-for-single-function-microservices/#:~:text=How%20Lambda%20Function%20URLs%20Work) for more information. 


<br />
<br />


---
<br />
<br />

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2yk9c5fgrp42rghwczx.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p9aluadu5pb31i3sbw8y.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/59cwxgol5gz2l8nqmya9.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cty7shzbpbn4g53scsdd.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3d8txz3k2n18j2kqz1u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o6b25v9ns0n9gehe23r6.png)


> **_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.

<br />

---
<br />
<br />

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lt7eacome91u8bb8lzuw.png)

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](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/ResourceRecordTypes.html) for more information. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvp37v5817hm9clfhspa.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gk2najqm7gwf2dd3qbwc.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xvcrpgd9kfdfctfo572q.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4x5qw5w1xbfo8c3q3c6.png)


> **_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pbv9s00k9yafziekj5fa.png)

> **_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 :smile:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3g598h4rx2rvkdww7sg.png)

---
<br />
<br />

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80bzal7euxysrsyxmq85.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0x56kjjyt0jxrv2wtbew.png)

> **_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 :smile:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmgmodnf6nsgi3g9bdua.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj4kqvunggram91s9uqj.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3c8nnmi9y8mwccpqy9qi.png)

**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.**

---
<br />
<br />


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ut35pm2902ge2ny9h7o0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4yfd1ep4a0r6qwd49suk.png)


Let's check whether or not our custom domain name for the Lambda URL works. It's the moment of truth :speak_no_evil: 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 :partying_face:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yv8yeyuj70cci0h4tn65.png)

---
<br />
<br />

## Conclusion:

I might have congratulated you multiple times during the tutorial but it does not hurt to say it one more time, **CONGRATULATIONS**:boom:. 

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.