Hi team. I'm after some help creating a config fil...
# help
h
Hi team. I'm after some help creating a config file to support a structure similar to below. These are the commands I'd use to run the config: terraform apply -var-file="config/global.tfvars" -var-file="config/azeun/dev/dev.tfvars" terraform apply -var-file="config/global.tfvars" -var-file="config/azeun/sbox/sbox.tfvars" Main.tf references remote modules e.g git::https://rhodri.visualstudio.com/demo1/_git/log-analytics?ref=1.0.0 ├── config │ ├── global.tfvars │ ├── azeun │ │ └── dev │ │ └── dev.tfvars │ │ └── sbox │ │ └── sbox.tfvars │ └── azeuw │ └── dev │ └── dev.tfvars ├── infracost.yml.tmpl └── main.tf
b
Hey Rhodri! Is it possible to share the TF code?
h
Hi @busy-agent-35515. Thanks for getting back to me. I created the template manually in the end, but would be good to understand what I was doing wrong. This is the repo https://dev.azure.com/rhod3rz01/Temp/_git/infracost
b
Thanks for the link! What's the final template you got? I can create my own and then we'll compare 🙂
h
Morning, this is the one I created manually ... https://dev.azure.com/rhod3rz01/Temp/_git/infracost?path=/infracost.yml
In terms of infracost.yml.tmpl, I didn't get this to work at all.
Hi @busy-agent-35515 ... any more thoughts on this please?
b
Hey! Sorry, I saw your message yesterday but haven't had time to tinker with the repo myself. I'll ping you when I have something
Copy code
version: 0.1
projects:
{{- range $project := matchPaths "config/:app/:env" }}
  - path: .
    name: {{ $<http://project.app|project.app> }}-{{ $project.env }}
    terraform_var_files:
      - config/{{ $<http://project.app|project.app> }}/{{ $project.env }}/{{ $project.env }}.tfvars

{{- end }}
Can you try this?
Noticed a typo in your config file on line 19 - for project
euw-dev
the path should be
config/euw...
h
Thanks @busy-agent-35515, but it's not working. I've used your .tmpl file with this command: infracost generate config --repo-path=. --template-path=infracost.yml.tmpl --out-file=infracost.yml but it just creates an empty file (with the exception of a couple of lines header). All here if you want to look. https://dev.azure.com/rhod3rz01/Temp/_git/infracost
b
@hallowed-architect-17276 Apologies, I again missed your message 😞 That's strange, I just ran the same command and it generated it fine. Here's the full listing of what I've done:
Copy code
❯ ls
config             infracost.yml.tmpl <http://main.tf|main.tf>
❯ cat infracost.yml.tmpl
version: 0.1
projects:
{{- range $project := matchPaths "config/:app/:env" }}
  - path: .
    name: {{ $<http://project.app|project.app> }}-{{ $project.env }}
    terraform_var_files:
      - config/{{ $<http://project.app|project.app> }}/{{ $project.env }}/{{ $project.env }}.tfvars

{{- end }}
❯ infracost generate config --repo-path=. --template-path=infracost.yml.tmpl --out-file=infracost.yml
❯ cat infracost.yml
version: 0.1
projects:
  - path: .
    name: eun-dev
    terraform_var_files:
      - config/eun/dev/dev.tfvars
  - path: .
    name: eun-prd
    terraform_var_files:
      - config/eun/prd/prd.tfvars
  - path: .
    name: euw-dev
    terraform_var_files:
      - config/euw/dev/dev.tfvars
  - path: .
    name: euw-prd
    terraform_var_files:
      - config/euw/prd/prd.tfvars
h
@busy-agent-35515 thanks for your help with this. I was running via windows/powershell and it just spits out an empty file. I saw you were using bash so ran via WSL, installed infracost and it worked fine 🙂