【发布时间】:2021-11-16 03:17:37
【问题描述】:
所以首先让我试着给你一个我的项目结构的概述:
我在我的模块“监控”中设置了“Cloudwatch 警报”,一切正常,但因为我使用 CloudWatch 代理的指标,我必须声明我想要监控的实例的“image_id”。
这个“image_id”的 ami 位于我的模块“gitlab”中。我通过这样的数据资源检索了 ami 对象:
data "aws_ami" "ami_img" {
most_recent = true
owners = ["326578563245"]
}
}
在此之后,我将它放在模块 gitlab 的输出中,如下所示:
output "ami" {
value = data.aws_ami.ami_img
}
现在我将它放在父级的 main.tf 中,如下所示:
module "monitoring" {
ami_img_id = module.monitoring.ami.id
}
之后,我在“监控”模块的 variables.tf 中初始化变量,如下所示:
variable "ami_img_id" {}
所以现在我应该能够在我的模块“监控”的 main.tf 中使用它的属性,如下所示:
#Alarm for Drive: 90% Maximum 1/1 Datapoint Period 30 Minutes
resource "aws_cloudwatch_metric_alarm" "disk_used" {
alarm_name = "${var.env_prefix}|Gitlab|Disk used > 90%!"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = "disk_used_percent"
namespace = "CWAgent"
period = "1800"
statistic = "Maximum"
threshold = "90"
datapoints_to_alarm = "1"
alarm_description = "This alarm is triggered if 1/1 Datapoint, in 30 Minutes, is over 90%."
#alarm_actions = aws_sns_topic.topic_alarms.arn
alarm_actions = ["${data.aws_sns_topic.topic_alarms_data.arn}"]
dimensions = {
InstanceId = data.aws_instance.gitlab_data.instance_id
ImageId = var.ami_img_id
InstanceType = data.aws_instance.gitlab_data.instance_type
device = "xvda1"
fstype = "xfs"
path = "/"
}
insufficient_data_actions = []
}
当我尝试时
terraform apply
我得到这个输出:
Error: Unsupported attribute
│
│ on main.tf line 68, in module "monitoring":
│ 68: ami_img_id = module.monitoring.ami.id
│ ├────────────────
│ │ module.monitoring is a object, known only after apply
│
│ This object does not have an attribute named "ami".
我已经成功应用了这个模块,现在我有 3 个不收集数据的警报,因为 CloudWatch 代理需要我猜的最后一个数据。 所以这个错误消息对我没有任何意义,因为我已经部署了所有东西,只是我最新的应用程序,在我实现了 ami img id 之后#t 工作。 我可以使用模块中的数据实现另一个资源,我需要它来摆脱这个,但是当我已经检索到这些信息时,我认为这不是一个好方法。抱歉,我没能把这个问题缩短一点。
【问题讨论】:
标签: terraform terraform-provider-aws