In file1.tf
(generated by kops) I have a resource like this:
resource "aws_vpc" "my-vpc-tf-id" {
...
}
The resource ID was dynamically generated by kops and also added to terraform.tfvars
(so it can be used in other places in the .tf
files):
my_var = "my-vpc-tf-id"
Now I would like to reference the VPC resource from file2.tf
without hardcoding its name:
resource "aws_security_group" "db" {
...
vpc_id = "${aws_vpc.${var.my_var}.id}"
...
}
but Terraform complains that the ${var.my_var}
is not allowed. So instead I define this in file2.tf
:
resource "aws_security_group" "db" {
...
vpc_id = "${aws_vpc.{{MY_VAR_VAL}}.id}"
...
}
and I use sed
to replace the placeholder with the value. This works well but complicates certain other tasks so I was wondering if there were other ways of achieving this without using sed
or hardcoding the my_var
value (just Terraform's HCL).