使用CloudWatch作为事件通知

在本节,我们将设置 SES event publishing来跟踪邮件发送事件并将其记录到 CloudWatch。 通过将邮件事件记录到 CloudWatch,可以创建自定义Dashboard、设置警报或根据特定邮件事件触发操作。

在Configuration Set中开启CloudWatch Metric

TechNewsConfigSet中添加一个Event Destination

image-20231005110448989

事件类型选择"send", "delivery", "bounce", "complaint"

image-20231005110550233

destination的配置如下:

image-20231005110807346

通过设置dimension,可以根据Message Tag进一步对 Cloudwatch 指标进行切分以满足报告需求。 例如,可以选择创建一个 Cloudwatch Dashboard来展示特定部门的邮件指标,或者设置一个 Cloudwatch 警报,每当特定业务部门的退回邮件数量达到特定百分比时就会触发该警报,以保持合规。

创建完成后的页面如下:

image-20231005110903899

上面的操作同样可以通过CLI来完成:

aws sesv2 create-configuration-set-event-destination \
--configuration-set-name TechNewsConfigSet \
--event-destination-name CloudWatchEmailEventDestination \
--event-destination '{
    "Enabled": true,
    "MatchingEventTypes": ["SEND", "DELIVERY", "BOUNCE", "COMPLAINT"],
    "CloudWatchDestination": {
        "DimensionConfigurations": [
            {
                "DimensionName": "ses:configuration-set",
                "DimensionValueSource": "MESSAGE_TAG",
                "DefaultDimensionValue": "TechNewsConfigSet"
            }
        ]
    }
}'

创建CloudWatch Dashboard

创建一个Cloudwatch Dashboard:

image-20231006102617248

命名为SESDashboard:

image-20231006102637516

创建完成后,添加以下四个组件,来分别展示["send", "delivery", "bounce", "complaint"] 事件的数量:

aws cloudwatch put-dashboard --dashboard-name SESDashboard --dashboard-body --region us-east-1 '{
    "widgets": [
        {
            "type": "metric",
            "x": 0,
            "y": 0,
            "width": 24,
            "height": 6,
            "properties": {
                "metrics": [
                    ["AWS/SES", "Send", "ses:configuration-set", "TechNewsConfigSet", {"label": "Sent", "stat": "Sum"}],
                    ["AWS/SES", "Delivery", "ses:configuration-set", "TechNewsConfigSet", {"label": "Delivered", "stat": "Sum"}],
                    ["AWS/SES", "Bounce", "ses:configuration-set", "TechNewsConfigSet", {"label": "Bounced", "stat": "Sum"}],
                    ["AWS/SES", "Complaint", "ses:configuration-set", "TechNewsConfigSet", {"label": "Complaints", "stat": "Sum"}]
                ],
                "view": "singleValue",
                "region": "us-east-1",
                "title": "SES Metrics",
                "period": 86400
            }
        }
    ]
}'

这个dashboard分别展示过去24小时内四种事件的总数,让运营人员更直观的对SES邮件的表现有了解:

image-20231006102910424

创建CloudWatch alarm

为了监控bounce ratecomplaint rate,SES 已经在账户级别内置了声誉跟踪指标:

image-20231006103241295

但是我们还是希望能够跟踪单个业务部门级别的声誉指标,此时可借助Configuration Set。

在Configration set中开启Reputation options后,就能追踪BounceRateComplaintRate这两个指标:

image-20231006104036902

根据这两个指标可以用于设置Cloudwatch Alarm报警,例如当bounce rate在过去24h内大于5%时发送报警:

aws cloudwatch put-metric-alarm --alarm-name BounceAlarm \
--alarm-description "Alarm triggered when bounce percentage is above 5% of total emails sent" \
--metric-name Reputation.BounceRate \
--namespace "AWS/SES" \
--statistic Sum \
--evaluation-periods 1 \
--period 86400 \
--threshold 0.05 \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions Name=ses:configuration-set,Value=TechNewsConfigSet \
--evaluation-periods 1 \
--alarm-actions <sns-topic-arn>