1

I stuck at old code written by another developer 2 years before, that I can't pass the terraform validate. I tried validate with old terrafrom version as well, 0.11, 0.13, 0.15 and 1.0

variable "aws_name" {
    default = "${aws:name}"
}

I am confused by the sytax, that looks the author try to reference variable from another variable in terraform, but I don't think terraform supports this feature, from beginning.

I mean no support by this way from old versions, such as terrafrom 0.6, to current version 1.0.x

If the code use ${var.xxxx}, I think it was the code created before terraform 0.12, because after that, we don't need "${ }" to reference a variable, we can directly reference it via var.aws_name

Second we can't reference a variable as "aws:name" without "var" in front of it, and colon will misleading in terraform as well.

Any one knew this way in terraform, is it validated in some of terrafrom versions?

Update

As @Matt Schuchard mentioned, the azure pipeline task replacetokens@4 does support other style for the replacement (the fourth)

enter image description here

Bill
  • 2,494
  • 5
  • 26
  • 61

1 Answers1

3

try to reference variable from another variable in terraform, but I don't think terraform supports this feature

That's correct. You can't do this. The only reason for that I can think of is that the terraform was part of some CI/CD pipeline. Thus, maybe before the actual TF scripts were run, they were per-processed by an external tool, which made simple find-and-replace of ${aws:name} string to valid values.

One possibility could be Before Hooks in terragrunt.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 1
    oh, you are smart, let me check the pipeline, if someone run the sed or awk to replace it. that would be the only reason. – Bill Nov 04 '21 at 03:40
  • @Bill No problem. Let me know how it went. There can be many ways this could be done. `sed` or `awk` are only two some possibilities of many available. – Marcin Nov 04 '21 at 05:08
  • 1
    This scenario looks really similar to Packer HCL2 variable declarations that someone else also inherited asked here https://stackoverflow.com/questions/68955310/packer-variable-default-syntax/68956175. The reasoning there was also the same as here, so this is almost certainly correct. – Matthew Schuchard Nov 04 '21 at 11:04
  • Another important note is that one should be supplying inputs to the variable values to override the defaults, and not doing file manipulation on default values. The former is significantly easier and more stable than the latter. – Matthew Schuchard Nov 04 '21 at 11:07
  • Thanks @MattSchuchard another guess is correct, azure pipeline. But the format is not `#{aws:name}#`. Do you mean that azure pipeline task can custimize the replace style to `${aws:name}` as well? anyway I dont see that task in the pipeline file – Bill Nov 06 '21 at 23:27
  • @Bill If this is Packer, not Terraform, its better to re-tag your question as such as wall as change it text as it refers to terraform only. Thus, my and other possible answers will refer to terraform, not packer. – Marcin Nov 06 '21 at 23:39
  • no, it is terraform, not packer. this project is deployed by azure pipeline – Bill Nov 07 '21 at 01:26
  • @Bill Then as I explained TF does not support such variables. Thus I'm not sure what it has to do with `Packer HCL2`? – Marcin Nov 07 '21 at 05:17