Hello! We have a Terraform mono-repo style terrafo...
# help
d
Hello! We have a Terraform mono-repo style terraform project for Azure infrastructure. Each module for a separate Subscription which contains their own modules, some reusable, most not. There are dependencies between each subscription module. We want to get costs for each subscription and in total. We can get it for all resources but if we try to get the cost for a subscription module it will complain about variables not being defined. The problem is that these variables it is complaining about are output from resources in other Subscription modules, these are not values that we can specify manually, at least not easily. Is there a way for us to configure an infracost.yaml config file to look in other modules just for the variable values? Or am I going about this all wrong? The main aim of this is to baseline the cost for each subscription so that we can use this information to configure budgets in Azure.
1
s
Generally I think this should be possible. I would roughly expect that each module/subscription subdivision you have should be considered its own project. Then we'd be able to show a cost per project (and then an overall cost). Usually in those cases the dependency resolution will 'just work'. But it does sound like the current organization structure isn't working with the auto-detect, so you may need a custom config template that more explicitly tells the engine how to break up the repo. Have you had a chance to look at: https://www.infracost.io/docs/features/config_file/ In particular, I often start with the base example in https://www.infracost.io/docs/features/config_file/#examples, and then tweak modifications to 'Advanced config template replicating Infracost's auto-detect behavior' - and then use these commands:
Copy code
infracost generate config --template-path=infracost.yml.tmpl > infracost.yml
infracost breakdown --config-file=infracost.yml
Until it comes out looking right
d
I have tried 2 things I created a static config: version: 0.1 projects: - path: modules name: main skip_autodetect: false include_all_paths: true This correctly detected the subscription modules but also all the subscription child modules and each project is named main so not ideal. I created a template file: version: 0.1 projects: {{- range $project := matchPaths "modules/env?/main.tf" }} - path: {{ $project._dir }} name: {{ $project._path }} {{- end }} This is based on this example in the docs: Example folder structure:
Copy code
├── foo/
│   └── main.tf
└── bar/
  ├── dev/
  │   └── main.tf
  └── prod/
      └── main.tf
The following range in a template:
Copy code
// Match a nested folder if it exists
{{- range $match := matchPaths ":app/:env?/main.tf" }}
Will loop over:
[{app: foo}, {app: bar, env: dev}, {app: bar, env: prod}]
This does not find any projects.