Serverless AWS Lambda Development · บทเรียน

ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch

เรียนรู้การส่งตัวชี้วัดทางธุรกิจและประสิทธิภาพของคุณเองจาก Lambda และเรียกใช้การแจ้งเตือนกับการแจ้งข่าวสารเมื่อค่าเกินเกณฑ์

บทเรียน 4 จาก 413 ขั้นตอน

ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

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

Beyond Default Metrics

Lambda publishes built-in metrics like invocations and errors, but your app has its own signals, such as orders processed or items failed. These are custom metrics.

Embedded Metric Format

The easiest way to emit metrics is the Embedded Metric Format (EMF): print structured JSON to logs and CloudWatch extracts metrics automatically.

{
  "_aws": {
    "CloudWatchMetrics": [{
      "Namespace": "OrdersApp",
      "Metrics": [{"Name": "OrdersProcessed", "Unit": "Count"}]
    }]
  },
  "OrdersProcessed": 12
}

Emitting EMF in Python

Build the EMF object and print it as JSON. No extra API call or latency is needed.

import json, time

def emit(count):
    print(json.dumps({
        '_aws': {
            'Timestamp': int(time.time() * 1000),
            'CloudWatchMetrics': [{
                'Namespace': 'OrdersApp',
                'Dimensions': [[]],
                'Metrics': [{'Name': 'OrdersProcessed', 'Unit': 'Count'}]
            }]
        },
        'OrdersProcessed': count
    }))

PutMetricData API

Alternatively call put_metric_data directly. This is synchronous and adds latency, so EMF is usually preferred for hot paths.

import boto3
cw = boto3.client('cloudwatch')
cw.put_metric_data(Namespace='OrdersApp',
  MetricData=[{'MetricName': 'OrdersProcessed', 'Value': 1}])

Dimensions Add Context

Dimensions are key-value tags that slice a metric, such as by region or customer tier. Keep dimension cardinality low to control cost.

What an Alarm Does

A CloudWatch alarm watches a metric and changes state when it breaches a threshold for a set number of periods, then triggers an action.

Creating an Alarm

This alarm fires when the error count exceeds 5 in a single one-minute period.

aws cloudwatch put-metric-alarm \
  --alarm-name orders-errors \
  --metric-name Errors \
  --namespace AWS/Lambda \
  --threshold 5 \
  --comparison-operator GreaterThanThreshold \
  --evaluation-periods 1 \
  --period 60

Notifying with SNS

Point the alarm action at an SNS topic so it emails, texts, or pages your on-call when triggered.

Alarm States

Alarms have three states: OK, ALARM, and INSUFFICIENT_DATA. The last means the metric had no data points in the window.

Avoid Alarm Fatigue

Too many noisy alarms train people to ignore them. Alarm on symptoms users feel, like error rate and latency, not every minor metric.

Dashboards Tie It Together

Build a CloudWatch dashboard combining built-in and custom metrics so you see the whole system health at a glance during an incident.

Quick Check

Test your metrics knowledge.

Recap

You learned to emit custom metrics via EMF or PutMetricData, add dimensions, create threshold alarms wired to SNS, and avoid alarm fatigue with dashboards.

เริ่มต้นได้ฟรี

เรียนรู้ Serverless AWS Lambda Development ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

บทเรียน “ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch”

เรียนรู้การส่งตัวชี้วัดทางธุรกิจและประสิทธิภาพของคุณเองจาก Lambda และเรียกใช้การแจ้งเตือนกับการแจ้งข่าวสารเมื่อค่าเกินเกณฑ์ คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

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

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

บทเรียน “ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch” ใช้เวลานานแค่ไหน

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

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

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

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

  1. บันทึกและตัวชี้วัดของ CloudWatch
  2. การจัดการข้อผิดพลาดและการลองใหม่
  3. การแก้จุดบกพร่องแอปพลิเคชันแบบไร้เซิร์ฟเวอร์
  4. ตัวชี้วัดแบบกำหนดเองและการแจ้งเตือน CloudWatch
← กลับไปที่ Serverless AWS Lambda Development