Building a Serverless Blog Engine with AWS Lambda, API Gateway and DynamoDB
I'm excited to introduce a project which I've been working on, and also I'm thrilled to share with you all through this article. It involves " Building a Serverless Blog Engine with AWS Lambda, API Gateway and DynamoDB".
Project Description:-
Building a serverless blog engine with AWS Lambda, API Gateway, and DynamoDB involves creating a scalable, cost-effective, and easily maintainable blog platform. It offers a scalable and cost-effective solution for hosting a blog with minimal infrastructure management.
AWS Services:-
Services Description:
In order to perform these operations, we have to write our backend logic. We will create a Lambda Function for that. Then we build a Blog API using Amazon API Gateway and integrate that with our lambda function. When a user invokes our Blog API, API Gateway routes the request to our Lambda function. The Lambda function interacts with DynamoDB and returns a response to API Gateway. API Gateway then returns a response to the end user.
Procedure to build this blog:-
2. Steps to create a Lambda Function:-
const AWS = require("aws-sdk"); const dynamoDB = new AWS.DynamoDB.DocumentClient({ region: ‘us-east-1’, apiVersion: ‘2012- 08-10’, }); exports.handler = async (event, context, callback)=>{ if(event.id === undefined){ if(event.httpMethod === "GET"){ // retrieve all blogs from database const params = { TableName: "blog-database" } await dynamoDB.scan(params).promise().then((data)=>{ const restructuredData = data.Items.map((newData)=>{ return ({"blogId": newData.blogId, "title": newData.title, "content": newData.content, "author": newData.author}); }); callback(null, restructuredData); }).catch((err)=>{ callback(err); }) } else if(event.httpMethod === "POST"){ // create a blog in the database const params = { Item: { blogId: "blog_"+Math.random(), title: event.title, content: event.content, author: event.author }, TableName: "blog-database" } await dynamoDB.put(params).promise().then((data)=>{ callback(null, data); }).catch((err)=>{ callback(err); }) } else{ callback(null, Method: ‘${event.httpMethod}’ is not allowed); } } else{ if(event.httpMethod === "GET"){ // retrieve a particular blog from database const params = { Key: { blogId: event.id }, TableName: "blog-database" } await dynamoDB.get(params).promise().then((data)=>{ const restructuredData = { "blogId": data.Item.blogId, "title": data.Item.title, "content": data.Item.content, "author": data.Item.author } callback(null, restructuredData); }).catch((err)=>{ callback(err); }) } else if(event.httpMethod === "PATCH"){ // updade a particular blog in database const params = { Key: { blogId: event.id }, UpdateExpression: set ${event.key} = :value, ExpressionAttributeValues: { ":value": event.value }, TableName: "blog-database", ReturnValues: "UPDATED_NEW" }; await dynamoDB.update(params).promise().then((data)=>{ callback(null, data); }).catch((err)=>{ callback(err); }) } else if(event.httpMethod === "DELETE"){ // delete a particular blog from database const params = { Key: { blogId: event.id }, TableName: "blog-database" } await dynamoDB.delete(params).promise().then((data)=>{ callback(null, data); }).catch((err)=>{ callback(err); }) } else{ callback(null, Method: ‘${event.httpMethod}’ is not allowed); } } }
3. Setup API Gateway:-
Now, we will create our Blog-API. We will use the API Gateway service. Let’s understand what we are gonna create, what will happen behind the scenes of the API---
We will create an API that will have the following routes:
To achieve this, follow the steps below —
{
"httpMethod": "$context.httpMethod"
}
Recommended by LinkedIn
Similarly, create the following API Resources and Methods —
#set($inputRoot = $input.path('$'))
{
"httpMethod": "$context.httpMethod",
"title" : "$inputRoot.title",
"content" : "$inputRoot.content",
"author" : "$inputRoot.author"
}
{
"httpMethod": "$context.httpMethod",
"id" : "$input.params().path.id"
}
#set($inputRoot = $input.path('$'))
{
"id" : "$input.params().path.id",
"httpMethod": "$context.httpMethod",
"key" : "$inputRoot.key",
"value" : "$inputRoot.value"
}
{
"id" : "$input.params().path.id",
"httpMethod": "$context.httpMethod"
}
4. Testing our Blog API:-
{
“key”: “author”,
“value”: “arindam369”
}
I sincerely thank to My faculty mentor Dr.Kavitha Sadam mam.
The code in your lambda section does not copy & paste well and had at least 1 issue that prevented it from working. One thing, for certain, that would make that a more straightforward lift & shift would be to use encapsulated comments /* like this */ instead of the unterminated version // like this. I'm going back through everything again at the moment as it's all there, and all wired up, but isn't working. Still, happy for the headstart this article provided.