0Pricing
Serverless Backend with AWS Lambda & API Gateway · บทเรียน

การนำแอปพลิเคชัน SAM ไปใช้งาน

ใช้ SAM CLI เพื่อสร้าง ทดสอบภายในเครื่อง และนำแอปพลิเคชันไร้เซิร์ฟเวอร์ไปใช้งานบน AWS พร้อมจัดการสภาพแวดล้อมต่าง ๆ

การนำแอปพลิเคชัน SAM ไปใช้งาน เป็นบทเรียน Serverless Backend with AWS Lambda & API Gateway ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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/build directory.

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-overrides to 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless Backend with AWS Lambda & API Gateway หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless Backend with AWS Lambda & API Gateway บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การนำแอปพลิเคชัน SAM ไปใช้งาน” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Serverless Backend with AWS Lambda & API Gateway นี้ได้ไหม

ได้ บทเรียน Serverless Backend with AWS Lambda & API Gateway ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. บทนำสู่ AWS SAM
  2. การกำหนดทรัพยากรไร้เซิร์ฟเวอร์
  3. การนำแอปพลิเคชัน SAM ไปใช้งาน
  4. การทดสอบและแก้ไขข้อบกพร่องภายในเครื่องด้วย SAM CLI
← กลับไปที่ Serverless Backend with AWS Lambda & API Gateway