AWS Server-less Application Model (SAM) and cloud formation Stacks

In a typical client server model. You have a client and a server that is always running and that is going to be listening any http requests. 

for example, if you have a sample application you make a request a server to get all todo task it will provide a result and sit idle but it is always going to be running. 

So on the other end if we talk about server-less, we don't have consistent running server. When we make a request to get all todo tasks, server-less creates a function with a block of functionality which contains a little bit of code and this block of code is going to fetch data and provide the response. 

The function or a block of code was not running in the background it is created when we make a http request.

It really really cheap (only pay for what you use), fully managed and Reduce complexity to manage servers.

There are multiple AWS Services which support Server-less. let list them below.
  • Aws Cognito
  • Aws lambda
  • Aws DynamoDb
  • Aws Api Gateway
  • Aws S3
  • Aws SNS and SQS
  • Aws Kinesis Data Firehose
  • Aws Aurora 
  • Aws Step Functions
  • Aws Fargate 
we cannot cover all of them in one blog but we will cover Aws lambda, Aws Dynamo Db, Aws ApiGateway in this blog.

You can think about FAAS (Function as a service) if we talk about lambda but its alot more than that if we talk about server-less.

Server-less is totally new paradigm in which the developers don't have to manager servers anymore but that doesn't means we don't have servers at all. There are servers but we as a developer don't have to manage them.

Most Importantly as a developer. I will try to deploy these server-less services using 2 frameworks. Both of them are IAC (Infrastructure as code) tools.
1. Serverless
2. SAM 

we will use them one by one.

First of all, and obviously we need an aws account. so there are many videos or blog available to create an aws account. let me share the video link.
https://www.youtube.com/watch?v=P7hVdusJF7I
https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-creating.html

First of all never ever use a admin user to perform any activity, always create a new user for any task like we want to deploy lambda behind Api-gateway, which uses dynamo db for data. So we need a user which can access only these services , lambda, Api-gateway and dynamo db.

1. Create a user.
    Create an IAM account to give some access to  a user. 
    Go to IAM (Identity access management) on aws console.
    Click on users and create a user with a new name and programmatic access.
    Provide the user with lambda, gateway and dynamos permissions.
    In the end it provides you with Access Key Id and Secret access Key like snapshot.

2. Install aws cli
 Go to below link https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
and install aws cli.
After installation go to terminal and type command aws --version, it will tell about the version of install aws cli.
3. Configure Aws Profile.
    aws configure --profile <profilename>
it will ask you to input following details
    Aws Access Key ID: .........
    Aws Secret Access Key: .......
    Default region name: ........
    Default output format: ........
5. Install Node
    Go to link https://nodejs.org/en/download/
    install node according to OS.

5. Serverlerss framework (Go to serverless.com):
    Install serverless using  command
    npm install -g serverless 
After the installation, Initiate a project. so type serverless on cmd it will drive you to create a project.

We will select REST API and hit enter and provide the project name of you choice.
now go to folder by typing cd <projectname>
Choose any editor to edit the file we will use VS code. 
so type code . to open the project in VS code.
It will show the structure like below snapshot.

It creates YML file.
It contains all the configuration that is going to spin up our aws architecture.
Let Look at the yml file.

Handle.hello is the function, which is in the handler.js file so just take a look. 

Its a simple function returning status code of 200 with body. this is the lambda function which we have to deploy behind the api-gateway and which event trigger this function is defined in xml file.

AWS lambda: It's a server-less event driven service which runs on demand. It time out after 15 min so always use lambda for small task.

Its time to deploy IAC to cloud.

1. serverless or sls init 
    It will create an app with sample function.

2. serverless deploy -v or sls deploy -v 
    It will create and deploy your whole IAC to aws.
3. sls deploy -f <functionName>
    It will deploy a single function to aws.

After deployment you can go to API gateway on aws and can see you deployed IAC.

4. npm init -y
It will create a package.json file and then you can add dependencies for you project.
npm install uuid
npm install aws-sdk
npm install middy/core
npm install middy/http-json-body-parser

Now its time to take a break, and move to other aws provided framwork.
    
SAM: Its an open source framework for building serverless application. It an officially provided by aws.

It create YAML file. 
1. npm install SAM
follow this link to install aws sam cli.
 https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html
2. SAM init 
     It will init a new project for you.

Provide the project name is walk through.
It will create a project and structure is look like this.
It creates YAML file, as you know that server-less creates a YML file. 
Its a complete project, it already contains package file. and src folder.
Now its time to deploy simple pre-created function to cloud.
3. SAM build
     It will build your project with all dependencies.
4. SAM deploy
     It will deploy your IAC.
5. SAM local invoke --guided
    It will drive you to deployment of stack on aws.


both server-less  (YML file) and SAM (YAML file) stores build on S3 and cloud-formation read from S3 and create/update the Stack.

Bonus read.
so how to delete/clean a stack
aws cloudformation delete-stack  --stack-name [stackname] --region [region]

Comments

Popular posts from this blog

MySQL Docker Container Setup And Configuration On Windows 10

Create a VPC resource using terraform