Serverless Rest API through Cloud formation template

Serverless Rest API on AWS with Lambda and API Gateway using SAM (Serverless Application Model)

Advance tutorial to create Serverless Rest API with Lambda, API Gateway through Cloud Formation Template and AWS SAM

Apoorv Garg

--

In this article, we will learn and explore how to create a Serverless Rest API using AWS API Gateway, AWS Lambda Function, IAM Role and Policies.

Further, we will learn how to deploy the Serverless Rest API through Cloudformation template using AWS Serverless Application Model (SAM)

Problem Statement:

We will be creating a Serverless Rest API endpoint using Cloudformation template with the following details :
1. Method: GET
2. Path parameter : {userId}
3. Query parameter : firstName
4. Endpoint: https://xxxxx.execute-api.us-east-1.amazonaws.com/v2

Request:

https://xxxxx.execute-api.us-east-1.amazonaws.com/v2/apoorv487@gmail.com?firstName=apoorv

Response:

{
"statuscode":200,
"body" : "{\"message\":\"Hello apoorv, Your userId is apoorv487@gmail.com\"}"
}

Architecture :

Below is the High-Level Architecture of the serverless application which we are going to create.

High-Level Architecture

Prerequisites:

  • NojeJS
  • AWS CLI
  • AWS SAM CLI
  • Basic understanding of AWS Cloud Formation, AWS API Gateway, AWS Lambda, IAMRole and Policies.

What is AWS SAM ?

The AWS Serverless Application Model (AWS SAM) is an open-source framework that you can use to build serverless applications on AWS.
AWS SAM is an extension of AWS CloudFormation. So, you can also use the resources, intrinsic functions, and other template features that are available in AWS CloudFormation.

Let’s begin with the code :

To make this tutorial simpler and easily understandable, bifurcated the code into snippets.

At the time of writing this blog, I have the following versions installed in the system.

AWS CLI, SAM CLI and Node versions

Step 1: Create a SAM project

Create a SAM project with some boilerplate code. Select nodejs12.x as the runtime, Project name as SAMDemo, Select template “Hello World Example ‘’ and viola you are done !!

$sam init
Project creation

Boilerplate code has been generated by the above command and this is project hierarchy.

Project hierarchy

Step 2: Delete auto-generated code in template.yaml

Open “template.yaml” generated by the above command. It is an auto-generated template that contains default AWS resources.
Delete all the resources and outputs.
Now, add the following code in the template.yaml step by step.

Step 3: Create DemoGetUserIdApi (AWS::ApiGateway::RestApi)

DemoGetUserIdApi

Step 4: Create ApiGatewayResource (AWS::ApiGateway::Resource)

ApiGatewayResource

Step 5: Create ApiGatewayMethod(AWS::ApiGateway::Method)

ApiGatewayMethod

Step 6: Create ApiGatewayModel(AWS::ApiGateway::Model)

ApiGatewayModel

Step 7: Create ApiGatewayDeployment(AWS::ApiGateway::Deployment)

ApiGatewayDeployment

Step 8: Create ApiGatewayStage(AWS::ApiGateway::Stage)

ApiGatewayStage

Step 9: Create ApiGatewayIamRole(AWS::IAM::Role)

ApiGatewayIamRole

Step 10: Create ApiGatewayPolicy(AWS::IAM::Policy)

ApiGatewayPolicy

Step 11: Create DemoGetDetailsFunction(AWS::Serverless::Function)

DemoGetDetailsFunction

Step 12: Create LambdaIamRole(AWS::IAM::Role)

LambdaIamRole

Step 13: Create LambdaApiGatewayInvoke(AWS::Lambda::Permission)

LambdaApiGatewayInvoke

Step 14: Outputs

Outputs

Putting it all together :

The final Cloud Formation template “template.yaml” is as follows:

Final Cloud Formation template

Update Lambda function code in App.js

  1. In Project, rename “hello-world” folder with “demo-get-details
  2. In Package.json change name with “demo-get-details
demo-get-details Lambda

Create S3 bucket in AWS

Create a bucket on AWS S3. In this Demo project, we have taken “sam-demo-cloudformation” as the S3 bucket name

S3 bucket

Deploying the CloudFormation template through terminal

In the root directory of the project, run the following commands:

  1. Build all functions and layers, and their dependencies
$sam build

2. Packages an AWS SAM application

$sam package --output-template-file packaged.yaml --s3-bucket sam-demo-cloudformation

3. Deploying Lambda functions through AWS CloudFormation

sam deploy --template-file packaged.yaml --region us-east-1 --capabilities CAPABILITY_IAM --stack-name sam-demo-stack

Select “Y” when prompted to deploy this changeset and the resource creation gets started.

Finally, the stack gets created successfully on the AWS Cloud formation and hence the Serverless Rest API.

Stack on AWS CloudFormation

Cloudformation stack created on AWS
Resources created on AWS

Testing the Rest API

After successful deployment, test the API by making a request to the endpoint with the path parameter and query parameter using the Postman.

Testing the created API using Postman

Conclusion

We have now successfully created a serverless rest API using AWS API Gateway and Lambda.

Source Code :

You can find the complete source code of the project at

Make sure you give this post 50 claps 👏 and follow if you enjoyed this post and want to see more.

Thank you for reading. ❤️

Connect with me : LinkedIn

--

--

Apoorv Garg

Senior Consultant at GlobalLogic | iOS app developer | AWS Certified Solutions Architect - Associate