部署 SAM 应用
使用 SAM CLI 构建、本地测试并将无服务器应用部署到 AWS,同时管理不同环境
部署 SAM 应用 是 CoddyKit 上的免费 Serverless Backend with AWS Lambda & API Gateway 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless Backend with AWS Lambda & API Gateway 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless Backend with AWS Lambda & API Gateway 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Introduction to SAM CLI
Welcome to deploying SAM applications! The AWS Serverless Application Model Command Line Interface (SAM CLI) is your best friend for building and managing serverless projects.
It extends AWS CloudFormation to simplify defining serverless resources. The SAM CLI provides commands to build, test, and deploy your serverless applications.
Building Your Application
Before deploying, your SAM application needs to be built. The sam build command takes your application code and dependencies, then prepares them for deployment.
- It packages dependencies (e.g., Python libraries).
- It transpiles code if needed (e.g., TypeScript to JavaScript).
- It creates a deployable artifact in a
.aws-sam/builddirectory.
Example Lambda Function
Let's look at a simple Python Lambda function and its template.yaml. The sam build command processes this to create deployable code.
After running sam build, the packaged code would be ready for local testing or deployment.
import json
def lambda_handler(event, context):
"""
A simple Lambda handler.
"""
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"message": "Hello from your SAM Lambda!"
})
}Local Testing with `sam local invoke`
The SAM CLI allows you to test your Lambda functions locally without deploying them to AWS, saving time and costs. Use sam local invoke to simulate a Lambda invocation.
You can pass event data directly to your function, just like how AWS services (like API Gateway or S3) would trigger it.
Running a Lambda Locally
To test the Lambda function from the previous scene, you'd use sam local invoke with the logical ID of your function and an event file.
For example, sam local invoke MyHelloWorldFunction --event event.json would test a function named 'MyHelloWorldFunction' using the data in event.json.
{
"httpMethod": "GET",
"path": "/hello",
"queryStringParameters": null,
"headers": {
"Accept": "*/*"
},
"body": null
}Local API Testing with `sam local start-api`
If your SAM application includes an API Gateway, you can simulate the entire API locally using sam local start-api. This command starts a local web server that mimics API Gateway.
You can then send HTTP requests to http://127.0.0.1:3000 and test your API endpoints end-to-end, locally.
Preparing for Deployment: `sam deploy`
Once your application is built and tested locally, you're ready to deploy it to AWS. The sam deploy command is used for this.
It packages your application artifacts, uploads them to an S3 bucket, and then uses AWS CloudFormation to deploy your serverless resources.
Your First Guided Deployment
For your first deployment, using the --guided flag is highly recommended. It will walk you through setting up necessary parameters interactively.
- Stack Name: Unique identifier for your CloudFormation stack.
- AWS Region: Where to deploy your resources.
- S3 Bucket: For storing deployment artifacts.
- Capabilities: Acknowledging IAM resource creation.
Example: sam deploy --guided
Managing Multiple Environments
In real-world scenarios, you'll often have development, staging, and production environments. SAM CLI helps manage this by allowing you to specify different stack names and parameters.
- Use distinct Stack Names (e.g.,
my-app-dev,my-app-prod). - Use
--parameter-overridesto pass different values for environment-specific configurations (e.g., database names, API keys).
Quick Check
Which SAM CLI command is used to test your API Gateway endpoints locally?
Recap & Next Steps
Congratulations! You've learned the essential steps for building, testing, and deploying serverless applications using the SAM CLI.
sam build: Packages and prepares your application.sam local invoke: Tests individual Lambda functions locally.sam local start-api: Simulates API Gateway for local API testing.sam deploy: Deploys your application to AWS CloudFormation.
Mastering these commands is key to efficient serverless development!
常见问题解答
「部署 SAM 应用」课时是免费的吗?
是的 — 「部署 SAM 应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless Backend with AWS Lambda & API Gateway 课程的其余内容,请升级到 CoddyKit PRO。 Serverless Backend with AWS Lambda & API Gateway 课程共包含 4 节课。
「部署 SAM 应用」这节课中我会学到什么?
使用 SAM CLI 构建、本地测试并将无服务器应用部署到 AWS,同时管理不同环境 你通过在浏览器中直接运行的动手代码来练习 Serverless Backend with AWS Lambda & API Gateway,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless Backend with AWS Lambda & API Gateway 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless Backend with AWS Lambda & API Gateway 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「部署 SAM 应用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless Backend with AWS Lambda & API Gateway 课中编写并运行代码吗?
能。每节 Serverless Backend with AWS Lambda & API Gateway 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。