0
0
AWScloud~5 mins

Serverless Application Model (SAM) in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Building serverless applications can be complex because you need to manage many resources like functions, APIs, and databases. AWS Serverless Application Model (SAM) helps by letting you define all these resources in a simple file, so you can deploy and manage your app easily.
When you want to create a simple API using AWS Lambda and API Gateway without managing servers.
When you need to deploy a function that reacts to events like file uploads or database changes.
When you want to package and deploy your serverless app as one unit instead of separate pieces.
When you want to test your serverless functions locally before deploying to the cloud.
When you want to automate deployment of serverless apps using a single configuration file.
Config File - template.yaml
template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Simple SAM app with one Lambda function and API Gateway
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.lambda_handler
      Runtime: python3.9
      CodeUri: ./src/
      Events:
        HelloWorldApi:
          Type: Api
          Properties:
            Path: /hello
            Method: get

This file defines a serverless app with one Lambda function called HelloWorldFunction. The Transform line tells AWS this is a SAM template. The function uses Python 3.9 and runs code from the src folder. It is triggered by an HTTP GET request to the /hello path via API Gateway.

Commands
This command processes your SAM template and prepares your application for deployment by installing dependencies and packaging your code.
Terminal
sam build
Expected OutputExpected
Building codeuri: ./src runtime: python3.9 metadata: {} architecture: x86_64 functions: ['HelloWorldFunction'] Running PythonPipBuilder Build Succeeded
This command deploys your serverless app to AWS. The --guided flag walks you through configuration options like region and permissions, and saves them for future deploys.
Terminal
sam deploy --stack-name my-sam-app --guided
Expected OutputExpected
Configuring SAM deploy Stack Name [my-sam-app]: AWS Region [us-east-1]: Confirm changes before deploy [y/N]: N Allow SAM CLI IAM role creation [Y/n]: Y Save arguments to samconfig.toml [Y/n]: Y Deploying with following values =============================== Stack name : my-sam-app Region : us-east-1 Confirm changeset : false Deployment s3 bucket : sam-deployments-bucket Capabilities : CAPABILITY_IAM Waiting for stack create/update to complete Successfully created/updated stack - my-sam-app
--stack-name - Names the CloudFormation stack for your app
--guided - Runs interactive setup for deployment options
This command lists all AWS resources created by your deployed SAM app, so you can verify what is running.
Terminal
sam list resources --stack-name my-sam-app
Expected OutputExpected
Name Type LogicalId HelloWorldFunctionRole AWS::IAM::Role HelloWorldFunctionRole HelloWorldFunction AWS::Lambda::Function HelloWorldFunction HelloWorldApi AWS::ApiGateway::RestApi HelloWorldApi
--stack-name - Specifies which deployed app's resources to list
This command shows the live logs from your Lambda function, helping you see what happens when it runs.
Terminal
sam logs -n HelloWorldFunction --stack-name my-sam-app --tail
Expected OutputExpected
START RequestId: 1234abcd-5678-efgh-9012-ijklmnopqrst Version: $LATEST 2024-06-01T12:00:00.000Z 1234abcd-5678-efgh-9012-ijklmnopqrst Hello from Lambda! END RequestId: 1234abcd-5678-efgh-9012-ijklmnopqrst REPORT RequestId: 1234abcd-5678-efgh-9012-ijklmnopqrst Duration: 3 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 45 MB
-n - Specifies the function name to get logs for
--tail - Streams logs live as they come
Key Concept

If you remember nothing else from SAM, remember: it lets you define and deploy your serverless app with one simple file and easy commands.

Common Mistakes
Not running 'sam build' before 'sam deploy'
Your code and dependencies won't be packaged correctly, causing deployment errors or broken functions.
Always run 'sam build' first to prepare your app before deploying.
Forgetting to specify the stack name during deployment
SAM won't know which CloudFormation stack to update, causing deployment to fail or create unexpected stacks.
Use the --stack-name flag with a clear name every time you deploy.
Not checking logs after deployment
You miss errors or issues in your function that only appear when it runs in AWS.
Use 'sam logs' to view function output and troubleshoot.
Summary
Use a template.yaml file to define your serverless functions and triggers.
Run 'sam build' to prepare your app, then 'sam deploy --guided' to send it to AWS.
Check your deployed resources with 'sam list resources' and troubleshoot with 'sam logs'.