Skip to content

Hello SAM 👋

What is AWS SAM ?

The application will be constructed using AWS SAM. The next section provides an overview of AWS SAM; if you already have experience with this service, feel free to skip ahead.

Amazon Web Services Serverless Application Model (AWS SAM) is a framework for building serverless applications. It simplifies the process of creating and managing resources used in serverless applications using AWS services like Lambda, API Gateway, and DynamoDB. AWS SAM uses a template file to define your serverless application and its resources, providing a streamlined and consistent method for deploying complex serverless applications. By leveraging AWS SAM, you can more easily automate deployment processes, test your applications locally before deploying, and even generate boilerplate code for common serverless patterns.

In the following section we will build a simple hello world application using SAM.

  • sam init
  • Choose AWS Quick Start Templates
  • Next choose Hello World Example
  • If you choose to use the most popular runtime and package type, then make sure that Python 3.9 is installed
  • Choose Python 3.11
  • Choose Zip
  • Choose the default answers for the rest of the questions.
  • For project name, choose the default

  • You need to build the sam packge

  • Go to the folder the template created cd sam-app
  • Run sam build and next run sam deploy --guided. You should run guided each time you want to add something to the sam configuration file or create it for the first time.
  • When asked HelloWorldFunction may not have authorization defined, Is this okay? choose y
  • The rest can be defaults
  • Deploy this changeset? choose y
  • Give the deployment a try, you should see under Outputs the API Gateway endpoint URL, copy the URL and try it on browser.
  • When done, run sam delete to remove the stack.

Insights

Template

At the core of every AWS SAM application lies the template.yaml, a file that outlines the resources utilized by the app. The file is located at the root of the SAM application you created; in our case, it's sam-app.

Our sample template file is split into four main sections:

  1. Header - This section provides the template's definition and description, which are displayed in the CloudFormation console.
  2. Global - This section contains global configurations that apply to all resources. In our case, we have set default values for the timeout and memory of all Lambdas to 3 seconds and 128 MB, respectively.
  3. Resources - This section details the resources to be established as part of the application. In our case, it is a Lambda function.
  4. Outputs - This section enumerates output values that can be imported into other stacks (for creating cross-stack references), returned in responses (to provide stack call descriptions), or viewed on the AWS CloudFormation console.

The heart of your AWS SAM template file is the resource section, let's deep dive into its content.

HelloWorldFunction: #(1)!
    Type: AWS::Serverless::Function #(2)!
    Properties:
      CodeUri: hello_world/ #(3)!
      Handler: app.lambda_handler #(4)!
      Runtime: python3.11
      Architectures:
        - x86_64
      Events: #(5)!
        HelloWorld:
          Type: Api #(6)!
          Properties: #(7)!
            Path: /hello 
            Method: get
  1. This is the logical ID for the Lambda function.
  2. Specifies that the resource is a Lambda function. There are many different resource types.
  3. The directory where the Lambda function code resides.
  4. Specifies the entry point for the Lambda function.
  5. This section defines the event sources that will trigger the Lambda function.
  6. Specifies that this event is an API Gateway event.
  7. The API endpoint and the HTTP method.

Simplicity

With AWS SAM, defining a Lambda function is as straightforward as pointing to a directory containing your code. AWS SAM then takes care of bundling the folder's contents along with the necessary dependencies into a zip file, and uploading it to AWS. It also automatically generates the appropriate IAM roles for you during this process.

Interoperability

A key advantage of AWS SAM is its seamless integration with other services. For instance, in our example, we've integrated our Lambda function with API Gateway, demonstrating the simplicity of combining AWS services in a SAM application.

Deploying changes

Each time you make changes to any of your source files, there are two steps that need to be done in order to deploy the changes to AWS:

  1. sam build - This creates the deployment package for the different Lambdas.
  2. sam deploy - This deploys the zip package along with any required changes in the additional resources that are created.

Exercises

  • Accept a path parameter that contains your name, and when calling the endpoint, it will return the specified name - hello <path parameter>. For example calling https://apigw-url/hello/efi will return hello efi.
Tip
  1. Add /{name} to the path
  2. In the Lambda handler, extract the path parameter using the following code - event.get("pathParameters", {}).get("name")
  • Add a new Python Lambda to the template that returns "Mama Mia" in its response.
  • What is the purpose of sam build? Can you locate its artifacts?