Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable expansion in infracost #334

Closed
djtecha opened this issue Feb 8, 2022 · 7 comments
Closed

Variable expansion in infracost #334

djtecha opened this issue Feb 8, 2022 · 7 comments
Assignees
Labels
estimate/1h Need 1 hour to be done feature New feature or request hook/infracost_breakdown Bash hook

Comments

@djtecha
Copy link

djtecha commented Feb 8, 2022

I am trying to use the infracost pre-commit hook and wondering if there was a way to mimick the FR in this issue: #264

Trying to use the macros so I can have specific config files per project and it appears to not be expanding the GIT_WORKING_DIR variable.

repos:
- repo: git://github.com/antonbabenko/pre-commit-terraform
  rev: v1.62.3
  hooks:
    - id: infracost_breakdown
      args:
        - --args=--config-file=__GIT_WORKING_DIR__/infracost.yml
      verbose: true
@djtecha djtecha added the feature New feature or request label Feb 8, 2022
@antonbabenko
Copy link
Owner

@MaxymVlasov Could you put your infracost hat socks and take a look at this one?

@MaxymVlasov
Copy link
Collaborator

Let's fix that!

IMG_20220210_171900

@MaxymVlasov
Copy link
Collaborator

Hmm, can you provide an example where you need GIT_WORKING_DIR? Because it works fine without that var.

.pre-commit-config.yaml:

repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
  rev: v1.64.0
  hooks:
    - id: infracost_breakdown
      args:
        - --args=--config-file=infracost.yml
        # - --args=--path=./environment/qa
        - --hook-config='.totalHourlyCost >= 0.1'
        - --hook-config=".totalHourlyCost|tonumber > 1"
        - -h ".projects[].diff.totalMonthlyCost|tonumber!=10000"
        - -h [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000
      verbose: true # Always show costs

infracost.yml:

version: 0.1

projects:
  - path: ./environment/qa

Feel free to reopen the issue.

@djtecha
Copy link
Author

djtecha commented Feb 11, 2022

Is that infracost.yml in the top level dir? Because I think I need to create specific ones per TF workspace. So if I edit a file in something like ops/stg/main.tf it would trigger the infracost and use the config file found in ops/stg/infracost.yaml

@MaxymVlasov
Copy link
Collaborator

Here not in a root, works fine too

.pre-commit-config.yaml:

repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
  rev: v1.64.0
  hooks:
    - id: infracost_breakdown
      args:
        - --args=--config-file=test/infracost.yml
        - --hook-config='.totalHourlyCost >= 0.1'
        - --hook-config=".totalHourlyCost|tonumber > 1"
        - -h ".projects[].diff.totalMonthlyCost|tonumber!=10000"
        - -h [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000
      verbose: true # Always show costs

test/infracost.yml:

version: 0.1

projects:
  - path: ./environment/qa

@djtecha
Copy link
Author

djtecha commented Feb 24, 2022

Sry I've been negligent here. Finally had some time to circle back to this and maybe I'm misunderstanding how this should be designed. Say I had multiple workspaces that existed in the same repo like this:

workspace/
  serviceA/
    prod/
    stg/
  serviceB/
    prod/
    stg/

And I wanted infracost to run anytime a file a was changed in a SERVICE/ENV dir. How would I structure this if I have different AWS profiles I need to sue depending on ENV? Trying your method still requires me to have a block defining the config file in my top level pre-commit-config.yaml and it would run that against every service every time which isn't entirely needed.

@djtecha
Copy link
Author

djtecha commented Feb 24, 2022

Ok, maybe I get this. So I should jsut refference 1 infracost.yaml file that has a large set of projects defined (have a script that can generate this) Then those runs only get triggered on a file change.

# infracost.yml
version: 0.1
projects:
  - path: workspace/ops/stg
    env:
      AWS_PROFILE: stg
  - path: workspace/tf_test/stg
    env:
      AWS_PROFILE: stg
  - path: workspace/test_2/stg
    env:
      AWS_PROFILE: stg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
estimate/1h Need 1 hour to be done feature New feature or request hook/infracost_breakdown Bash hook
Projects
None yet
Development

No branches or pull requests

3 participants