Understanding AWS SAM Template

AWS SAM (Serverless Application Model) is an open-source framework to develop and deploy serverless applications on AWS. In this blog post, we will try to understand the AWS SAM Template, its various parts and how to write one for developing and deploying serverless applications on AWS. if you want to understand more about AWS SAM, you can read about it in our previous blog post here. If you are already aware of what is AWS SAM and would like to get started with it. you can refer to the blog post getting started with AWS SAM.

Following is the list of all the available sections in an AWS SAM template:-

  • Transform (required):The transform section is a required section in the AWS SAM template and the value is always set to AWS::Serverless-2016-10-31. This is a macro hosted by Cloudformation. It takes the entire AWS SAM template and converts it to a Cloudformation template for deployments.
  • Global (optional): Global section is an optional section and hosts all the properties that are common to some of the resources defined in the template. The resource section will inherit the property from the global section.
  • Description (optional): A description section is a text string that describes the template.
  • Metadata (optional): Metadata objects provide additional information about the template like implementational details about a resource etc. One of the points to note about this section is that Cloudformation neither transforms nor updates this section. This section should not be used to perform any important action or store any important information like environment variables.
  • Parameters (optional):These are the values that you can pass to your template at runtime i.e. during the deployment. The parameters can be referenced in other sections of the template like Resources to control the behaviour and output of the template.
  • Mappings (optional):This section can be used to define a key-value pairthat can be used to specify conditional parameter values. This is similar to a lookup table. You can use intrinsic functions like Fn::FindInMap to get and use these values in the Resources section.
  • Conditions (optional): Conditions section can be used to define the conditions that control whether certain resources will be created or not during the deployment. Conditions can also be used to define whether some properties will be assigned to the resource or not.
  • Resources (required): The Resources section is a required section and holds the definition of the stack resources and their properties which will be either created or updated while the deployment of the stack.
  • Outputs(optional): This section is used to output some of the properties after the resources are created or updated. One of the examples can be the arn of the function deployed.

Following is the example of an AWS SAM Template

  AWSTemplateFormatVersion: \"2010-09-09\"Transform: AWS::Serverless-2016-10-31Description:  Node.js barebone for AWS LambdaGlobals:  Function:    Timeout: 300    Runtime: nodejs14.x    MemorySize: 512Resources:  MainFunction:    Type: AWS::Serverless::Function    Properties:      CodeUri: src/      Handler: index.handleEvent

The above example shows some of the most used sections in an AWS SAM template.

Let\’s try to explain the above template.

  • The description, transform sections are self-explanatory as per our descriptions of various sections.
  • The Globals section holds some properties in function. This means any function deployed using this template will inherit the values. The timeout is set to 300 seconds and the function will be deployed using nodejs v14 and will have a memory of 512Mb.
  • The Resources section defined the function using a specific type. The type AWS::Serverless::Function is a special type and will be transformed to the appropriate Cloudformation equivalent once deployed. The defined properties in the resource point to the code and the handler function defined for the Lambda.

That was an introduction to understanding the SAM templates and various sections. In the next blog post, we will use the above mentioned SAM template to deploy a basic function to AWS.

Till then Happy Coding.