[tutorial] Using AWS SAM to Create and Run Lambda Functions
Note: This page is an AI-generated (gpt-5-mini-2025-08-07) translation from Traditional Chinese and may contain minor inaccuracies.
๐ Introduction
This guide explains how to use AWS SAM to create and run Lambda functions, including basic installation, project structure explanation, and the workflow for running Lambdas locally
๐ Quick Start
AWS CLI
1 | brew install awscli |
AWS SAM (Serverless Application Model) CLI
1 | brew install aws-sam-cli |
Initialize SAM project
1 | sam init |
Project structure overview
1 | . |
events/
- Directory for JSON files containing mock events (event payloads) used to test Lambda functionshello_world/
- Where the application is placed; Lambda-related functions including the handler (e.g., app.py) and dependencies (e.g., requirements.txt)tests/
- Folder for unit testssamconfig.toml
- Stores SAM CLI execution parameters (e.g., deployment region, Stack name, S3 bucket) to simplify configuration when runningsam deploy
template.yaml
- Defines all Lambda functions; the main IaC core file (which services are used and their dependencies); itโs an extension of AWS CloudFormation syntax
template.yaml
You can use the CloudFormation Linter tool
cfn-lint
to detect formatting and property errors in the file
Header
1 | AWSTemplateFormatVersion: '2010-09-09' |
- AWSTemplateFormatVersion - The version number of AWS CloudFormation
- Transform - Tells CloudFormation that this template uses SAM to extend the syntax
- Description - Project description
Content
- Globals - Default settings provided to Resources
- Resources - AWS resources to create; refer to AWS resource and property types reference, AWS SAM resources and properties
- Outputs - Expected outputs after deployment (the information you need to know)
samconfig.toml
See AWS SAM CLI configuration file, Configuring the AWS SAM CLI, AWS SAM CLI command reference
The purpose is to simplify the complexity when using sam commands
Original | Optimized with samconfig |
---|---|
sam build --cached --parallel --use-containers |
sam build |
sam local invoke --env-vars locals.json |
sam local invoke |
sam local start-api --env-vars locals.json --warm-containers EAGER |
sam local start-api |
Local Invoke
1 | sam local invoke |
1 | No current session found, using default AWS::AccountId |
๐ Recap
- Installation commands
- Understand the AWS SAM project structure
- Know the purpose of specific files
template.yaml
andsamconfig.toml
and their official documentation for fields - Invoke a simple
Hello World
Lambda function locally
๐ References
[tutorial] Using AWS SAM to Create and Run Lambda Functions
https://hsiangjenli.github.io/blog/tutorial-aws-sam-lambda.en/