下面是我用 Terraform 制作的 AWS Cloudwatch 警报语法:
resource "aws_cloudwatch_metric_alarm" "service_start_alarm" {
alarm_name = "${var.module_name}-start"
insufficient_data_actions = []
ok_actions = []
alarm_actions = [var.trigger_sns.arn]
treat_missing_data = "notBreaching"
metric_name = "Requests"
namespace = "AWS/CloudFront"
statistic = "Sum"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 1
datapoints_to_alarm = 1
threshold = 0
period = 60
dimensions = {
DistributionId = var.distribution_id
Region = "Global"
}
}
我想在Cloudfront发行版收到任何请求时发出警报并触发一些执行逻辑(用例是后端系统关闭一段时间,因此第一个请求将触发它们)
但是,当我在浏览器中输入 Cloudfront 的 URL 时,Cloudfront 收到 1 个请求,但没有发出警报。
我解决了有关错误代码率的类似问题。我想您的问题是由于在 us-east-1 以外的区域创建的警报引起的。由于 CloudFront 是一项全球服务,因此应在 us-east-1 中创建 CloudFront 的警报。如果您的问题确实如此,我建议使用
region="us-east-1"
定义替代 AWS 提供商,并在您的警报资源定义中显式传递它。
provider "aws" {
alias = "useast1"
region = "us-east-1"
}
然后
resource "aws_cloudwatch_metric_alarm" "alarm" {
provider = aws.useast1
#other alarm properties here
}
如果指标表示请求数,则阈值 0 可能不合适。根据您的预期流量设置更实际的阈值。我会尝试100
resource "aws_cloudwatch_metric_alarm" "service_start_alarm" {
alarm_name = "${var.module_name}-start"
....................
threshold = 100 *# Adjust this threshold based on expected traffic*
...........
}
}
此外,请确保指标“Requests”在“AWS/CloudFront”命名空间中正确命名。