简体   繁体   中英

Regex in a list in terraform

I am trying to regex a specific variable in a list, specifically in the tags of an aws_instance in terraform. It looks like this:

variable "string"  { default = "Foo" }
variable "id"  { default = "https://s3-us-east-1.amazonaws.com/bucket/folder/item-v1.2/item-item2-v1.2.gz" }
variable "id2" { default = "456" }

resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  tags {
    Name = "${join(".", compact(list(var.string, var.id, var.id2, "lorem-ipsum")))}"
  }
}

Would I be able to apply a regex on variable.id in that list, specifically

${replace(variable.id, "/.*-(.*)/.*/", "$1")}

So the output would be something like

tags = foo, v1.2, 456, lorem ipsum

The regex already works, I just have no idea how to put it in a list like that. How would I go about in doing it? Thank you!

Since you are using TF 0.14.4, you can do the following:

resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  tags {
    Name = join(".", compact(list(
                var.string,
                replace(var.id, "/.*-(.*)/.*/", "$1"),
                var.id2,
                "LOREM-ipsum")))
  }
}

The above gives:

Foo.v1.2.456.LOREM-ipsum

Or simpler:

resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  tags {
    Name = join(".", [
                var.string,
                replace(var.id, "/.*-(.*)/.*/", "$1"),
                var.id2,
                "LOREM-ipsum"])
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM