How to Build Serverless API using AWS Serverless Application Model (AWS SAM)

Niyaz Abbasov
PASHA Bank
Published in
4 min readMay 30, 2020

Introduction

Using infrastructure as code is the best way to build production serverless applications. You can build a serverless API easily through the AWS console UI. But in a real-life scenario, you’d never build a production application this way. It’s just a good learning experience. In this article, I’ll show you how to build a simple Serverless API using AWS Serverless Application Model (AWS SAM).

What is AWS SAM? AWS SAM is a serverless development framework that allows you to deploy your service applications onto AWS. All the configuration for your serverless applications is going to be a simple YAML code. AWS SAM generates complex CloudFormation from your simple YAML file.

AWS SAM is an extension of CloudFormation with a few new resource types that simplifies the development of serverless applications. It supports anything that CloudFormation supports, so we can use Outputs, Mappings, Resources and etc.

What is CloudFormation? CloudFormation is a declarative way of outlining your AWS Infrastructure for any kind of resource (most of them are supported). For example, within a CloudFormation template, you say: I need two EC2 machines, two Elastic IPs for these EC2 machines, and Elastic Load Balancer in front of these machines. CloudFormation creates those resources for you, with the exact configuration that you specify.

Before start, you’ll need to go through the SAM CLI installation guide page.

Student Management API

I’ll use a simple Student Management API for this article. Student Management API is able to list, store, update, and delete students in a DynamoDB database. The following diagram is an overview of the components of the solution, and how they interact:

Setup infrastructure

Here is the student_infrastructure.yaml— the file where we specify the infrastructure:

The first two lines are SAM boilerplate and the third being a description of the application.

Next is the Resources section, where CloudFormation resources are defined.

  • AWS::Serverless::SimpleTable — specifying the resource to be a DynamoDB table naming Students with the primary key.
  • AWS::Serverless::Api — specifying the resource to be an API Gateway with endpoint and integration with the Lambda.
  • AWS::Serverless::Function — specifying the function, when invoked will look for a Lambda studentManagement.handler in lambda_handler folder.

Last section Outputs is optional to declare output values after stack creation.

Deploy

Here is deploy command:

$ sam deploy --guided --template-file student_infrastructure.yaml

This command takes student_infrastructure.yaml and uses it to create or update a CloudFormation stack. --guided argument helps us to set stack-name, region, and other options in dialog mode.

After getting a successful creation message in CLI, navigate to the AWS CloudFormation service, you should see the stack was created successfully:

AWS CloudFormation service

If you click into your stack and then go to the Resources tab, you should see all resources were created:

Stack’s resources

If you click on the Students in Physical ID column, you’ll be navigated to the DynamoDB console. In the Items tab, you’ll see that table is empty. Let’s import our dummy data. To import data run this command:

aws dynamodb batch-write-item --request-items file://students.json
AWS DynamoDB — Students table

Let’s navigate to AWS API Gateway service and you should see Students API. If you click onStudents API, you’ll be navigated to the page where API resources are described:

API Gateway Students API resources

If you navigate to the Lambda service, you should see the student-management-app-*Lambda function:

AWS Lambda

Here is our Lambda function:

Testing

To test our API we’ll use Postman. First, navigate to the API Gateway services, click on your stack, Stages menu and select prod. Under prod stage copy Invoke URL. This is a public URL provided by AWS where API is deployed. Here is a sample URL: https://j0bd959rp2.execute-api.eu-central-1.amazonaws.com/prod/students.

List students
Get student by id
Create a student
Delete a student

Conclusion

You can see the complete project here.

If you are interested, you may read the Benefits of Using AWS SAM and with this article, AWS SAM might be a good choice for triggering your Lambdas in the cloud.

--

--