Introduction
In one of previous posts we’ve discussed AWS Cloud Formation Service and how it helps us manage our infrastructure as code and simplify deployment tasks on AWS.
AWS Serverless Application Model (SAM) takes it to the next level by simplifying the deployment of serverless resources.
The AWS Serverless Application Model (SAM) is an open-source framework for building serverless applications. It provides shorthand syntax to express functions, APIs, databases, and event source mappings.
SAM builds upon CloudFormation service and CloudFormation Templates. So, it is not a new service but is an extension of AWS CloudFormation. You can define and deploy serverless SAM and non-SAM resources using SAM.
In this post, we will take first steps and setup a SAM template which will automate the packaging and deployment of a simple lambda function and wire it up with AWS API Gateway, so it can be triggered by an HTTP Request.
What we will be Building
As this is an introductory post, we will keep it simple. The following picture shows the AWS resources, we’ll be defining and deploying using AWS SAM:
This is a very common arrangement; a Lambda function which can be triggered by API Gateway on receiving HTTP web Request.
First, we’ll define and deploy a simple Lambda function using SAM. Later, we will see how simple it is to setup and deploy an API Gateway using SAM (Events property). In later posts, we will extend this example and define and deploy other serverless resources as we progress.
Serverless Resources
Following are the examples of Serverless Resources which we can work with using SAM:
AWS::Serverless::Function | AWS Lambda Function |
AWS::Serverless::Api | Amazon API Gateway |
AWS::Serverless::SimpleTable | Amazon DynamoDB Table |
AWS::Serverless::Application | Application |
AWS::Serverless::HttpAPI | HTTP API |
AWS::Serverless::LayerFunction | Layer Function |
AWS::Serverless::StateMachine | State Machine |
For more information, please check the official website on this link.
Deploying a Lambda Function using SAM
Lets deploy a simple Nodejs based lambda function using SAM.
I’ve created a git repository and it contains an index.js file containing JavaScript code for our function:
Here is how the code looks like:
A very simple function, which just returns some random data. Now, it could be returning some JSON from DynamoDB, or doing some other complex task. But for our purposes, we will keep it simple to keep the focus on SAM.
Now, we will deploy this code as AWS Lambda function using AWS SAM.
Deploying Lambda Function using SAM
To start, I’ve created a simple YAML file (sam-template.yaml) in the root folder of the repo, which defines our lambda function characteristics:
Transform Key here is needed for the SAM template.
Resources section will contain all the resources definitions and in this example, we are defining a serverless lambda function with logical name MyLambdaFunction.
Type: ‘AWS::Serverless::Function’ is used to define and deploy lambda function.
Three (3) properties are required for lambda function:
- Runtime: Supports various runtimes e.g. nodejs, C#, python etc.
- Handler: follows the scheme of <filename>.<function_name> .
- CodeUri: local code location.
Package and Deploy
Once our template is ready, we can use aws cli commands for packaging and deploying purposes. To learn more about how to configure and use aws cli, you can check this post.
Package Command
The following cloudformation package command simplifies packaging of the code into an artifact and also uploads it to S3
aws cloudformation package `
--template-file sam-template.yaml `
--s3-bucket lambda-artifacts-sam `
--output-template-file sam-output-template.yaml
(note: you can create bucket using command >> aws s3 mb s3://lambda-artifacts-sam , if needed)
Once the command is executed, it will generate output yaml (sam-output-template.ymal) file as shown below:
If we now visit AWS Web Console, we can see that package command has uploaded our code to the artifacts bucket:
CLI has also generated sam-output-template.yaml file which is pointing to the same uploaded artifact and this file is the one, we will use with deploy command.
Deploy Command
Ok, so our code is packaged up, already uploaded to S3 and updated output yaml file (sam-output-template.yaml) is available. Now, we can use cloudformation deploy command as shown below:
aws cloudformation deploy `
--template-file sam-output-template.yaml `
--stack-name my-serverless-app-sam `
--capabilities CAPABILITY_IAM
(Note: These command will be executed under the context of current user and its permissions to manage AWS resources).
Here is the output of the deploy command:
and if we now visit the AWS web console, we will see that our stack is created.
AWS Web Console
Stack is created with the name specified in the cli command:
and following resources are created as part of stack creation:
on the lambda function dashboard, we can check that function is created as defined:
and here is the code, available in the lambda code editor:
Here is the update on our architecture diagram. We have covered the Lambda part.
Delete Stack
If you want to delete the stack. Following is the command:
aws cloudformation delete-stack `
--stack-name my-serverless-app-sam
Next, we will cover the API Gateway Part from our diagram.
Defining the API Gateway – Events Property
Perhaps the most powerful feature of AWS::Serverless::Function resource type is the option to specify events from other amazon web services that can trigger your lambda function.
You can think of Events as a way to “deploy resources that can trigger your lambda function”.
Not only, Events property, does configure the resources to be a trigger, but it will actually deploy the resource without having to define it explicitly elsewhere in your template. Isn’t this cool? Compare to CloudFromation template, where we have to explicitly define all those items in the template, but SAM simplifies that a lot.
These events are defined using the Events property of the AWS::Serverless::Function resource type.
We can different options to specify as a source of the event, e.g. S3, API Gateway, SNS, AlexaSkill, IoTRule etc. You can find list of other types on the official website. The following example uses Api Type:
There are two required properties, Path and Method.
Deploy command will automatically create this API resource and configure it invoke the lambda function when our API is called.
Following is the screenshot of the web console, showing that API is created:
This API resource not only connects the API to Lambda function, but also actually creates the API Gateway Resource without explicitly defining it as another resource additionally. It also takes care of permissions needed for the API to have access to invoke the lambda function:
and following is the output of the invoking the URL which calls the lambda function:
Here again is the diagram of our deployed serverless components for reference:
Summary
In this post, we covered some basics of AWS SAM which is an extension of AWS CloudFormation service and greatly simplifies managing serverless resources.
We then defined and deployed a lambda function along with API Gateway using a SAM template. We will continue extending our serverless infrastructure in upcoming posts.
You can download the code and SAM template from this git repository.
Let me know if you have some comments or questions. Till next time, Happy Coding.
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.
2 thoughts on “AWS Serverless Applications using SAM -Basics”
Comments are closed.