代码示例:下面的例子演示如何使用CloudWatch API创建告警,监视EKS集群的CPU使用率是否超过阈值。如果超过,将向SNS主题发送通知。
import boto3
cloudwatch = boto3.client('cloudwatch')
sns = boto3.client('sns')
response = cloudwatch.put_metric_alarm(
    AlarmName='EKS-CPU-Usage',
    MetricName='CPUUsage',
    Namespace='AWS/EKS',
    Statistic='Average',
    Period=300,
    EvaluationPeriods=1,
    Threshold=70.0,
    ComparisonOperator='GreaterThanThreshold',
    AlarmActions=[
        sns.create_topic(Name='EKS-CPU-Usage-Alert')['TopicArn'],
    ]
)
print(response)