โมเดลแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ของ AWS (SAM)
เรียนรู้การกำหนด พัฒนา และนำแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ไปใช้งานด้วย AWS SAM ซึ่งเป็นเฟรมเวิร์กโอเพนซอร์สสำหรับการสร้างระบบบน AWS
โมเดลแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ของ AWS (SAM) เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โมเดลแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ของ AWS (SAM)”
เรียนรู้การกำหนด พัฒนา และนำแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ไปใช้งานด้วย AWS SAM ซึ่งเป็นเฟรมเวิร์กโอเพนซอร์สสำหรับการสร้างระบบบน AWS คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “โมเดลแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ของ AWS (SAM)” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม
ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โมเดลแอปพลิเคชันแบบไร้เซิร์ฟเวอร์ของ AWS (SAM)
- บทนำสู่เฟรมเวิร์กแบบไร้เซิร์ฟเวอร์
- CI/CD สำหรับแอปแบบไร้เซิร์ฟเวอร์
- การทดสอบภายในเครื่องด้วย SAM CLI