AWS

AWS SAM Template

A serverless application can be built using services provided by AWS such as AWS Serverless Application Model (SAM). AWS provides AWS SAM CLI for developing applications based on SAM. It facilitates an execution environment similar to Lambda for building, testing, and debugging applications provided by SAM templates. Also, using AWS SAM CLI we can deploy the SAM application to AWS.

What will we cover?

In this guide we will explore AWS SAM Template.

What is AWS SAM?

AWS SAM is used for developing serverless applications on AWS. Both SAM and SAM CLI are under an open-source license. SAM defines the resources used in serverless applications. It has short syntaxes to denote APIs, functions, databases, and event source mappings. The SAM syntaxes are transformed and expanded into AWS CloudFormation syntaxes. Applications can be modeled using YAML. In this way, serverless applications can be built rapidly.

Features of AWS SAM

The AWS SAM can be integrated with other AWS services and thus has several benefits for developing serverless applications:

  1. Using AWS SAM, organization of corresponding components and resources is simplified. One can use it to distribute configuration among resources and all corresponding resources can be deployed collectively.
  1. While deploying, the SAM syntaxes are expanded into native AWS Cloudformation syntaxes. This provides us with the option of reliable deployment with CloudFormation.
  1. We can develop serverless applications in many runtime languages. With the SAM CLI we can locally build, pilot run, and fix the serverless applications. Also, the SAM CLI can be integrated with AWS provided toolkits.
  1. AWS SAM CLI can be installed on many major Linux based distros, Mac and Windows.

Looking into the SAM template

An AWS SAM template is very much similar to AWS CloudFormation template, however there are some major differences between the two in terms of the following template sections:

  1. Transform section: An AWS SAM template uses an explicit declaration of the form, Transform: AWS::Serverless-2016-10-31, this declaration specifies that this AWS CloudFormation template is an AWS SAM template.
  1. Globals section: AWS SAM has a unique Globals section that declares the resources that have common configurations. The Globals section works with the AWS::Serverless::Function, AWS::Serverless::Api, AWS::Serverless::HttpApi, and AWS::Serverless::SimpleTable resources.
  1. Resources section: The Resources section of the AWS SAM template can also contain CloudFormation resources.
  1. Parameters section: The SAM template contains a Parameters section for passing values at run time. This section contains objects which make the ‘sam deploy –guided’ command to produce extra prompts to a user.

The remaining template sections…

Beside the above mentioned sections, remaining sections go with the CloudFormation template file section with the corresponding name. AWS SAM template only requires the Transform and Resources sections as a must-have. However, we can also add other sections. Apart from the above section, we can also have the below mentioned section in AWS SAM template:

  1. Description: It contains an optional description of the template.
  1. Metadata: It is also an optional section that gives further information concerning the template.
  1. Mappings: A keys mapping with their corresponding values for specifying conditional parameter values.
  1. Conditions: It contains rules or requisites that govern if specific resources are generated or if specific properties of a resource are given a value while creating or updating a stack.
  1. Outputs: It declares the values that are returned when the properties of a stack are viewed.

The template sections discussed above can be added in any order. However, one should stick to a logical order. AWS SAM CLI has a config file of TOML file format. This file, named as samconfig.toml, holds default parameters for the SAM CLI commands.

To verify an AWS SAM template, use the command format:

sam validate [OPTIONS]

Example SAM Template

Here are sample templates of AWS SAM containing specific sections and the corresponding sample prompts that appears with the ‘sam deploy –guided’ command:

1. Parameters section:

Parameters:

OurPar:

Type: String

Default: OurPalVal

Sample Output Prompt:

Parameter OurPar [OurParVal]:

2. Resources section:

Resources:

HelloWorld:

Type: AWS::Serverless::Function

Properties:

CodeUri: hello_world/

Handler: app.lambda_handler

Runtime: python3.7

CodeSigningConfigArn: arn:aws:lambda:us-east-1:XXXXXXXXXXXX:code-signing-config:csc-12e12345db1234567

Sample Output Prompt:

#Found code signing configurations in your function definitions

Do you want to sign your code? [Y/n]:

#Please provide signing profile details for the following functions & layers

#Signing profile details for function 'HelloWorld'

Signing Profile Name:

Signing Profile Owner Account ID (optional):

#Signing profile details for layer 'MyLayer', which is used by functions {'HelloWorld'}

Signing Profile Name:

Signing Profile Owner Account ID (optional):

Conclusion

In this guide we have explored the AWS SAM template. Next thing you can try is to deploy a simple Hello World serverless application containing an AWS SAM template file.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.