【问题标题】:Terraform resource inheritanceTerraform 资源继承
【发布时间】:2020-02-14 22:31:11
【问题描述】:

有没有办法声明要继承的抽象资源?

例子:

resource "digitalocean_droplet" "worker_abstract" {
  abstract = true // ???

  name = "swarm-worker-${count.index}"
  tags = [
    "${digitalocean_tag.swarm_worker.id}"
  ]

  // other config stuff

  provisioner "remote-exec" {
    //...
  }
}

然后将声明的资源与覆盖的变量一起使用:

resource "worker_abstract" "worker_foo" {
  count = 2
  name = "swarm-worker-foo-${count.index}"
  tags = [
    "${digitalocean_tag.swarm_worker.id}",
    "${digitalocean_tag.foo.id}"
  ]
}

resource "worker_abstract" "worker_bar" {
  count = 5
  name = "swarm-worker-bar-${count.index}"
  tags = [
    "${digitalocean_tag.swarm_worker.id}"
    "${digitalocean_tag.bar.id}"
  ]
}

【问题讨论】:

标签: terraform


【解决方案1】:

这可能比您建议的解决方案更“冗长”,但这听起来像是 modules in 0.12 的完美用例。

你可以创建一个模块,比如在文件worker/main.tf

variable "instances" {
  type = number
}

variable "name" {
  type = string
}

variable "tags" {
  type    = list(string)
  default = []
}

resource "digitalocean_droplet" "worker" {
  count = var.instances

  name = "swarm-worker-${var.name}-${count.index}"
  tags = var.tags

  // other config stuff

  provisioner "remote-exec" {
    //...
  }
}

然后你可以像你的例子一样使用你的模块(假设从上面的目录worker)

module "worker_foo" {
  source = "./worker"

  instances = 2
  name = "foo"
  tags = [
    "${digitalocean_tag.swarm_worker.id}",
    "${digitalocean_tag.foo.id}"
  ]
}

module "worker_bar" {
  source = "./worker"

  instances = 5
  name = "bar"
  tags = [
    "${digitalocean_tag.swarm_worker.id}"
    "${digitalocean_tag.bar.id}"
  ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 2015-11-28
    • 1970-01-01
    相关资源
    最近更新 更多