Raywon Kari - Published on June 10, 2021 - 6 min read
Serverless has NOT just been a buzzword but also a paradigm shift in general, when it comes to choosing the kind of cloud infrastructure one should embrace and implement. If you are still not embracing serverless, you are missing out on the fruits it gives. Check out my earlier blog post about serverless to know more, if you haven't already.
As the adoption increases, Cloud providers are creating new tools to even speed up the usage and make it user friendly. As part of that, AWS has created a new framework called SAM i.e., Serverless Application Model. Using this framework, it's as easy as running a few commands and writing a few lines of code and our initial version of a serverless application will be ready.
In order to get started, we need to do the following:
The following commands on macOS will install the CLI:
brew tap aws/tapbrew install aws-sam-cli## Verifysam --versionSAM CLI, version 1.24.1
For all distributions, the doc can be found here.
Next up is to configure the AWS CLI, follow the instructions here.
SAM is basically a framework, using which we can extend our CloudFormation templates with Serverless resources, such as Lambda functions, API Gateway, Step functions, Dynamodb tables etc. We create SAM templates, in other words CloudFormation templates with SAM framework specific resources, and deploy them using SAM CLI, or regular AWS CLI, or CloudFormation action using CodePipeline.
Using SAM, we get a toml config file which we can use to define certain constants and behaviors, such as app name, changeset logic, parameters etc.
The following resource definitions are currently supported:
Apart from resource definitions, we do have other stuff to do:
The benefits we get with using SAM is that, it does a lot of work under the hood by creating certain dependencies automatically for us, which we would have done it ourselves if we were to use regular CloudFormation templates.
One example is that, we would need an IAM role to deploy a lambda function. In CloudFormation, we would have added an IAM role resource and plugged that in to an AWS::Lambda::Function resource. Using SAM, we don't need to specify that because SAM adds it for us.
Following are a few advantages we get when using SAM:
First, we need to create our application locally.
sam init# Select# 1 - AWS Quick Start Templates# 1 - Zip# 7 - nodejs12.x# Project name: getting-started-with-aws-sam# Template selection: Hello world example# that's it
Check the folder structure:
.├── README.md├── events│ └── event.json├── hello-world│ ├── app.js│ ├── package.json│ └── tests│ ├── integration│ │ └── test-api-gateway.js│ └── unit│ └── test-handler.js├── samconfig.toml└── template.yaml
Once the code is ready, now is the time to try deploying it.
sam deploy --guided -t template.yaml --profile raywon# few questions will be asked# then the resources will be deployed# Example output: 'CloudFormation outputs from deployed stack-----------------------------------------------------------------------------------------------------------------------------------Outputs-----------------------------------------------------------------------------------------------------------------------------------Key HelloWorldFunctionIamRoleDescription Implicit IAM Role created for Hello World functionValue arn:aws:iam::010503879086:role/getting-started-with-aws-sa-HelloWorldFunctionRole-1B3NDI7M829S2Key HelloWorldApiDescription API Gateway endpoint URL for Prod stage for Hello World functionValue https://3d887brxqc.execute-api.eu-north-1.amazonaws.com/Prod/hello/Key HelloWorldFunctionDescription Hello World Lambda Function ARNValue arn:aws:lambda:eu-north-1:010503879086:function:getting-started-with-aws-sam-HelloWorldFunction-JYuswyaYSmfn-----------------------------------------------------------------------------------------------------------------------------------'
and Done! Our sample API application is ready. Source code can be found here.
Watch it in practice below:
It's an exercise for you to play around with them :)
We at MatHem.se are building a modern platform with Serverless workloads. This lead to developing tools over a period of time to ease development efforts when it comes to the platform, and also to improve the developer experience.
All the tools are open sourced. Head over to the GH org where we host them: MhLabs
Spoiling the surprises, some handy tools to shout out:
Feel free to play around! Serverless all the way!