Serverless Applications with AWS Lambda, API Gateway, and DynamoDB deployed with Terraform

Nick Rondeau
Geek Culture
Published in
2 min readAug 6, 2021

In this scenario we’re going to create a serverless application. We will be using cloud services like AWS API Gateway, Lambda and DynamoDB deploying them using Terraform.

The lambda function will have both read and write access to the DynamoDB table.

Serverless computing is a cloud computing model in which a cloud provider automatically manages the provisioning and allocation of compute resources. This contrasts with traditional cloud computing where the user is responsible for directly managing virtual servers.

A few things needed:

  • An AWS Account, with a user set up in IAM and all keys and such entered into AWS CLI.
  • Terraform installed

Let’s get Started by creating out Lambda function. I’ve named the file lambdademo.js

A little explanation about the expressions we used. await can be put in front of any async (Asynchronous functions) promise-based function to pause your code on that line until the promise fulfills, then return the resulting value. You can use await when calling any function that returns a Promise, including web API functions.

We used Node. js, a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node. js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

Now we need to zip the file, creating lambdademo.js

This zip will be uploaded to an S3 bucket.

$ zip lambdademo.zip lambdademo.js

Now we’ll create a file assume_role_policy.json, where we’ll authorize lambda to access other AWS services on our behalf. This file will have the following JSON object.

Now, we’ll create a file policy.json, where we’ll define what are the actions that our lambda function can perform and on which resource. It allows our lambda function to perform the above defined actions on DynamoDB table with the name of myDB.

Let’s create our main.tf file. This sets our provider, region, DynamoDB table, etc and lays out all of our resources.

Let’s take it for a test drive. terraform init

then terraform plan

After running terraform apply command, there will be a URL present in the CLI which will look like the following —

https://brqhu55tr8.execute-api.us-east-1.amazonaws.com/Prod

Let’s try a few commands to test our application like writing to our DynamoDB table. We’ll use a curl command which allows us to connect.

$ curl -X POST -d '{"operation":"write","id":"1","name":"ram"}' https://brqhu55tr8.execute-api.us-east-1.amazonaws.com/Prod/myresource

which should return {"message": "Item entered successfully"}

Perform read operation by running the following:

curl -X POST -d '{"operation":"read","id":"1"}' https://brqhu55tr8.execute-api.us-east-1.amazonaws.com/Prod/myresource

Which should then return {"message":"{"Item":{"id":"1","name":"ram"}"}

Congratulations! We have created and deployed a serverless application that can perform read and write operation on DynamoDB using a Lambda function which is invoked through API gateway call.

Thank you for reading!

--

--