DEV Community

Alan Blockley
Alan Blockley

Posted on • Originally published at alan.dbla.tech

AWS SAM Demystified

Are you intrigued by the concept of serverless computing and the wonders it can bring to your applications? Well, get ready for a mind-blowing adventure into the world of AWS Serverless Application Model (SAM)! In this article, we'll take you on a journey to demystify AWS SAM and explore its key features.

Buckle up and prepare to unleash the magic of serverless development!

Image description

Firstly let's start by understanding the essence of AWS SAM. Let's start at the beginning. AWS SAM is a powerful framework designed to simplify the development, testing, and deployment of serverless applications on Amazon Web Services (AWS). It's like having a spellbook filled with pre-defined templates, ready to enchant your applications and take away the complexities of infrastructure management.

AWS SAM is driven by three pillars;

Image description

The SAM template is your ticket to serverless stardom. It allows you to describe the AWS resources needed for your application, including functions, event sources, permissions, and more. With its simplified syntax and predefined resource types, you'll be conjuring serverless applications in no time.

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

    Sample SAM Template for sam-app

  Globals:
    Function:
      Timeout: 3
      MemorySize: 128

  Resources:
    HelloWorldFunction:
      Type: AWS::Serverless::Function 
      Properties:
        CodeUri: hello_world/
        Handler: app.lambda_handler
        Runtime: python3.9
        Architectures:
          - x86_64
        Events:
          HelloWorld:
            Type: Api 
            Properties:
              Path: /hello
              Method: get

  Outputs:
    HelloWorldApi:
      Description: "API Gateway endpoint URL for Prod stage for Hello World function"
      Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
    HelloWorldFunction:
      Description: "Hello World Lambda Function ARN"
      Value: !GetAtt HelloWorldFunction.Arn
    HelloWorldFunctionIamRole:
      Description: "Implicit IAM Role created for Hello World function"
      Value: !GetAtt HelloWorldFunctionRole.Arn
Enter fullscreen mode Exit fullscreen mode

The SAM CLI (Command Line Interface) is your trusty sidekick in the realm of local development. It enables you to test and debug your serverless applications on your local machine using Docker. You can simulate AWS Lambda and API Gateway locally, sparing you the need to repeatedly deploy your code to AWS during development.

  $ sam local invoke

  ...

  {"statusCode": 200, "body": "{\"message\": \"hello world\"}"}%

  $ sam local start-api

  ...

  2023-06-07 15:07:08 127.0.0.1 - - [07/Jun/2023 15:07:08] "GET /hello HTTP/1.1" 200 -
  2023-06-07 15:07:08 127.0.0.1 - - [07/Jun/2023 15:07:08] "GET /favicon.ico HTTP/1.1" 403 -

Enter fullscreen mode Exit fullscreen mode

Image description

With a flick of your wrist, SAM empowers you to package and deploy your serverless applications effortlessly. It bundles your code and dependencies, generating deployment artefacts ready to be deployed using AWS CloudFormation. Deploying your application becomes as simple as casting a spell!

  $ sam deploy --guided

  Configuring SAM deploy
  ======================

  Looking for config file [samconfig.toml] :  Found
  Reading default arguments  :  Success

  Setting default arguments for 'sam deploy'
  =========================================
  Stack Name [sam-app]: hello-world
  AWS Region [ap-southeast-2]:
  #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
  Confirm changes before deploy [Y/n]:
  #SAM needs permission to be able to create roles to connect to the resources in your template
  Allow SAM CLI IAM role creation [Y/n]:
  #Preserves the state of previously provisioned resources when an operation fails
  Disable rollback [y/N]:
  HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
  Save arguments to configuration file [Y/n]: n
Enter fullscreen mode Exit fullscreen mode

Image description

AWS SAM embraces the thriving serverless ecosystem by supporting popular programming languages, frameworks, and tools. Whether you're a Python charmer, a Node.js ninja, or a Java guru, SAM has got you covered. It's a gateway to a vibrant community of serverless enthusiasts, who share knowledge and reusable components.

A detailed list of runtimes can be found here - https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

And that's it! You've gained a solid foundation to embark on your serverless journey. In this article, we've unravelled the essence of AWS SAM, explored its three pillars, mastered the SAM template, and discovered the joys of local development, packaging, and deployment. Get ready for the next adventure where we'll dive deeper into the mystical world of AWS SAM and uncover more powerful spells for serverless success. Stay curious, keep exploring, and embrace the magic of AWS SAM!

Top comments (0)