https://infracost.io logo
#help
Title
# help
w

white-airport-8778

01/17/2024, 7:27 PM
1. Is there a smarter way to setup this config? I’m having issues managing the varied directory structures:
```version: 0.1
projects:
{{- range $project := matchPaths "app/env?/terraform.tf" }}
{{- if ne $project.app "modules" }}
- path: ./{{ $project._dir }}
{{- if ne $project.app "ami-upgrade" }}
terraform_var_files:
- ../inputs.auto.tfvars
{{- end }}
{{- end }}
{{- end }}
{{- range $project := matchPaths "app/env/:why/terraform.tf" }}
{{- if ne $project.app "modules" }}
- path: ./{{ $project._dir }}
{{- if ne $project.app "ami-upgrade" }}
terraform_var_files:
- ../../inputs.auto.tfvars
{{- end }}
{{- end }}```
{{- end }}
@mysterious-teacher-68276 any ideas on this?
@acceptable-kilobyte-27376 was asking about it 🙂
a

acceptable-kilobyte-27376

01/17/2024, 7:28 PM
I think a clearer example of how to formulate an if statement for like pattern matching might help?
m

mysterious-teacher-68276

01/17/2024, 7:29 PM
I think a clearer example of how to formulate an if statement for like pattern matching might help?
Not sure I fully understand what your asking here?
a

acceptable-kilobyte-27376

