Terraform 12 data aws_ami complex name_regex

68 views Asked by At

I'm trying to do a complex regex for filtering amis when using a data.tf, this is the code I have

data "aws_ami" "main" {
  most_recent = true
  name_regex  = var.ami_regex
  owners      = var.owners
}

Then I'm trying to send a proper regex inside the var.ami_regex without success, I have the following naming convention for my amis.

1 ) centos-7-java-testing-DATE

2 ) centos-7-java-pci-testing-DATE

And as the *2 is created after the 1, if I want to match the 1 inside my code, It will match the 2, because of the date, So my idea is to add a proper regex to exclude the one that includes the "pci" word on the name, I tried the following regex ^centos-7-java-(?!.*pci).* but no luck, looks like terraform doesn't like that kind of regex.

The error: Error: "name_regex": error parsing regexp: invalid or unsupported Perl syntax: `(?!

1

There are 1 answers

1
Salathiel Genese On

I would think the best practice is to declare both AMI as resources and either:

  • terraform import them or,
  • use the import block

Depending on your Terraform version.

Alternatively, have you considered using a ternary operator?

data "aws_ami" "main" {
  most_recent = true
  # name_regex  = var.ami_regex
  name        = var.condition ? "testing-name" : "pci-name"
  owners      = var.owners
}