Reputation: 131
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!
Upvotes: 1
Views: 1960
Reputation: 238081
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"])
}
}
Upvotes: 2