AWS for DevOps — X-Ray & SNS Notifications
10.5 — X-Ray & SNS Notifications
X-Ray traces requests across distributed services. SNS delivers alerts to your team. Together, they close the loop: detect → understand → notify.
AWS X-Ray
How X-Ray Works
┌──────┐ ┌──────────┐ ┌──────────┐ ┌─────────┐
│Client│────→│API Gateway│────→│Lambda Fn │────→│DynamoDB │
└──────┘ └──────────┘ └──────────┘ └─────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────┐
│ X-Ray — Trace Map │
│ Shows latency, errors, connections │
└─────────────────────────────────────────┘
Real-World Analogy
X-Ray is like a package tracking system. When you order something online, you can see: shipped from warehouse → arrived at sorting center → out for delivery → delivered. X-Ray does the same for API requests through your microservices.
Enabling X-Ray on Lambda
# Update a Lambda function to enable X-Ray tracing
$ aws lambda update-function-configuration \
--function-name my-api-handler \
--tracing-config Mode=Active
{
"FunctionName": "my-api-handler",
"TracingConfig": {
"Mode": "Active"
}
}
Querying Traces
# Get trace summaries for the last 10 minutes
$ aws xray get-trace-summaries \
--start-time $(date -u -v-10M +%s) \
--end-time $(date -u +%s) \
--filter-expression 'service("my-api-handler") AND responsetime > 1'
{
"TraceSummaries": [
{
"Id": "1-abc123-def456",
"Duration": 2.3,
"ResponseTime": 2.1,
"Http": {
"HttpURL": "https://api.example.com/users",
"HttpStatus": 200,
"HttpMethod": "GET"
}
}
]
}
# Get full trace details
$ aws xray batch-get-traces --trace-ids "1-abc123-def456"
X-Ray Service Map
The service map shows how your services are connected and where bottlenecks exist:
| Metric | What It Shows | Action |
|---|---|---|
| Response time | How long each service takes | Optimize slow services |
| Error rate | Percentage of failed requests | Fix failing services |
| Throughput | Requests per second | Scale busy services |
| Connections | Which services call which | Identify coupling |
Amazon SNS (Simple Notification Service)
SNS is a pub/sub messaging service. For DevOps, it’s the primary way to deliver alerts from CloudWatch Alarms to your team.
Creating an SNS Topic and Subscription
# Create a topic for alerts
$ aws sns create-topic --name devops-alerts
{
"TopicArn": "arn:aws:sns:ap-south-1:123456789012:devops-alerts"
}
# Subscribe an email
$ aws sns subscribe \
--topic-arn arn:aws:sns:ap-south-1:123456789012:devops-alerts \
--protocol email \
--notification-endpoint oncall@example.com
{
"SubscriptionArn": "pending confirmation"
}
# Subscribe a Slack webhook via HTTPS
$ aws sns subscribe \
--topic-arn arn:aws:sns:ap-south-1:123456789012:devops-alerts \
--protocol https \
--notification-endpoint https://hooks.slack.com/services/T00/B00/xxx
Connecting CloudWatch Alarms to SNS
# Create an alarm that notifies via SNS
$ aws cloudwatch put-metric-alarm \
--alarm-name high-error-rate \
--metric-name 5XXError \
--namespace AWS/ApiGateway \
--statistic Sum \
--period 300 \
--threshold 10 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 2 \
--alarm-actions arn:aws:sns:ap-south-1:123456789012:devops-alerts \
--ok-actions arn:aws:sns:ap-south-1:123456789012:devops-alerts
Notification Flow
CloudWatch Alarm ──→ SNS Topic ──→ Email
(5XX > 10) │ Slack (via HTTPS)
│ PagerDuty (via HTTPS)
└──→ Lambda ──→ Custom processing
Alert Routing Strategy
| Alert Level | SNS Topic | Subscribers | Example |
|---|---|---|---|
| P1 — Critical | critical-alerts |
PagerDuty + phone | Service down, data loss |
| P2 — High | devops-alerts |
Slack #incidents + email | High error rate, latency spike |
| P3 — Warning | warning-alerts |
Slack #monitoring | Disk 80%, approaching limits |
| P4 — Info | info-alerts |
Slack #deployments | Deploy complete, scaling event |
Common Mistakes
| Mistake | Why It Hurts | Fix |
|---|---|---|
| One SNS topic for everything | Alert fatigue — all alerts look the same | Separate topics by severity |
| No OK action on alarms | Team doesn’t know when issue resolves | Add --ok-actions to alarms |
| Email-only notifications | Slow response — who checks email at 3 AM? | Use PagerDuty/Slack for critical alerts |
| Not enabling X-Ray | Can’t trace issues across services | Enable Active tracing on Lambda/API Gateway |
Quick Reference
| Task | Command |
|---|---|
| Create SNS topic | aws sns create-topic --name NAME |
| Subscribe email | aws sns subscribe --topic-arn ARN --protocol email --notification-endpoint EMAIL |
| Publish test message | aws sns publish --topic-arn ARN --subject "Test" --message "Hello" |
| List subscriptions | aws sns list-subscriptions-by-topic --topic-arn ARN |
| Enable X-Ray on Lambda | aws lambda update-function-configuration --function-name FN --tracing-config Mode=Active |
| Get X-Ray traces | aws xray get-trace-summaries --start-time T1 --end-time T2 |
Next → Monitoring Exercises