AWS SNS(Simple Notification Service)는 완전 관리형 발행-구독(Pub/Sub) 메시징 서비스로, 마이크로서비스, 분산 시스템, 서버리스 애플리케이션의 디커플링을 지원한다.
SNS 핵심 개념
| 개념 | 설명 |
|---|
| Topic | 메시지 발행 엔드포인트 |
| Subscription | 토픽 구독 (SQS, Lambda, HTTP, 이메일, SMS) |
| Message Filtering | 구독자별 메시지 필터 정책 |
| FIFO Topic | 순서 보장, 중복 제거 |
| DLQ | 전송 실패 메시지 보관 |
SNS 토픽 생성 및 발행
python
import boto3
import json
sns = boto3.client('sns', region_name='ap-northeast-2')
# 토픽 생성
topic = sns.create_topic(Name='order-events')
topic_arn = topic['TopicArn']
# SQS 구독
sns.subscribe(
TopicArn=topic_arn,
Protocol='sqs',
Endpoint='arn:aws:sqs:ap-northeast-2:123456789:order-processor',
Attributes={
'FilterPolicy': json.dumps({
'eventType': ['ORDER_PLACED', 'ORDER_UPDATED']
}),
},
)
# Lambda 구독
sns.subscribe(
TopicArn=topic_arn,
Protocol='lambda',
Endpoint='arn:aws:lambda:ap-northeast-2:123456789:function:send-email',
Attributes={
'FilterPolicy': json.dumps({
'eventType': ['ORDER_COMPLETED']
}),
},
)
# 메시지 발행
sns.publish(
TopicArn=topic_arn,
Message=json.dumps({
'orderId': 'ORD-001',
'customerId': 'CUST-123',
'amount': 99.99,
}),
MessageAttributes={
'eventType': {
'DataType': 'String',
'StringValue': 'ORDER_PLACED',
},
},
)
| 항목 | SNS | SQS | EventBridge |
|---|
| 패턴 | Pub/Sub | 큐 | 이벤트 버스 |
| 구독자 수 | 다수 | 단일 소비자 | 다수 |
| 메시지 보존 | 없음 | 최대 14일 | 아카이브 설정 |
| 필터링 | 메시지 속성 | 없음 | 이벤트 패턴 |
| 외부 연동 | 제한적 | 없음 | 200+ SaaS |
Fan-out 패턴
주문 서비스 → SNS Topic
├── SQS Queue 1 → Lambda: 결제 처리
├── SQS Queue 2 → Lambda: 재고 업데이트
├── SQS Queue 3 → Lambda: 이메일 발송
└── Lambda: 분석 이벤트 기록