[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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ events
โ”‚ โ””โ”€โ”€ event.json
โ”œโ”€โ”€ hello_world
โ”‚ โ”œโ”€โ”€ __init__.py
โ”‚ โ”œโ”€โ”€ app.py
โ”‚ โ””โ”€โ”€ requirements.txt
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ samconfig.toml
โ”œโ”€โ”€ template.yaml
โ””โ”€โ”€ tests
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ integration
โ”‚ โ”œโ”€โ”€ __init__.py
โ”‚ โ””โ”€โ”€ test_api_gateway.py
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ unit
โ”œโ”€โ”€ __init__.py
โ””โ”€โ”€ test_handler.py
  • events/ - Directory for JSON files containing mock events (event payloads) used to test Lambda functions
  • hello_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 tests
  • samconfig.toml - Stores SAM CLI execution parameters (e.g., deployment region, Stack name, S3 bucket) to simplify configuration when running sam 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
2
3
4
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
TODO....
  1. AWSTemplateFormatVersion - The version number of AWS CloudFormation
  2. Transform - Tells CloudFormation that this template uses SAM to extend the syntax
  3. Description - Project description

Content

  1. Globals - Default settings provided to Resources
  2. Resources - AWS resources to create; refer to AWS resource and property types reference, AWS SAM resources and properties
  3. 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
2
3
4
5
6
7
8
9
10
11
No current session found, using default AWS::AccountId                                                       
Invoking app.lambda_handler (python3.13)
Local image is up-to-date
Using local image: public.ecr.aws/lambda/python:3.13-rapid-x86_64.

Mounting /Users/XXXXXX/Documents/TEST/hello_world as
/var/task:ro,delegated, inside runtime container
START RequestId: f6a6ec50-58b2-432c-9381-ec45ca43b130 Version: $LATEST
END RequestId: e883464b-1216-43ae-b0fe-f7f803a73057
REPORT RequestId: e883464b-1216-43ae-b0fe-f7f803a73057 Init Duration: 1.26 ms Duration: 381.91 ms Billed Duration: 382 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}

๐Ÿ” Recap

  • Installation commands
  • Understand the AWS SAM project structure
  • Know the purpose of specific files template.yaml and samconfig.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/

Author

Hsiang-Jen Li

Posted on

2025-06-20

Updated on

2025-06-20

Licensed under