01/17/2024, 7:30 PM
I made a minor change as well, because the naming is also somewhat relative.
Copy code
version: 0.1
projects:
{{- range $project := matchPaths ":app/:env?/terraform.tf" }}
  {{- if ne $<http://project.app|project.app> "modules" }}
    - path: ./{{ $project._dir }}
      name: {{ $<http://project.app|project.app> }}-{{ $project.env }}
    {{- if ne $<http://project.app|project.app> "ami-upgrade" }}
      terraform_var_files:
        - ../inputs.auto.tfvars
    {{- end }}
  {{- end }}
{{- end }}

{{- range $project := matchPaths ":app/:env/:why/terraform.tf" }}
  {{- if ne $<http://project.app|project.app> "modules" }}
    - path: ./{{ $project._dir }}
      name: {{ $<http://project.app|project.app> }}-{{ $project.env }}-{{ $project.why }}
    {{- if ne $<http://project.app|project.app> "ami-upgrade" }}
      terraform_var_files:
        - ../../inputs.auto.tfvars
    {{- end }}
  {{- end }}
{{- end }}
So everything in this repo is set to use a var file that's at.. ./<app>/<env>/<file>.tfvars Problem is some of the components exist at: ./<app>/<env>/main.tf and ./<app>/<env>/<something>/main.tf
So I'm trying to figure out how set the path relatively. In some cases its: ../file.tfvars others: ../../file.tfvars
If that makes zero sense what I have seems to work. I just felt like there might be a cleaner way.
m

mysterious-teacher-68276

01/17/2024, 7:35 PM
No that makes sense, just out of curiosity do you have the latest version of the CLI on your machine? We’ve made some updates to the autodetection functionality so in your repos case you might not even have to write a config-file
a

acceptable-kilobyte-27376

01/17/2024, 7:35 PM
I installed it locally this morning via
curl -fsSL <https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh> | sh
I however did not try to see what happens without a config file. Maybe I missed it (or was specifically looking at the config file section). How would we test locally without a config file at all? Just remove
--template-path
?
m

mysterious-teacher-68276

01/17/2024, 7:37 PM
yup exactly
in terms of a cleaner way with your config file - there isn’t really a cleaner way because of the way we require var files to be made relative
it’s bit a pain I know, we are looking to improve this
a

acceptable-kilobyte-27376

01/17/2024, 7:38 PM
Okay.. That actually looks super right. Follow up from that.. If we set an account wide config file base how would we override a repo to use nothing instead? Or would the better path be to look at removing that altogether?
m

mysterious-teacher-68276

01/17/2024, 7:39 PM
Okay.. That actually looks super right.
💪 nice
Follow up from that.. If we set an account wide config file base how would we override a repo to use nothing instead?
Hmm we’d have to write a config file with the special autodetect syntax (nots in the docs at the moment). We don’t have a way to specify “don’t use a config-file” at the moment as these new auto-detect improvements are very recent.
Or would the better path be to look at removing that altogether?
Let me link up with @white-airport-8778 about this and we can discuss but this might be the best way. Are all of your repos in a semi consistent fashion? i.e. how you specify vars e.t.c?
a

acceptable-kilobyte-27376

01/17/2024, 7:42 PM
No.. Not at all. Its actually a real headache
m

mysterious-teacher-68276

01/17/2024, 7:43 PM
ha no worries, most people are like this, no one has it consistent
a

acceptable-kilobyte-27376

01/17/2024, 7:45 PM
We have service repos that store their terraform stuff in a
.infrastructure
folder. It looks like that doesn't get parsed by the autoconfig (I assume it ignores .folders).
Error: No valid Terraform files found at path
m

mysterious-teacher-68276

01/17/2024, 7:45 PM
ah yeah we don’t parse hidden directories
ok well for the repos that work with config detect you can update them to use this special config-file:
Copy code
version: 0.1
projects:
{{- range $mod := .DetectedRootModules }}
  {{- range $project := $mod.Projects }}
  - path: {{ $mod.Path }}
    name: {{ $project.Name }}
    terraform_var_files:
    {{- range $varFile := $project.TerraformVarFiles }}
      - {{ $varFile }}
    {{- end }}
  {{- end}}
{{- end }}
a

acceptable-kilobyte-27376

01/17/2024, 7:46 PM
Yeah. So we'd need a way to like.. Flag config to NOT use the org settings file. Because the service repos are our most numerous and changing repos.
m

mysterious-teacher-68276

01/17/2024, 7:47 PM
cool - we can also probably make a change to support hidden directories as well, shouldn’t be too much of an issue
which means you could ditch all the config files
w

white-airport-8778

01/17/2024, 7:47 PM
Would having an Exclude filter in the Org-level cofig file be good idea? or would you prefer to have a per-repo flag in that UI?
a

acceptable-kilobyte-27376

01/17/2024, 7:47 PM
I could see hidden directories TOTALLY going either way
m

mysterious-teacher-68276

01/17/2024, 7:48 PM
when I say hidden directories I mean, having a whitelisted list that you specify that we look in
i.e., look in
.infrastructure
a

acceptable-kilobyte-27376

01/17/2024, 7:48 PM
Oh yeah. That would simplify things.
At the same time the "default logic" code block is something we can easily put in our one-off repos. So its not a huge issue.
m

mysterious-teacher-68276

01/17/2024, 7:50 PM
ok well in the meantime this config file: https://infracost-community.slack.com/archives/C046GMHQ6NM/p1705520810031719?thread_ts=1705519634.794369&amp;cid=C046GMHQ6NM should work for that. Until we have that flag drafted.
a

acceptable-kilobyte-27376

01/17/2024, 7:51 PM
Yeah I've tested it a little and it looks good. Thank you!
Okay follow up to the followup. On some of our more simple repos it looks great. On our more confusing repos it doesn't seem to do the job at all. The estimated cost projection dropped from like $17k to $3k. And it seemed to group things a level "up". The config we're using for that repo is...
Copy code
version: 0.1
projects:
{{- range $module := matchPaths ":module/resources/:env/:name.tfvars" }}
  - path: ./{{ $module.module }}
    name: {{ $module.env }}-{{ $module.module }}-{{ $module.name }}
    include_all_paths: true
    terraform_var_files:
      - resources/{{ $module.env}}/{{ base $module._path }}
{{- end }}
{{- range $module := matchPaths ":module" }}
  {{- if not (startsWith $module.module ".")}}
    {{- if isDir $module.module }}
      {{- if not (pathExists $module.module "resources") }}
  - path: {{ $module.module }}
    name: {{ $module.module }}
      {{- end }}
    {{- end }}
  {{- end }}
{{- end}}
m

mysterious-teacher-68276

01/18/2024, 5:42 AM
Ok sorry I didn’t see this yesterday, was late in France. Which repo is this and I can have a look what’s going wrong
5 Views