AWS 无服务器应用模型(SAM)
学习使用 AWS SAM 定义、开发和部署无服务器应用;AWS SAM 是一个用于在 AWS 上构建应用的开源框架
AWS 无服务器应用模型(SAM) 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless AWS Lambda Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless AWS Lambda Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Simplify Serverless with AWS SAM
AWS Serverless Application Model (SAM) is an open-source framework that simplifies building and deploying serverless applications on AWS.
It extends AWS CloudFormation, giving you a special, easier syntax for defining serverless resources like Lambda functions, API Gateways, and DynamoDB tables. SAM helps you manage your entire serverless app as a single unit.
SAM Template: The Blueprint
At the core of every SAM application is the template.yaml file. This YAML (or JSON) file acts as the blueprint, describing all the serverless resources that make up your application.
SAM templates are essentially AWS CloudFormation templates, but they include a crucial Transform property that unlocks SAM's simplified, serverless-specific syntax.
Anatomy of a SAM Template
Let's look at the key sections you'll find in a template.yaml file:
AWSTemplateFormatVersion: Standard CloudFormation version.Transform: AWS::Serverless-2016-10-31: This is vital! It tells CloudFormation to use SAM's simplified syntax.Description: A short explanation of your application.Resources: This is where you define all your serverless components like Lambda functions, APIs, and databases.
Defining a Serverless Function
In SAM, you define a Lambda function using the AWS::Serverless::Function resource type. You specify essential properties like its runtime, entry point (handler), memory, and how it gets triggered.
Type: AWS::Serverless::Function: Identifies it as a Lambda.Handler: The function within your code that AWS Lambda calls to begin execution (e.g.,app.lambda_handler).Runtime: The programming language environment (e.g.,python3.9,nodejs16.x).CodeUri: The local path to your function's source code.Events: Specifies how your function is invoked (e.g., an API Gateway endpoint).
Our 'Hello World' Lambda Handler
Here's a simple Python Lambda function. It will be the code executed when our serverless function is invoked. It just returns a 'Hello from Lambda!' message.
import json
def lambda_handler(event, context):
# Log the event for debugging purposes
print("Received event: " + json.dumps(event))
# Basic response for API Gateway
response = {
"statusCode": 200,
"body": json.dumps("Hello from Lambda!")
}
return responseSAM Template for Our Function
Now, let's define our 'Hello World' Lambda function in a template.yaml. We'll set it up to be triggered by an API Gateway GET request at the /hello path.
This template assumes your Python code (app.py) is in a folder named hello_world/ next to this template.yaml file.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A simple "Hello World" serverless application.
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.9
CodeUri: hello_world/
MemorySize: 128
Timeout: 30
Events:
HelloWorldApi:
Type: Api
Properties:
Path: /hello
Method: GETThe SAM CLI: Your Deployment Tool
The AWS SAM Command Line Interface (CLI) is your main tool for developing and deploying SAM applications. It provides commands to initialize projects, build dependencies, test locally, and deploy your application to AWS.
sam init: Start a new serverless project.sam build: Package your application code and dependencies.sam deploy: Deploy your application to AWS CloudFormation.
Building Your SAM Application
Before you can deploy your application, you need to 'build' it. The sam build command collects your code, bundles any dependencies (like Python packages), and prepares everything into a deployable package.
This command creates a .aws-sam directory containing the processed artifacts.
sam buildDeploying Your SAM Application
Once built, use sam deploy to push your application to AWS. This command creates or updates a CloudFormation stack based on your SAM template, provisioning all the necessary resources.
You'll need to specify a unique stack name and an S3 bucket for code artifacts. The --capabilities CAPABILITY_IAM argument acknowledges that your template creates IAM roles.
sam deploy --stack-name my-hello-app \
--s3-bucket your-unique-s3-bucket-name \
--capabilities CAPABILITY_IAM \
--region us-east-1Quick Check: SAM Template Components
Which of the following are essential components or sections found within a typical AWS SAM template (template.yaml)?
Recap: Your SAM Journey Begins!
You've just learned the fundamentals of AWS SAM!
- SAM simplifies serverless application development and deployment using a CloudFormation-based template.
- Key components include the
template.yamlwith itsTransformproperty andResourcessection. - The SAM CLI provides commands like
sam init,sam build, andsam deployto manage your applications.
With SAM, you can efficiently define, build, and deploy complex serverless architectures, making it a powerful tool for serverless development.
常见问题解答
「AWS 无服务器应用模型(SAM)」课时是免费的吗?
是的 — 「AWS 无服务器应用模型(SAM)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「AWS 无服务器应用模型(SAM)」这节课中我会学到什么?
学习使用 AWS SAM 定义、开发和部署无服务器应用;AWS SAM 是一个用于在 AWS 上构建应用的开源框架 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless AWS Lambda Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「AWS 无服务器应用模型(SAM)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?
能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- AWS 无服务器应用模型(SAM)
- 无服务器框架入门
- 无服务器应用的持续集成/持续交付
- 使用 SAM CLI 进行本地测试