รูปแบบ Fan-Out ด้วย SNS
เรียนรู้รูปแบบการส่งข้อความแบบ Fan-Out โดยใช้ Amazon SNS เพื่อส่งเหตุการณ์เดียวไปยังผู้ใช้ Lambda อิสระหลายรายพร้อมกัน
รูปแบบ Fan-Out ด้วย SNS เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Fan-Out?
Fan-out means a single event is delivered to many subscribers at once, each processing it independently and in parallel.
Why Not Call Each Directly?
Calling each downstream function from your producer tightly couples them. Adding a consumer means changing the producer. Fan-out decouples this.
SNS as the Hub
Amazon SNS is a pub/sub topic. Publishers send a message once; SNS delivers a copy to every subscriber, including multiple Lambdas.
Publishing a Message
The producer publishes to the topic ARN. It does not know or care who is subscribed.
import boto3, json
sns = boto3.client('sns')
sns.publish(
TopicArn='arn:aws:sns:us-east-1:123:orders',
Message=json.dumps({'orderId': 42})
)The SNS Event Shape
Each subscribed Lambda receives the message wrapped in an SNS records envelope.
{
"Records": [{
"Sns": {
"Message": "{\"orderId\": 42}",
"TopicArn": "arn:aws:sns:us-east-1:123:orders"
}
}]
}A Consumer Handler
Each consumer parses the message from the record and does its own job: one emails a receipt, another updates inventory.
import json
def handler(event, context):
for record in event['Records']:
msg = json.loads(record['Sns']['Message'])
print('Processing order', msg['orderId'])Fan-Out plus SQS
For reliability, subscribe SQS queues to the topic and let Lambda poll the queues. This buffers spikes and gives each consumer its own retry and DLQ.
Message Filtering
SNS filter policies let a subscriber receive only messages matching attributes, so not every consumer processes every event.
{
"eventType": ["order.created"]
}SNS vs EventBridge
SNS is simple, high-throughput pub/sub. EventBridge adds richer routing, schemas, and many SaaS sources. Choose SNS for raw fan-out, EventBridge for complex routing.
Ordering Caveat
Standard SNS topics do not guarantee order or exactly-once delivery. Use SNS FIFO topics when ordering and deduplication matter.
Idempotent Consumers
Because a message may be delivered more than once, each consumer should be idempotent so duplicates do not cause double effects.
Quick Check
Test your fan-out knowledge.
Recap
You learned the fan-out pattern: publish once to SNS, deliver to many parallel consumers, add SQS for durability, filter messages, and keep consumers idempotent.
คำถามที่พบบ่อย
บทเรียน “รูปแบบ Fan-Out ด้วย SNS” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รูปแบบ Fan-Out ด้วย SNS” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบ Fan-Out ด้วย SNS”
เรียนรู้รูปแบบการส่งข้อความแบบ Fan-Out โดยใช้ Amazon SNS เพื่อส่งเหตุการณ์เดียวไปยังผู้ใช้ Lambda อิสระหลายรายพร้อมกัน คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “รูปแบบ Fan-Out ด้วย SNS” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม
ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเรียกใช้ Lambda แบบไม่พร้อมกัน
- คิวจดหมายตาย (DLQ) สำหรับความล้มเหลว
- การประสานงานด้วย AWS Step Functions
- รูปแบบ Fan-Out ด้วย SNS