【问题标题】:Terraform With Resource Not Found未找到资源的 Terraform
【发布时间】:2019-01-09 10:11:50
【问题描述】:

我正在尝试使用 terraform 创建 Auto Scaling 组的 cloudwatch 警报。我已将 terraform 模块与 github 源一起用于 cloudwatch 警报。

我的自动伸缩组配置代码是:

   resource "aws_launch_configuration" "main" {
     name_prefix          = "${format("%s-%s-postgres-", var.name, var.environment)}"
     instance_type        = "${var.instance_type}"
     image_id             = "${var.ami}"
     key_name             = "${var.key_name}"
     iam_instance_profile = "${aws_iam_instance_profile.server.id}"
     security_groups      = ["${var.security_groups}", "${aws_security_group.main.id}"]
     user_data            = "${data.template_file.main.rendered}"

  root_block_device {
    volume_size           = "${var.root_volume_size}"
    volume_type           = "gp2"
    delete_on_termination = true
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "main" {
  count                     = "${var.size}"
  name                      = "${var.name}-${var.environment}-postgres-asg-${count.index}"
  launch_configuration      = "${aws_launch_configuration.main.name}"
  max_size                  = "${var.max_size}"
  min_size                  = "${var.min_size}"
  desired_capacity          = "${var.desired_capacity}"
  health_check_type         = "EC2"
  health_check_grace_period = 600
  force_delete              = false
  load_balancers            = ["${aws_elb.main.name}"]
  vpc_zone_identifier       = ["${element(var.subnet_ids, count.index % length(var.subnet_ids))}"]
  termination_policies      = ["OldestLaunchConfiguration", "Default"]
  depends_on                = ["aws_launch_configuration.main"]

  tags = ["${list(
    map(
      "key", "Name",
      "value", "${var.name}-${var.environment}-postgres-${count.index}",
      "propagate_at_launch", true
    ),
    map(
      "key", "role",
      "value", "postgres",
      "propagate_at_launch", true
    ),
    map(
      "key", "LaunchConfigName",
      "value", "${aws_launch_configuration.main.name}",
      "propagate_at_launch", true
    ),
    map(
      "key", "ServerGroupIndex",
      "value", count.index,
      "propagate_at_launch", true
    ),
    map(
      "key", "Function",
      "value", "${var.name}",
      "propagate_at_launch", true
    ),
    map(
      "key", "Stage",
      "value", "${var.environment}",
      "propagate_at_launch", true
    )
  )}"]

  lifecycle {
    create_before_destroy = true
  }
}

cloudwatch 警报的 Terraform 代码是:

module "alarm-asg-cpu" {
  source = "git::git@github.com:gruntwork-io/module-aws-monitoring.git//modules/alarms/asg-cpu-alarms?ref=v0.9.1"
  alarm_sns_topic_arns                    = ["${var.ebs_backup_sns_topic}"]
  asg_names                               = ["${aws_autoscaling_group.main.name}"]
  num_asg_names                           ="1"
  high_cpu_utilization_threshold          = "10"
  high_cpu_utilization_period             = "60"
  high_cpu_utilization_evaluation_periods = "1"
  high_cpu_utilization_statistic          = "Average"
}

module "alarm-asg-disk" {
  source = "git::git@github.com:gruntwork-io/module-aws-monitoring.git//modules/alarms/asg-disk-alarms?ref=v0.9.1"
  alarm_sns_topic_arns                     = ["${var.ebs_backup_sns_topic}"]
  asg_names                                = ["${aws_autoscaling_group.main.name}"]
  num_asg_names                            ="1"
  file_system                              = "/dev/xvdh"
  mount_path                               = "/var/lib/pgsql"
  high_disk_utilization_threshold          = "10"
  high_disk_utilization_period             = "60"
  high_disk_utilization_evaluation_periods = "1"
  high_disk_utilization_statistic          = "Maximum" 
}

当我运行 terraform plan 命令时。它显示以下错误:

我陷入了这个问题。我该如何解决这个问题?

【问题讨论】:

  • 所有代码都在同一个目录下?似乎第二个文件被父模块postgres调用。
  • 不,所有这些代码都位于同一个名为“main.tf”的文件中
  • @QuentinRevel 我已经将 github 源代码与 cloudwatch 警报模块一起使用?这是github仓库的问题吗?
  • 我不这么认为,我不明白为什么它不起作用
  • 它抱怨的 Postgres 模块在哪里?

标签: amazon-web-services terraform autoscaling terraform-provider-aws


【解决方案1】:

所以你在资源自动缩放中设置计数

resource "aws_autoscaling_group" "main" {
  count                     = "${var.size}"
  ...

资源将按以下格式创建,具体取决于您设置的大小计数。

aws_autoscaling_group.main[0]
aws_autoscaling_group.main[1]
aws_autoscaling_group.main[2]
...

然后,当您从 module "alarm-asg-cpu" 引用时,您还需要设置计数。

这就是找不到资源aws_autoscaling_group.main的原因

但是在 terraform 中有一个未解决的问题,您不能在模块中设置 count

https://github.com/hashicorp/terraform/issues/953

所以你需要想其他方法来解决它,例如,不要使用count创建资源aws_autoscaling_group,逐个创建它们

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2019-04-06
    • 2019-04-30
    • 2013-06-22
    • 1970-01-01
    相关资源
    最近更新 更多