launch website with aws lambda


Lamba is a functionality within the AWS environment where you can write functions and invoke them based on user parameters. The goal of lambda is to go serverless, you don’t have to manage servers and capacity planning. Lambda is designed to scale. You will be charged when the lambda function is triggered. You are probably aware of all the theory behind lambda :). So how can you create a website without a server?

  1. Within a lambda, you get a temporary storage (for temporary files if needed)
  2. You can store files (which can be css/js/images and content) in S3 or Dynamodb or RDS  etc and then Lamdba can read files/write from/to there.
  3. Your lambda function can be written to get the query parameter and get respective files from different sources to produce a page.

When I started looking in it it’s confusing from documents that I found on the internet. We will go through the whole process of creating a domain and pointing it to a lambda function. It’s a multistep process which involves Route 53, Lambda, API Gateway, ACM (Certificate Manager) and S3, etc for data sources.

  1. You need to have a registered domain (preferably on Route 53)
  2. You need to create a lambda function
  3. You need to associate API gateway with a lambda function.
  4. Goto AWS Certificate Manager to create Certificate.
  5. Goto API gateway’s “Custom Domain Names” to create a custom domain.
  6. You need to add CNAME entry in Route 53 once step3 is done.
  7. Edit Base Path Mappings in API gateway’s “Custom Domain Names”

We will go with all the above steps.


Domain setup in Route 53.

As you can see we have root domain defined in Route 53 DNS servers.

We will be creating a subdomain weblambda.bitarray.io which will point to a lambda function.

For now, no changes required as long as you have access to your domain, we are just showing DNS settings for this tutorial. Do NOT create subdomain at this point.

 


Create lambda function

Create a lambda function under AWS->Lambda.

  1. give a name to a function
  2. we will be using python3.7 environment for this tutorial
  3. leave the permissions as is.

 

Once you hit “Create function” it will take to you to a page where you can edit/code and do function related changes. It will load a basic event handler code (by default).

 

You can copy below code (if you want to ) to above editor or leave as is (goal of this tutorial is not about coding but how to set up the environment so that lambda function can be triggered by an external URL.

Throttle settings, capacity limits etc can be configured here, we can discuss that in another tutorial.

Below is the code for our lambda function:

import json


def lambda_handler(event, context):
    # TODO implement
    x= "this is a test";
    try:
     p=event["queryStringParameters"]['q']
    except:
     p=""

    return {
        'statusCode': 200,
        'body': json.dumps('Hello bitarray!' + x + p)
    }

 


API Gateway setup

Goto  AWS->API Gateway Service

Click on “Create API”

Chose Rest API, and provide a name “lambdaweb” or any other name and save it.

 

 

Then you will go to the resources section of API where you need to add a method like GET, POST etc.

Select Integration type as Lambda ( because we will be calling lambda function through this gateway) you can select which lambda function will be triggered.

 

Save it and you will see execution plan details like below.

You can click on “Test” to see if API Gateway (lambdaweb) can execute lambda function and returns without any error.

 

Now we need to deploy the API.

From Actions “select deploy API”

 

Next step is with AWS Certificate Manager or ACM.


AWS Certificate Manager

Goto AWS certificate Manager. select “Request a certificate” then “Request Public Certificate”. Click Next , it will ask for the validation method, chose DNS or email validation type. If you chose DNS it will show below the screen and you can click on “create record in Route 53”, It will create record in route 53 automatically. Do NOT delete this ACM certificate.

once it’s issued you will see below:

 

 

Next thing is to do with API Gateway’s “Custom Domain Names“. Click on “Custom Domain Names as highlighted above”

 


API Gateway – Custom Domain Name

Create Custom Domain.

Under domain use the same name which we used to create ACM certificate.

From Drop Down select the certificate that we created.

 

once you save it, it will take 40 mins!! You will see “initializing” for around 40 mins.

Take a not at “Target Domain Name”. This will be used as CNAME entry for weblambda.bitarray.io! (which is our next step)

Once initialization is done you can launch weblambda.bitarray.io


Route 53

From previous exercise we got details for Target Domain Name. We will use this as CNAME for weblambda.bitarray.io.

goto Route 53 and create a new recordset like beow.

 

now we need to wait while Custom domain name initialization is done.

Once it’s done add base path. (You need to tell custom domain wat API to trigger and what path do you want to add on top of the subdomain.

As shown below we don’t have any value in base path which means we can launch lambda directly by calling https://weblambda.bitarray.io else it would be https://weblambda.bitarray.io./<base path>

The destination needs to be APIs. You can have multiple base paths (off course with different paths)

 

Finally when we launch it via web browser

below is the output