Custom Domain for Lambda Function

A lambda function can be associated with API Gateway. However, the default URL would be difficult to pass. To get a good-looking URL, follow this post.

Lambda Function

Let’s create a lambda function.

Select ‘Author from scratch’

Our function will be a simple one that simply says Hello {user}!

Register a Domain/Hosted Zone

We can buy a domain from Route 53 or other providers like GoDaddy.com.

We can emulate a free domain using ClouDNS. This actually gives us a DNS Zone for free which can be used for our purpose.

Create a new DNS Zone. Assign a name, e.g. user10.cloudns.ph

Go to Route 53. Click on ‘Hosted Zones’ and then ‘Create Hosted Zone’

In the domain name, enter any name; this will be the sub-domain of the one created in ClouDNS, e.g. myapi.user10.cloudns.ph

This will create some Namespace records like below.

Go back to the ClouDNS console. Click on the create DNS Hosting Zone. Then click ‘Add new record’.

Add as follows. In the ‘Points to:’ section, add the NS records shown in Route 53 above. You need to repeat it for each of the four records.

Our ClouDNS Zone page should loo like this

API Gateway

Go to API Gateway -> REST API

(Use the first option; the second one has the word private next to it)

Set API name as hello-api

Click ‘Create API’

Go to Actions -> Create Resource

Set ‘Resource Name’ as ‘predict’
Click on ‘Create Resource’

Go to Actions -> Create Method

In the drop-down select ‘POST’. Then click on the check mark. Click ‘Save’

On the following screen, select ‘Integration type’ as ‘Lambda Function’ and ‘Lambda Function’ as ‘HelloWorld’

Click on ‘Test’

In ‘Request Body’, type:

{"username": "Max"}

You will see ‘Hello Max!’ in ‘Response Body’ on the right.

Go to Actions -> Deploy API

  • Deployment stage: [New Stage]
  • Stage name: test

It will give you an ‘Invoke URL’.

This URL can be used in a test script as follow:

Create a test.py file

import requests
url = 'https://7j9znyubc2.execute-api.us-east-2.amazonaws.com/test/predict'

data = {'username': 'Max'}
response = requests.post(url, json=data).json()
print(response)

Certificate

We need to create a certificate to access the URL with HTTPS.

Go to AWS Certificate Manager. Select ‘Request a certificate’

Select ‘Request a public certificate’

Enter the domain name. Select ‘DNS Validation’.

Refresh the list. Click on the certificate.

In order to validate the request, we need to add a CNAME record in Route 53. ACM can do this automatically for us.

Click ‘Create records in Route 53’

Click ‘Create records’

Wait for some time. Its Status will change to ‘Success’

API Custom Domain

We need to create an ‘API Custom Domain’ that can be used for our API instead of the Invoke URL.

In API Gateway, click ‘Custom Domain Names’ and then ‘create’

You will get the following screen.

Note the ‘API Gateway Domain Name’. We will create a Record in Route 53.

The type of the record will be ‘A’.

An API mapping will be created to map to a particular stage.

Enter the API name and stage.

Now we can change the test.py file to

import requests
url = 'https://myapi.user10.cloudns.ph/predict'

data = {'username': 'Max'}
response = requests.post(url, json=data).json()
print(response)

Author | MMG

Learning...