将现有 SNS 主题分配给 Terraform 中的警报
Assign existing SNS Topic to Alarm in Terraform
我有一个未使用 Terraform 管理的现有 SNS 主题,我想引用它,因此我可以在 aws_cloudwatch_metric_alarm.alarm_actions
列表中分配它。在尝试 运行 计划或申请时,我收到以下消息:
unknown resource 'aws_sns_topic.my-alerts' referenced in variable aws_sns_topic.my-alerts.arn
根据消息判断,我猜我必须以某种方式创建 SNS 资源,但我不确定如何 attach/reference 它与现有主题。
我正在引用数据块中的现有主题:
data "aws_sns_topic" "my-alerts" {
name = "my-alerts"
}
然后尝试稍后在警报中使用:
resource "aws_cloudwatch_metric_alarm" "app-health-alarm" {
alarm_name = "app-health-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "ApplicationComponetHealthRequestFailing"
namespace = "ApplicationComponetHealth"
period = "300"
statistic = "Average"
dimensions = {
component="my-app"
}
threshold = "1"
alarm_description = "Checks the health of the app"
datapoints_to_alarm = "2"
alarm_actions = ["${aws_sns_topic.my-alerts.arn}"]
}
引用数据源时需要在其前面加上data.
前缀
所以在你的情况下应该是:
resource "aws_cloudwatch_metric_alarm" "app-health-alarm" {
alarm_name = "app-health-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "ApplicationComponetHealthRequestFailing"
namespace = "ApplicationComponetHealth"
period = "300"
statistic = "Average"
dimensions = {
component="my-app"
}
threshold = "1"
alarm_description = "Checks the health of the app"
datapoints_to_alarm = "2"
alarm_actions = ["${data.aws_sns_topic.my-alerts.arn}"]
}
我有一个未使用 Terraform 管理的现有 SNS 主题,我想引用它,因此我可以在 aws_cloudwatch_metric_alarm.alarm_actions
列表中分配它。在尝试 运行 计划或申请时,我收到以下消息:
unknown resource 'aws_sns_topic.my-alerts' referenced in variable aws_sns_topic.my-alerts.arn
根据消息判断,我猜我必须以某种方式创建 SNS 资源,但我不确定如何 attach/reference 它与现有主题。
我正在引用数据块中的现有主题:
data "aws_sns_topic" "my-alerts" {
name = "my-alerts"
}
然后尝试稍后在警报中使用:
resource "aws_cloudwatch_metric_alarm" "app-health-alarm" {
alarm_name = "app-health-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "ApplicationComponetHealthRequestFailing"
namespace = "ApplicationComponetHealth"
period = "300"
statistic = "Average"
dimensions = {
component="my-app"
}
threshold = "1"
alarm_description = "Checks the health of the app"
datapoints_to_alarm = "2"
alarm_actions = ["${aws_sns_topic.my-alerts.arn}"]
}
引用数据源时需要在其前面加上data.
所以在你的情况下应该是:
resource "aws_cloudwatch_metric_alarm" "app-health-alarm" {
alarm_name = "app-health-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "ApplicationComponetHealthRequestFailing"
namespace = "ApplicationComponetHealth"
period = "300"
statistic = "Average"
dimensions = {
component="my-app"
}
threshold = "1"
alarm_description = "Checks the health of the app"
datapoints_to_alarm = "2"
alarm_actions = ["${data.aws_sns_topic.my-alerts.arn}"]
}