Deployed serverless backend API application 
 using AWS CloudFormation for provisioning & decommissioning of Infrastructure as code

Deployed serverless backend API application using AWS CloudFormation for provisioning & decommissioning of Infrastructure as code

·

5 min read

Problem statement :

In this project, I have tried to deploy a backend API application using a serverless framework & using aws cloudFormation to implement provisioning, and decommissioning of infrastructure as code.

Main Purpose of using the serverless framework :

Instead of hosting 24*7 on an EC2 instance, we can use event-driven trigger functionality using a serverless framework so that the organization/user should not pay based on instance hour.

Required Services :

AWS Cloud, Cloud-Formation, IAM, API Gateway, Cloud Watch, Serverless framework, EC2, DynamoDB

Before getting started let's get to know what are the services used for.

what is CloudFormation?

AWS CloudFormation is a service provided by Amazon Web Services (AWS) that enables users to define and manage their infrastructure resources as code in a declarative manner.

what is IAM?

AWS Identity and Access Management (IAM) is a service provided by Amazon Web Services (AWS) that enables users to manage access to AWS services and resources securely.

what is API Gateway?

Amazon API Gateway is a fully managed service provided by Amazon Web Services (AWS) that makes it easy for developers to create, deploy, and manage APIs at any scale.

what is Cloud Watch?

Amazon CloudWatch is a monitoring and observability service provided by Amazon Web Services (AWS) that allows users to collect, view, and analyze metrics, logs, and events from AWS resources and applications.

what is a Serverless framework?

The Serverless Framework is an open-source web framework for building applications that run on serverless architectures.

what is DynamoDB?

Amazon DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS). It is designed to provide fast and predictable performance, seamless scalability, and high availability for internet-scale applications.

Required Steps :

  1. Let’s make an EC2 instance & connect first. (Image: ubuntu with t2.micro free tier)

    aws > EC2 > Launch instance > make instance details

To connect to an instance select ec2 > click connect > choose to connect > open cli in the browser, similarly, you can connect using ssh as well.

Here am connected.

  1. Since this project will be serverless we need serverless to be installed on our EC2 instance for that we are using the following commands on EC2 cli

  1. Now let’s clone the application repository from GitHub for this using aws-node-todo-application code. inside a folder that we have created already on ec2 for serverless.

And we can see all folders inside this repo also for dynamo DB I have created a serverless.yml file where I have specified the db config along with bucket details.

So here I have defined: the DynamoDB table name, billing mode, attribute type, and key type along with region, and code handler authentication as well.

Now let’s try to deploy this to make a DynamoDB table for that: sls deploy

the command required and it will give us APIs that have been specified in the serverless.yml file.

  1. Now in GitHub, since main codes are in the dev branch we need to switch to the dev branch using: git checkout dev

  1. Now since in the code uuid & aws-sdk services are used hence we need to configure our dev branch with dependencies for that npm install command required :

Now let’s make some changes in serverless.yaml as well with DynamoDB table details like region and ARN

  1. Now let’s deploy this on serverless : (I already created an account in serverless framework website and have set my org with pg619 which is specified in the serverless.yml file and required for aws-cli login and deploy)

For this command:

sls –org=pg619

choose a new API web project

specified your name and details and deploy as Y

It will create cloud formation stacks and validate details :

  1. Now let’s deploy into a serverless dashboard using sls deploy where we will get a specified 4 API link using that we need to test on the postman.

    As you can see we got GET , POST, PUT , and GET API endpoints

    1. Now when we created a user we have permitted like :

      Let’s test the POST method in postman using some sample payload, for that open postman > add collection > under collection specify 4 API Endpoints this way :

      Now let’s manipulate API endpoints :

      GET Method :

      Yes, we can fetch details as you can see in above mentioned pic.

      POST Method :

      Yes with the POST method also we can push some data into the endpoint

      Now let’s check the whole data :

As you can see GET method also works fine and shows details.

Now let’s check the serverless framework dashboard :

  1. Now for provisioning, we need to stop our instance and need to check whether API endpoints are working or not: for this EC2 > select ec2 > instance state > stop

Stopped instance.

Now if I hit the endpoint to fetch details :

Yes, the serverless framework is working fine with provisioning status.

  1. Now for decommissioning command required: sls remove so all cloud formation and stacks will be removed from aws cloud formation.

Now let's check the cloud formation that I have created: aws > cloud formation > stack > click your stack :