We have our terraform projects structured like so:...
# help
c
We have our terraform projects structured like so:
Copy code
dev/
  <http://moduleA.tf|moduleA.tf>
  <http://moduleB.tf|moduleB.tf>
prod/
  <http://moduleA.tf|moduleA.tf>
  <http://moduleC.tf|moduleC.tf>
Is there an easy way to make the config file treat each individual module as a project, so we can get cost estimates per module? So the projects would be like
dev-moduleA
,
dev-moduleB
etc? Or is there another/better way to get a cost breakdown per module inside of a project?
b
Hello! You can try using the config template: https://www.infracost.io/docs/features/config_file/#template-syntax
c
I tried using this range loop based off the documentation, but it seems like it's not parsing correctly
Copy code
version: 0.1

projects:
{{- range $project := matchPaths ":env/:<http://module.tf|module.tf>" }}
  - path: .
    name: {{ $project.module }}-{{ $project.env }}
{{- end }}
Resulting in
Infracost generate config failed.
/usr/local/bin/infracost command was closed with a non 0 exit code: Error: could not parse template: "version: 0.1\n\nprojects:\n{{- range $project := matchPaths env/module.tf }}\n - path: .\n name: {{ $project.module }}-{{ $project.env }}\n{{- end }}" err: template: infracost.yml.tmpl4 expected :=
b
Can you try this?
Copy code
version: 0.1
projects:
{{- range $project := matchPaths ":env/:file" }}
  - path: {{ $project._path }}
    name: {{ $project.env }}-{{ stem $project.file }}
{{- end }}
It produces the following config file:
Copy code
version: 0.1
projects:
  - path: dev/moduleA.tf
    name: dev-moduleA
  - path: dev/moduleB.tf
    name: dev-moduleB
  - path: prod/moduleA.tf
    name: prod-moduleA
  - path: prod/moduleC.tf
    name: prod-moduleC
c
That does give the desired list, however it seems that all the estimate values are $0 and there is no breakdown. So I guess that the projects list expects the path to be a directory rather than a singular file?
b
Yep, sorry, I forgot that
--path
requires a directory.
I don't think it's currently possible
Hm, I wonder if we can hack the template so it loops over files, but excludes individual files
c
It would be an interesting experiment 😄
b
So we could specify the directory
dev/
but keep only one file in each project
Nope, I can't crack it. Will ask the team tomorrow if it's possible at all right now 🙂
c
Thanks, appreciate the help and efforts so far!
b
Hey, so I've asked our main config guy about this. Unfortunately, CLI doesn't support that kind of thing yet, sorry.