0Pricing
AWS for Backend Developers (EC2, S3, RDS, Lambda) · บทเรียน

Serverless Application Model (SAM)

กำหนด พัฒนา และปรับใช้แอปพลิเคชันแบบไร้เซิร์ฟเวอร์ด้วยเฟรมเวิร์ก AWS Serverless Application Model (SAM)

Serverless Application Model (SAM) เป็นบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What is AWS SAM?

Welcome to the AWS Serverless Application Model (SAM)! SAM is an open-source framework that helps you build, test, and deploy serverless applications on AWS faster and easier.

Think of it as a simplified way to define your serverless resources like Lambda functions and API Gateway endpoints.

SAM: CloudFormation's Helper

At its core, SAM is an extension of AWS CloudFormation. It provides a shorthand syntax for common serverless components.

When you deploy a SAM application, SAM transforms your simplified template into a full AWS CloudFormation template, ensuring you get all the benefits of CloudFormation's robust deployment capabilities.

SAM Template Structure

SAM applications are defined in a SAM template, typically a YAML file named template.yaml. Key sections include:

  • AWSTemplateFormatVersion: Standard CloudFormation version.
  • Transform: AWS::Serverless-2016-10-31: This line tells CloudFormation to process the template using SAM.
  • Resources: Where you define your serverless components (functions, APIs, databases).

Defining a Serverless Function

The primary resource for a Lambda function in SAM is AWS::Serverless::Function. This resource simplifies defining a Lambda function and its related configurations.

You specify properties like Handler (the function entry point), Runtime (e.g., python3.9), and CodeUri (the path to your function's code).

Integrating with API Gateway

To expose your Lambda function via an HTTP endpoint, you can directly define Events within your AWS::Serverless::Function resource.

Using the Api event type automatically provisions an Amazon API Gateway endpoint. You specify the Path and Method for your API route.

Your First SAM Template

Here's a basic template.yaml that defines a Python Lambda function triggered by an API Gateway GET request on the /hello path.

The CodeUri: hello_world/ indicates that the Lambda code is in a local directory named hello_world.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A simple "Hello World" SAM app.

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: GET

The Lambda Function Code

This is the Python code (app.py) that resides inside the hello_world/ directory. This function will be executed when the API Gateway endpoint is invoked.

It simply returns a JSON response with a "Hello from SAM Lambda!" message.

import json

def lambda_handler(event, context):
    """
    Handles API Gateway requests and returns a simple greeting.
    """
    print("Received an event:", event) # For logging
    return {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json"
        },
        "body": json.dumps({
            "message": "Hello from SAM Lambda!"
        }),
    }

# Example local invocation (for direct script testing)
if __name__ == "__main__":
    test_event = {
        "httpMethod": "GET",
        "path": "/hello",
        "requestContext": {"requestId": "test-id-123"},
        "headers": {"User-Agent": "test-client"}
    }
    response = lambda_handler(test_event, None)
    print("Local Response:", response)

Building Your SAM Application

Before you can deploy or even test your application locally, you need to build it. The sam build command processes your template, gathers dependencies, and prepares your code for deployment.

It creates a .aws-sam/build directory containing the processed artifacts.

sam build

Testing Locally with SAM CLI

The SAM CLI provides powerful tools for local testing, saving you time and money by avoiding repeated deployments to AWS.

  • sam local invoke: Invokes a single Lambda function locally with a given event.
  • sam local start-api: Starts a local HTTP server that emulates API Gateway, allowing you to interact with your serverless API from your browser or curl.

sam local start-api

Deploying Your Serverless App

Once your application is built and tested locally, you can deploy it to AWS using the sam deploy command.

The first time you deploy, it's recommended to use the --guided flag. This provides an interactive prompt to configure settings like stack name, AWS Region, and S3 bucket for deployment artifacts.

sam deploy --guided

SAM CLI Quick Check

Which SAM CLI command is used to test your serverless API Gateway locally, allowing you to interact with it from your browser or curl?

Recap & Next Steps

Congratulations! You've learned the fundamentals of the AWS Serverless Application Model (SAM).

  • SAM simplifies serverless development by extending CloudFormation.
  • You can define Lambda functions and API Gateway endpoints easily in a template.yaml.
  • The SAM CLI allows you to build, test locally, and deploy your serverless applications efficiently.

SAM is a powerful tool for developing modern, event-driven applications on AWS!

คำถามที่พบบ่อย

บทเรียน “Serverless Application Model (SAM)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “Serverless Application Model (SAM)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “Serverless Application Model (SAM)”

กำหนด พัฒนา และปรับใช้แอปพลิเคชันแบบไร้เซิร์ฟเวอร์ด้วยเฟรมเวิร์ก AWS Serverless Application Model (SAM) คุณปฏิบัติ AWS for Backend Developers (EC2, S3, RDS, Lambda) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AWS for Backend Developers (EC2, S3, RDS, Lambda) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “Serverless Application Model (SAM)” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) นี้ได้ไหม

ได้ บทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐาน CodeCommit และ CodeBuild
  2. CodeDeploy สำหรับ EC2/Lambda
  3. Serverless Application Model (SAM)
  4. โครงสร้างพื้นฐานในรูปโค้ดด้วย CloudFormation
← กลับไปที่ AWS for Backend Developers (EC2, S3, RDS, Lambda)