0
0
AWScloud~10 mins

Serverless Application Model (SAM) in AWS - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Serverless Application Model (SAM)
Write SAM Template
Run sam build
Run sam deploy
AWS CloudFormation Creates Resources
Serverless App is Live
Invoke Functions / Use API Gateway
Monitor and Update
Back to Write SAM Template
The flow shows writing a SAM template, building, deploying, AWS creating resources, app running, and monitoring with updates.
Execution Sample
AWS
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: app.lambda_handler
      Runtime: python3.12
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: GET
This SAM template defines a simple Lambda function triggered by an HTTP GET request at /hello.
Process Table
StepActionInput/StateOutput/State Change
1Write SAM TemplateEmpty projectSAM template with Lambda function and API event defined
2Run sam buildSAM templateBuild artifacts prepared for deployment
3Run sam deployBuild artifactsCloudFormation stack created or updated
4CloudFormation creates resourcesStack requestLambda function, API Gateway, IAM roles created
5Serverless app is liveResources readyAPI endpoint available, Lambda ready to invoke
6Invoke function via APIHTTP GET /helloLambda function executes and returns response
7Monitor and updateLogs and metricsTemplate updated and redeployed as needed
8ExitNo further changesApplication running and serving requests
💡 Execution stops when the serverless application is deployed and running, ready to serve requests.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
SAM TemplateNoneDefined with function and APIUnchangedUsed for deploymentUsed by CloudFormationDeployedDeployed and live
Build ArtifactsNoneNoneCreatedUsed for deploymentUnchangedUnchangedUnchanged
CloudFormation StackNoneNoneNoneCreated/UpdatedResources createdResources readyResources ready
Lambda FunctionNoneNoneNoneNoneCreatedReady to invokeReady to invoke
API GatewayNoneNoneNoneNoneCreatedEndpoint availableEndpoint available
Key Moments - 3 Insights
Why do we need to run 'sam build' before 'sam deploy'?
'sam build' prepares your code and dependencies into a deployable package. Without it, 'sam deploy' won't have the correct artifacts to create resources, as shown in execution_table step 2 and 3.
What happens if the SAM template is missing the 'Transform' section?
CloudFormation won't recognize SAM-specific resources like AWS::Serverless::Function, so deployment fails. This is implied in execution_table step 3 where the template is used for deployment.
How does the API Gateway connect to the Lambda function?
In the SAM template, the 'Events' section links the API path and method to the Lambda function. CloudFormation creates this connection during resource creation (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does AWS create the Lambda function and API Gateway?
AStep 4
BStep 2
CStep 6
DStep 3
💡 Hint
Refer to execution_table row with 'CloudFormation creates resources' which is step 4.
According to variable_tracker, what is the state of the SAM Template after step 3?
ANot yet defined
BDeployed and live
CUsed for deployment
DCreated by CloudFormation
💡 Hint
Check the 'SAM Template' row under 'After Step 3' column in variable_tracker.
If you skip 'sam build' and run 'sam deploy' directly, what will likely happen?
ACloudFormation stack is created but resources are missing
BDeployment fails due to missing build artifacts
CDeployment succeeds with no issues
DLambda function runs without API Gateway
💡 Hint
Refer to key_moments about the importance of 'sam build' before 'sam deploy'.
Concept Snapshot
AWS SAM is a template specification to define serverless apps.
Write a SAM template with Lambda and events.
Run 'sam build' to prepare deployment artifacts.
Run 'sam deploy' to create/update resources via CloudFormation.
App runs with Lambda and API Gateway connected.
Update template and redeploy to change app.
Full Transcript
The Serverless Application Model (SAM) lets you define serverless apps using a simple template. You write a SAM template describing Lambda functions and their triggers like API Gateway. Then you run 'sam build' to package your code and dependencies. Next, 'sam deploy' uses AWS CloudFormation to create or update your resources. Once deployed, your Lambda functions and API Gateway endpoints are live and ready to serve requests. You can monitor logs and metrics, then update your SAM template and redeploy to improve or fix your app. This flow helps you manage serverless apps easily and repeatably.