hey <#C046GMHQ6NM|>, I want to write my custom `in...
# help
b
hey #C046GMHQ6NM, I want to write my custom
infracost-usage.yml
where my resource is
google_compute_instance
now all I want is that I want to put usage in a way that it will be running only for 8 hours a day rather 24 hours a day. How do I achieve this, I checked org setttings and usage but I did not find the relevant field. Also, below is the result of one of my merge request, not sure why it is not in proper table i.e. you see how new monthly cost has nothing under it..
w
follow this docs section, and use this to set the hrs. As noted in that doc, you can either set the hrs for 1 VM using
resource_usage
or all VMs using
resource_type_default_usage
b
thanks @white-airport-8778, However not sure why my table looks in bad format. below is my gitl;ab-ci.yml
Copy code
image: dipkmish/terraform:v1.9.3

variables:
  GOOGLE_PROJECT: $GOOGLE_PROJECT
  SERVICE_ACCOUNT: "$GOOGLE_CREDENTIALS"
  TF_STATE_BUCKET: "infracost-state-bucket"
  TF_STATE_PREFIX: "terraform/state"
  INFRACOST_API_KEY: $INFRACOST_API_KEY
  TF_ROOT: terraform
  TOKEN: $GITLAB_TOKEN

stages:
  - validate
  - plan
  - infracost
  - apply
  - destroy

before_script:
  - rm -rf .terraform
  - terraform --version
  - export GCP_KEY='${SERVICE_ACCOUNT}'
  - echo "$GCP_KEY" > /tmp/gcpkey.json
  - export GOOGLE_APPLICATION_CREDENTIALS=/tmp/gcpkey.json
  - cd terraform
  - terraform init -reconfigure -get=true -upgrade=true -backend-config=bucket=${TF_STATE_BUCKET} -backend-config=prefix=${TF_STATE_PREFIX} -no-color


infracost-report:
  stage: infracost
  image:
    name: infracost/infracost:ci-0.10
    entrypoint: [""]
  before_script: []
  script:
    # Clone the base branch of the pull request (e.g. main/master) into a temp directory.
    - git clone $CI_REPOSITORY_URL --branch=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME --single-branch /tmp/base

    # Generate an Infracost cost snapshot from the comparison branch, so that Infracost can compare the cost difference.
    - infracost breakdown --path /tmp/base/${TF_ROOT} --usage-file infracost-usage.yml --format json --out-file=infracost-base.json
    - infracost output --path infracost-base.json --format html > infracost_report.html

    # Generate an Infracost diff and save it to a JSON file.
    - infracost diff --path . --usage-file infracost-usage.yml --compare-to infracost-base.json --format=json --out-file=infracost.json

    # Posts a comment to the PR using the 'update' behavior.
    - infracost comment gitlab --path infracost.json --repo $CI_PROJECT_PATH --merge-request $CI_MERGE_REQUEST_IID --gitlab-server-url $CI_SERVER_URL --gitlab-token $TOKEN --behavior=update
  artifacts:
    paths:
      - infracost_report.html
      # - infracost_report_usage.html
    expire_in: never
  only:
    - merge_requests

# Run Infracost on default branch and update Infracost Cloud
infracost:default-branch-update:
  stage: infracost
  image:
    name: infracost/infracost:ci-0.10
    entrypoint: [""]
  before_script: []
  script:
    - |
      infracost breakdown --path ${TF_ROOT} \
                          --format=json \
                          --out-file=infracost.json

      infracost upload --path infracost.json
  variables:
    INFRACOST_API_KEY: $INFRACOST_API_KEY
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

# Set the MR status to Merged in Infracost Cloud
infracost:merge-request-status-update:
  stage: infracost
  image:
    name: infracost/infracost:ci-0.10
    entrypoint: [""]
  before_script: []
  script:
    - |
      # Extract Merge Request ID from the Commit Message
      if [[ ${CI_COMMIT_MESSAGE} =~ ${PATTERN} ]]; then
        MR_ID=${BASH_REMATCH[1]};
      else
        echo "${VTY_RB}Unable to extract Merge Request ID${VTY_P}"; exit 1;
      fi

      curl \
        --request POST \
        --header "Content-Type: application/json" \
        --header "X-API-Key: ${INFRACOST_API_KEY}" \
        --data "{ \"query\": \"mutation {updatePullRequestStatus( url: \\\"${CI_PROJECT_URL}/merge_requests/${MR_ID}\\\", status: MERGED )}\" }" \
        "<https://dashboard.api.infracost.io/graphql>";
  variables:
    PATTERN: "See merge request.+?!([0-9]+)"
    INFRACOST_API_KEY: $INFRACOST_API_KEY
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_TITLE =~ /^Merge branch/

validate:
  stage: validate
  script:
    - terraform validate

plan:
  stage: plan
  script:
    - terraform plan -var-file="terraform.tfvars" -out=tfplan
  artifacts:
    paths:
      - terraform/tfplan

apply:
  stage: apply
  needs: 
    - plan
  script:
    - terraform apply -auto-approve tfplan
  when: manual

destroy:
  stage: destroy
  needs: 
    - plan
  script:
    - terraform destroy -var-file="terraform.tfvars" -auto-approve
  when: manual
w
when you open an MR and only change an instance type in 1 project, does it show the right table formatting? it’s odd to see the same project appear twice in that table
b
It doesnt seems to be working.. see my changes https://gitlab.com/dipkmish/infracost/-/merge_requests/7/diffs
Mate, do you have 2 mins so that I can explain you what I am doing.. if you can come on huddle if possible pls.
In above MR, I am changing timings from 240 to 520 hours.. so ideally it should increase cost but in 2 projects it is doing $120 to 0 and in other it is 0 to $120. Looks like it got stuck to $120 as number.. also why 2 tables in my comment.. this i am not sure as well..
Now that I merged it .. it still says 0 cost change, ideally there should be cost change because I am increasing usage from 240 hours per month to 520 hours per month.
I got it working by making a change in usage file. I added the name of the resource which i have in terraform for state files. But i am still not sure why the table is not comming properly.
Can we also estimate for the number of request any server will be handling .. for eg like if we have nginx running on compute instance .. can we estimate if we know that 1000 request is going to be served .. just wanted to check that cost ..
See this, not sure why it is making it to 0 first.. it should compare with the existing price to the estimated price post changes.. also not sure why 2 projects are listed here.. also the formatting of table where new monthly cost is oddly placed. I dont understand this - / + of the cost
w
I’ll DM you
b
Tnx