Go Server-less with AWS SAM (Server-less Application Model)

tudip-logo

Tudip

08 April 2020

Let’s start the serverless journey from AWS lambda.

It is the one of the most popular widely used services in AWS. It has revolutionised how people do applications, deploy them and scale them.

Serverless is a new trend of development platforms where there is no need to maintain any server anywhere.

AWS came to appear as FaaS(Function as a Service).

AWS provides the following services for serverless Application models which are best in performance.

  • AWS Lambda
  • DyanamoDB
  • AWS cognito
  • AWS API gateway
  • AWS S3
  • AWS S
  • NS and SQS
  • AWS Kinesis
  • Aurora Serverless

Server Application Model (SAM)

SAM is a Serverless development framework which allows you to deploy your server less application into AWS cloud.

SAM is nothing but the cloud formation code and all are written in simple YAML code. The YAML codes are very simple which will generate complex cloud formation code.

Anatomy of SAM Template

Transform Header:

Transform header indicates it is a SAM template:

Transform: ‘AWS::Serverless-2016-10-31’

Resources:

While writing code we are using three resources broadly used.

  • AWS::Serverless::Function (Lambda Function)
  • AWS::Serverless::Api (AWS api gateway)
  • AWS::Serverless::SimpleTable (DynamoDB)

Two Helpers are used to package and deploy the cloudformation code.

Cloudformation or SAM package and deploy.

SAM deployment

server-1

 

Why AWS Lambda?

Compared with EC2 and lambda which is the better option to develop with list minimal risks.

EC2:

  • There is a virtual server in the cloud and there is some limit in RAM and CPU like t2-micro has 512MB RAM.
  • Continuously running.
  • If you have enabled auto scaling then you will get more servers started with your resources
  • It’s not serverless, definitely you have to maintain and manage the servers.

AWS Lambda:

  • Aws Lambda is the virtual function in which you no need to maintain any servers.
  • These are the short executions and cost effective. It is limited to the execution and cost of memory usages.
  • It will run on demand. Auto scale will be monitored with demand of usages.

Let’s take an example of how the Lambda works with a Serverless model.

Example:
Let’s see how Lambda helps you to do the serverless things.

Example: profile picture functionality in a project
It will trigger a Lambda function to do some process and upload the file to S3 bucket and store the file metadata or path in DynamoDB user table. This will be triggered by API gateway.

While users request to see the profile picture through API gateway and API Gateway will trigger the Lambda function and lambda will do the needful to get the images. Here there is no need to have a server, only code and deploy will work.

server-2

 

Here we have the AWS SAM template and Application code inside a Folder and when we will do the cloudformation Package it will Push the AWS template and application code into a S3 bucket for future reference.

And the SAM package will also transform the SAM template into a cloudformation template and will push it into S3 bucket.

Write the following code in the app.py file.

server-3

 

This program only prints HelloWorld in AWS Lambda console.

 Write the following code in template.yaml file

server-4

server-5

This template will generate required resources in the cloudformation stack.

AWS Cloud formation deploy will create and execute a change set. Change set is basically the changes from existing stack to new changes need to be deployed in the stack and cloud formation will apply it into stack and required components may be it is Lambda, API gateway, IAM policy, DynamoDB or S3 bucket.

server-6

server-middle

Here is the command and output of the AWS SAM Package. It will push the application code into the AWS S3 bucket.

and the generated cloudformation code will be:

server-7

This template will be executed in AWS console to generate required resources

Generated YAML code in the template-generated.yaml file and you can see the code-uri points to the AWS S3 bucket.

server-8

It will create the respected stack in the AWS account with a respected IAM role.

Go to your AWS console and find the cloudformation service in the AWS console you will find a stack has been created in your account.

server-9

You will find the following resources have been created in your account.

server-10

Lambda function and IAM role created.

Go to your lambda function service in the AWS console you will find a Lambda function has been created which we have written in our lambda_function.py file.

server-11-1024x216

 

Lets test the lambda by creating a test event you will find following Output.

server-12

 

This is the simple SAM Application.

Lets create a simple REST API which will use the AWS API gateway to expose and endpoint to trigger REST API.

Let’s add something into our existing app.py file so that we can create a REST API using SAM.

Add the following code in the app.py file

server-13

This will simply return HelloWorld as a response to the API endpoint.

And modify the following code in template.yaml

server-14

server-15

This template will expose an endpoint /Hello with GET method in AWS API gateway and integrate with lambda function.

Then Package and deploy the project using cloudformation package and deploy which we have previously used.

Go to your AWS console and find in the cloud formation stack you can find API gateway, Lambda and an IAM role has been created for the template.

server-16

Here is the command to build and deploy the API in SAM.

You can see the following resources are created in the AWS console.

server-17

AWS has created and deployed everything so the time we have to only verify and test the API

Lets go to the AWS API Gateway console and find the respected API endpoint created or not.

server-18

 

Here AWS created the end point for us only we have to check if it is working fine or not. Also AWS created some stage variables to point in your development stages.

Inside the stage variable section you will find the API URL.

server-19

 

If you browse the url in any of the browsers you will get the following result.

server-20

Request a quote