Getting started with Serverless Application Model (SAM)

In this article on Serverless Computing I will show you how to get started with the Serverless Application Model or SAM. Thanks to the Serverless Application Model you can quickly get started and establish a good workflow for developing your serverless applications.

I will show you how to install the SAM CLI, create a scaffold project and deploy it. The project will create a single lambda function and make it accessible through an API Gateway endpoint. I won’t spend any time on the project structure or how it works. In this article I will focus on the SAM tooling and how you can create and deploy a serverless application.

I’ll assume that you’re using Linux or some other *nix based OS, if you’re using Windows you shouldn’t have any issue adapting the commands and directory structure to follow along. I’m also assuming that you have your AWS CLI setup and configured for your account access.

Getting started with SAM

First we will install the SAM CLI  to make things easier for you. This will remove all the nitty gritty details for you, but don’t worry: I’ll come back to the implementation details later on.

We will install the SAM CLI using pip, if you have previously installed the CLI then make sure to run the command below again to get the latest version.

$ pip install --upgrade --user aws-sam-cli

Verify that the CLI is installed correctly by running the sam command. The rest of this article is writting using version 0.3.0.

Next we will start a new SAM application. Using the CLI we will create a new project that contains a basic project for us to start with. I’ll use the Python 3.6 runtime for this but you can change this if you prefer another one.

$ sam init --runtime python3.6 --name greeter
[+] Initializing project structure...
[SUCCESS] - Read greeter/README.md for further instructions on how to proceed
[*] Project initialization is now complete

This will have created a new project structure for you in the directory greeter (or whatever name you picked for your project). At first glance the following files and directories will be there for  you.

List of files in the greeter project

We will go over the specifics later on. For now just be aware that the source code of your application is in the hello_world directory and the template.yaml file contains the specifications of your serverless application. If

Packaging and deploying your application

Thanks to the project scaffold created by the SAM CLI you are now ready to package and deploy your new application.

First we will install the necessary dependencies for our project and copy the source of our application to the build folder. This is the source code that SAM will upload to S3 and create our lambda function.

$ pip install -r requirements.txt -t hello_world/build

$ cp hello_world/*.py hello_world/build

Next we have to create an S3 bucket to store our lambda function code. If you want you can use an existing bucket.

$ aws s3 mb s3://sinax-tutorial-sam-greeter

Then we create the CLI tool to package our source code, upload it to our S3 bucket and create a CloudFormation template that can be used to create our stack:

$ sam package --template-file template.yaml --output-template-file serverless.yaml --s3-bucket sinax-tutorial-sam-greeter 
Uploading to e22d81e3b33aeb37f36f6e263b0543fc 947167 / 947167.0 (100.00%)
Successfully packaged artifacts and wrote output template to file serverless.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /home/michael/aws-workdir/sam/greeter/serverless.yaml --stack-name <YOUR STACK NAME>

This step did two things. First it took the source code of our serverless application, compressed it into a zip-file and uploaded it into our S3 bucket. Next it transformed the template.yaml file and created a new CloudFormation template (named serverless.yaml as specified by the –output-template-file parameter).

We can now deploy our new serverless application using the SAM CLI:

$sam deploy --template-file serverless.yaml --stack-name greeter --capabilities CAPABILITY_IAM

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - greeter

The use of SAM command here is a bit superfluous as we could have used the aws cloudformation deploy … command as well. As the output indicates our stack was successfully created so let’s have a look at the outputs of our stack.

$aws cloudformation describe-stacks --stack-name greeter

Somewhere in the JSON output you’ll see a section called Outputs that will contain a variable called HelloWorldApi. This contains the URL of an API endpoint created for our serverless application. Copy the URL and visit it in your browser or use our trusty curl command:

$ curl https://dlhk0uo3ug.execute-api.eu-central-1.amazonaws.com/Prod/hello/

You’ll get the output of our lambda function that is returned by the API Gateway endpoint created for us.

Conclusion and next steps

In this article we saw how to get started with the Serverless Application Model. You created a basic SAM project, packaged the source code and deployed it with CloudFormation. In the next part of this series we will look into more details on how SAM works, the different components and get started building a real application.



Leave a Reply