Terraform:如何有条件地将 EBS 卷分配给 ECS 集群 [英] Terraform: How to conditionally assign an EBS volume to an ECS Cluster

查看:15
本文介绍了Terraform:如何有条件地将 EBS 卷分配给 ECS 集群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义 ECS 集群的 ecs_cluster 模块.我希望模块可以重复使用,这样我就可以创建具有不同配置的各种集群.因此,我希望能够选择性地指定是否在 ECS 主机的启动配置中创建和附加 EBS 卷.

I have an ecs_cluster module which defines an ECS cluster. I want the module to be re-usable so I can create various clusters with different configurations. Hence I want to be able to optionally specify whether to create and attach an EBS volume in the launch configuration of the ECS hosts.

我最初尝试在启动配置中的 ebs_block_device 中使用 count,例如

I initially tried using count in the ebs_block_device inside the launch configuration e.g.

variable "ebs_volume_device_name" { type = "string", default = "" }
variable "ebs_volume_type" { type = "string", default = "" }
variable "ebs_volume_size" { type = "string", default = "" }

resource "aws_launch_configuration" "launch_configuration" {
  name_prefix = "foo"
  image_id = "bar"
  # Irrelevant stuff removed for brevity...

  ebs_block_device {
    count = "${length(var.ebs_volume_device_name) > 0 ? 1 : 0}"
    device_name = "${var.ebs_volume_device_name }"
    volume_type = "${var.ebs_volume_type}"
    volume_size = "${var.ebs_volume_size}"
  }
} 

但是这会导致以下错误:

However this results in the following error:

module.ecs_cluster.aws_launch_configuration.launch_configuration: ebs_block_device.0: invalid or unknown key: count

然后我尝试指定 launch_configuration 资源两次,一次使用 ebs 块设备,一次不使用 ebs 块设备,例如

I then tried specifying the launch_configuration resource twice, once with and once without the ebs block device e.g.

variable "ebs_volume_device_name" { type = "string", default = "" }
variable "ebs_volume_type" { type = "string", default = "" }
variable "ebs_volume_size" { type = "string", default = "" }

resource "aws_launch_configuration" "launch_configuration" {
  count = "${length(var.ebs_volume_device_name) == 0 ? 1 : 0}"
  name_prefix = "foo"
  image_id = "bar"
  # Irrelevant stuff removed for brevity...

  # No specification of ebs_block_device
}

resource "aws_launch_configuration" "launch_configuration" {
  count = "${length(var.ebs_volume_device_name) > 0 ? 1 : 0}"
  name_prefix = "foo"
  image_id = "bar"
  # Irrelevant stuff removed for brevity...

  ebs_block_device {

    device_name = "${var.ebs_volume_device_name }"
    volume_type = "${var.ebs_volume_type}"
    volume_size = "${var.ebs_volume_size}"
  }
}

但是 Terraform 然后抱怨,因为资源被定义了两次.

However Terraform then complains because the resource is defined twice.

我无法更改任何一个资源的 ID,因为我有一个自动缩放组,该组取决于启动配置的名称,例如

I can't change the id of either of the resources as I have an auto scaling group which depends upon the name of the launch configuration e.g.

resource "aws_autoscaling_group" "autoscaling_group" {
  name = "foo"
  launch_configuration = "${aws_launch_configuration.launch_configuration.name}"
}

我想我可以有条件地定义 2 个自动缩放组并将一个映射到每个启动配置,但这感觉真的很混乱.此外,这些资源本身也有依赖资源,例如 cloudwatch 指标警报等.在 2 个不同的条件下重复所有这些代码两次感觉非常难.我是不是漏了个小把戏?

I guess I could conditionally define 2 autoscaling groups and map one to each launch configuration but this feels really messy. Also these resources themselves have dependent resources such as cloudwatch metric alarms etc. It feels very unDRY to repeat all of this code twice with 2 separate conditions. Am I missing a trick here?

感谢任何相关的 Terraform 智慧!

Grateful for any relevant Terraform wisdom!

推荐答案

不幸的是,count 元属性仅适用于资源级别.在资源中包含条件块(例如您的 ebs_block_device 或例如日志记录等)是 github 的 terraform 问题中经常提到的问题,据我所知,目前还没有解决方案.

The count meta-attribute works only on resource-level, unfortunately. Having a conditional block within a resource (such as your ebs_block_device or for example logging or etc) is a problem commonly mentioned in terraform issues in github and as far as I can tell there isn't a solution yet.

在您的情况下,技巧"可能是让您的 autoscaling_group.launch_configuration 属性也有一个三元运算符,即

In your case a 'trick' could be to have your autoscaling_group.launch_configuration property also have a ternary operator, i.e.

resource "aws_autoscaling_group" "autoscaling_group" {
  name = "foo"
  launch_configuration = "${length(var.ebs_volume_device_name) == 0 ? aws_launch_configuration.launch_configuration.name : aws_launch_configuration.launch_configuration2.name}"
}

或者更好的是在带有输出名称的launch_configuration模块中提取该逻辑,然后上面的内容看起来像

Or better yet extract that logic in a launch_configuration module with an output name and then the above can look like

resource "aws_autoscaling_group" "autoscaling_group" {
      name = "foo"
      launch_configuration = "${module.launch_config.name}"
}

不是说它不丑,而是 terraform 对你的条件.

Not saying it isn't ugly but that's terraform's conditionals for you.

这篇关于Terraform:如何有条件地将 EBS 卷分配给 ECS 集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