将现有SNS主题分配给Terraform中的警报

问题描述 投票:1回答:1

我有一个现有的SNS主题,不是我想要引用的Terraform管理,所以我可以在aws_cloudwatch_metric_alarm.alarm_actions列表中分配它。在尝试运行计划或申请时,我收到以下消息:

变量aws_sns_topic.my-alerts.arn中引用的未知资源'aws_sns_topic.my-alerts'

从消息判断我猜我必须以某种方式创建一个SNS资源,但我不确定如何使用现有主题附加/引用它。

我正在引用数据块中的现有主题:

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}"]
}
amazon-web-services terraform amazon-sns
1个回答
0
投票

引用数据源时,需要在其前面添加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}"]
}
© www.soinside.com 2019 - 2024. All rights reserved.