การกำหนดทรัพยากรไร้เซิร์ฟเวอร์
เขียนเทมเพลต SAM เพื่อกำหนดฟังก์ชัน Lambda จุดปลายทาง API Gateway ตาราง DynamoDB และองค์ประกอบไร้เซิร์ฟเวอร์อื่น ๆ
การกำหนดทรัพยากรไร้เซิร์ฟเวอร์ เป็นบทเรียน Serverless Backend with AWS Lambda & API Gateway ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless Backend with AWS Lambda & API Gateway และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What are SAM Templates?
AWS Serverless Application Model (SAM) templates are a powerful way to define your serverless applications using Infrastructure as Code (IaC).
Instead of manually configuring resources in the AWS console, you describe them in a YAML or JSON file. This makes your deployments repeatable, version-controlled, and easier to manage.
Anatomy of a SAM Template
A SAM template has a clear structure. The most important parts are:
AWSTemplateFormatVersion: Specifies the template format version.Transform: AlwaysAWS::Serverless-2016-10-31for SAM.Description: A brief description of your application.Resources: Where you define all your AWS components.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My First SAM App
Resources:
# Your serverless resources go hereDefining a Serverless Function
The core of many serverless applications is the AWS Lambda function. In SAM, you declare a Lambda using the AWS::Serverless::Function resource type.
You need to specify its handler, runtime, and where its code is located.
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.9
CodeUri: s3://my-bucket/my-app.zip
MemorySize: 128
Timeout: 30Key Lambda Properties
Let's break down some common AWS::Serverless::Function properties:
Handler: The entry point in your code (e.g.,filename.function_name).Runtime: The programming language runtime (e.g.,python3.9,nodejs18.x).CodeUri: Path to your function's deployment package (local or S3).MemorySize: RAM allocated to the function (MB).Timeout: Max execution time (seconds).
API Gateway as an Event Source
To make your Lambda function accessible via an HTTP endpoint, you integrate it with Amazon API Gateway. In SAM, you do this by adding an Events property to your function.
The HttpApi type is often preferred for its simplicity and cost-effectiveness.
Resources:
MyApiFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.9
CodeUri: s3://my-bucket/api-app.zip
Events:
MyApiEvent:
Type: HttpApi
Properties:
Path: /hello
Method: GETDefining a DynamoDB Table
For data persistence, you can define a DynamoDB table directly in your SAM template. SAM provides a convenient AWS::Serverless::SimpleTable resource type.
This creates a basic DynamoDB table with a primary key. For more advanced configurations, you can use AWS::DynamoDB::Table.
Resources:
MyDataTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: String
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1IAM Permissions for Resources
Your Lambda functions often need permission to interact with other AWS services, like DynamoDB. You grant these permissions using IAM (Identity and Access Management) policies.
SAM allows you to attach policies directly to your Lambda function's execution role using the Policies property.
Resources:
MyLambdaWithDbAccess:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.9
CodeUri: s3://my-bucket/db-app.zip
Policies:
- DynamoDBReadWriteAccess:
TableName: !Ref MyDataTable # Grants access to MyDataTable
Events:
MyApiEvent:
Type: HttpApi
Properties:
Path: /items
Method: GET
MyDataTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: id
Type: StringExporting Important Values
After deploying your serverless application, you often need to know the endpoint URL of your API or the name of your DynamoDB table.
The Outputs section in your SAM template allows you to export these values, making them easily accessible after deployment.
Outputs:
ApiUrl:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/hello"A Full Serverless Template
Here's a more complete SAM template that defines an HTTP API endpoint, a Lambda function to handle requests, and a DynamoDB table for data storage.
Notice how different resource types are declared and linked together.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A complete serverless API with Lambda and DynamoDB
Resources:
MyApiFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python3.9
CodeUri: s3://my-bucket/full-app.zip
Policies:
- DynamoDBReadWriteAccess:
TableName: !Ref MyItemsTable
Events:
MyApiEvent:
Type: HttpApi
Properties:
Path: /items
Method: GET
MyItemsTable:
Type: AWS::Serverless::SimpleTable
Properties:
PrimaryKey:
Name: itemId
Type: String
Outputs:
ApiEndpoint:
Description: "API Gateway endpoint URL"
Value: !Sub "https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/items"SAM Template Check
Which of the following are valid top-level sections in a SAM template?
Recap: Defining Serverless Resources
In this lesson, you learned how to define various serverless resources within a SAM template. We covered:
- The basic structure of a SAM template.
- Declaring
AWS::Serverless::Functionfor Lambda. - Integrating Lambda with API Gateway using
Events. - Defining
AWS::Serverless::SimpleTablefor DynamoDB. - Granting permissions with IAM
Policies. - Using the
Outputssection to export values.
Next, we'll learn how to deploy these templates to AWS!
คำถามที่พบบ่อย
บทเรียน “การกำหนดทรัพยากรไร้เซิร์ฟเวอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกำหนดทรัพยากรไร้เซิร์ฟเวอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless Backend with AWS Lambda & API Gateway ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless Backend with AWS Lambda & API Gateway มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดทรัพยากรไร้เซิร์ฟเวอร์”
เขียนเทมเพลต SAM เพื่อกำหนดฟังก์ชัน Lambda จุดปลายทาง API Gateway ตาราง DynamoDB และองค์ประกอบไร้เซิร์ฟเวอร์อื่น ๆ คุณปฏิบัติ Serverless Backend with AWS Lambda & API Gateway ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless Backend with AWS Lambda & API Gateway หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless Backend with AWS Lambda & API Gateway บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนดทรัพยากรไร้เซิร์ฟเวอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless Backend with AWS Lambda & API Gateway นี้ได้ไหม
ได้ บทเรียน Serverless Backend with AWS Lambda & API Gateway ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ AWS SAM
- การกำหนดทรัพยากรไร้เซิร์ฟเวอร์
- การนำแอปพลิเคชัน SAM ไปใช้งาน
- การทดสอบและแก้ไขข้อบกพร่องภายในเครื่องด้วย SAM CLI