This is Part-2 of a multi-part blog post of how you can run a fast dynamic website in a completely serverless manner using managed services provided by AWS.

In the previous post, we have discussed how to setup a static website using S3.

Every website has at least one dynamic section like contact us, email subscription or a feedback form. When hosting the website using serverless technologies such as Amazon S3, APIs are essential for powering the dynamic sections of the website. Amazon API Gateway gives us the ability to create any number of arbitrary APIs using Amazon Lambda as the backend. The best thing about these two services is, their Free Tier quota is quite high so you won’t have to pay anything until your website gets very popular.

Lets create a contact-us api for a website. First we need a data store to keep our contact-us form data, and for that we will use Amazon DynamoDB (NoSQL Database). Then we need an Amazon Lambda function to process the POST requests of the contact-us form. And finally we need an API endpoint to call the Lambda function from the website for that we will use Amazon API Gateway.

Logon on AWS Management console and select DynamoDB and follow these steps.

Click Create table. Enter mywebsite-contact-us as Table name. Enter email as Partition key. Leave the rest of the settings as it is and click Create.

Note: Table creation may take few minutes.

Once the table is ready, lets write a Lambda function in Node.js to store contact-us form data into this table.

Logon on AWS Management console and select Lambda and follow below steps.

Click Create a Lambda function. Select Blank function. Skip Configure Triggers and click Next. In Configure function, set mywebsite-contact-us as Name and select Nodejs 8.10 as Runtime. Copy below function to Lambda function code.

6. Select Create a custom role as Role.

7. Set mywebsite_lambda_role as Role Name and Click Allow.

8. Click Next and then Create function.

The Lambda function needs permissions to store data in the DynamoDB table mywebsite-contact-us. Logon on to the AWS Management console and select IAM and follow these steps to set required permissions.

Click on Roles and select mywebsite_lambda_role. Under Inline Polices select Create Role Policy. Select Custom Policy and enter mywebsite-contact-us-table as Policy name. Copy and paste the following policy.

Logon on to the AWS Management console and select API Gateway and follow these steps.

Click “Getting started”. Click “Create API”. Select check box “Import from Swagger” Paste following swagger template and then click “Import”

The mywebsite API is created with contact-us resource and we will now have to integrate it with the Lambda function mywebsite-contact-us to process contact-us form requests from your website.

Select mywebsite API. Click on POST method under contact-us resource. For Integration type select Lambda function. Select the region of mywebsite-contact-us Lambda function. Set Lambda function as mywebsite-contact-us and click Save. Click OK to give permisison for API Gateway to call mywebsite-contact-us Lambda function. Click on OPTIONS method under contact-us resource. For Integration type select Mock and click Save. Click Enable CORS under Actions for Cross-Origin Resource Sharing. This is needed for posting contact-us form data to this API from your website.

The mywebsite API is created now and we need to deploy it so that we can make calls to the API.

Select Deploy API under Actions. Select New Stage and Set test as stage. Keep Invoke URL handy as we will need this for testing.

Now everything is ready and we need to make sure everything is working as expected.

We use Postman for API testing but feel free to use any REST client. Our testing URL will be Invoke URL appended with contact-us, for example the testing URL should look like https://abcde12345.execute-api.ap-southeast-1.amazonaws.com/test/contact-us. Select method as POST and use the testing URL. Post following JSON as request body.

{

"email": "info@tensult.com",

"name": "Dilip",

"message": "I am just testing :)"

}

5. Logon on to the AWS Management Console, and navigate to DynamoDB. Here check the items of the table mywebsite-contact-us and you should find one entry with the above details.

We have covered only few aspects the website here and there is lot to cover so in upcoming posts, so watch out for future posts in this series.

Try this to create your first API and please let me know how did it go.

What’s next?

You want to learn more about AWS DynamoDB then you can also read this blog.