SAM template

Before go to for actual coding, we will explore the structure of SAM application and SAM template.
Let’s open sam-app/template.yaml file. It should have a structure like the following. Hello SAM app

✍️ sam-app/template.yaml

You may notice that the syntax looks exactly like AWS CloudFormation, this is because SAM templates are an extension of CloudFormation templates. That is, any resource that you can declare in CloudFormation, you can also declare in a SAM template. Let’s take a closer look at the components of the template.


Notice the transform line of the template, it tells CloudFormation that this template adheres to the open source AWS Serverless Application Model specification :

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >

  Sample SAM Template for sam-app


This section defines properties common to all your Serverless functions and APIs. In this case, it’s specifying that all functions in this project will have a default timeout of 3 seconds.

    Timeout: 3

Hello World Function

The following section creates a Lambda function with an IAM execution role. It also specifies that the code for this Lambda function is located under a folder named hello_world, and that its entrypoint is a function named lambdaHandler within a file named

    Type: AWS::Serverless::Function # More info about Function Resource:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.7
          Type: Api # More info about API Event Source:
            Path: /hello
            Method: get

Event Triggers

This section is part of the function definition and it specifies the different events that will trigger the Lambda function. In this case, we are specifying the event to be an API Gateway with an endpoint on /hello that will listen on HTTP method GET.

          Type: Api # More info about API Event Source:
            Path: /hello
            Method: get


The Outputs section is optional and it declares output values that you can import into other CloudFormation stacks (to create cross-stack references), or simply to view them on the CloudFormation console. In this case we are making the API Gateway endpoint URL, the Lambda function ARN and the IAM Role ARN available as Outputs to make them easier to find.

  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}"
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn