【问题标题】:select specific patterns from string to create resources - terraform从字符串中选择特定模式来创建资源 - terraform
【发布时间】:2021-10-13 22:59:38
【问题描述】:

我想在 AWS 上使用来自 terraform 变量的模式创建资源。

例如:

proyects = ["proyect1/X", "proyect1/Y", "proyect2/X", ...]

resource "aws_example" "proyect1" {
count = length(var.proyects) <-- only using the proyect1/... ones
name = var.proyects[count.index] <-- only using the proyect1/... ones

已创建资源 proyect1/X

资源 proyect1/Y 已创建

resource "aws_example" "proyect2" {
count = length(var.proyects) <-- only using the proyect2/... ones
name = var.proyects[count.index] <-- only using the proyect2/... ones

资源 proyect2/X 已创建

资源 proyect2/Y 已创建


我不知道这是否可能...我正在尝试使用https://www.terraform.io/docs/language/functions/index.html

【问题讨论】:

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


    【解决方案1】:

    您可以通过proyect1proyect2 过滤并使用for_each

    variable "proyects" {
      default = ["proyect1/X", "proyect1/Y", "proyect2/X", "proyect2/Y", "proyect2/Z"]
    }
    
    
    resource "aws_example" "proyect1" {
      for_each = [for v in var.proyects : v if length(regexall("proyect1.*", v)) > 0]
      name = each.key
    }
    
    resource "aws_example" "proyect2" {
      for_each = [for v in var.proyects : v if length(regexall("proyect2.*", v)) > 0]
      name = each.key
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-08
      • 2021-05-15
      • 1970-01-01
      • 2022-09-22
      • 2021-03-13
      • 1970-01-01
      • 2017-06-06
      • 2021-11-06
      相关资源
      最近更新 更多