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

infracost is not fetching variables defined in Terraform Enterprise #3004

Open
vitorstone opened this issue Apr 8, 2024 · 1 comment
Open
Assignees
Labels
bug Something isn't working

Comments

@vitorstone
Copy link

Description

Hello!

I'm trying to do a simple run of infracost from my Jenkins declarative pipeline. This is the stage I have for infracost:

stage("infracost"){
            steps{
                container('runner'){
                    script {
                        withCredentials([string(credentialsId: 'terraform-infracost-apikey', variable: 'INFRACOST_API_KEY')]) {
                            withCredentials([string(credentialsId: 'tfe-token', variable: 'INFRACOST_TERRAFORM_CLOUD_TOKEN')]) {
                                env.INFRACOST_CURRENCY = "CAD"
                                env.INFRACOST_TERRAFORM_CLOUD_HOST = "REDACTED"
                                env.INFRACOST_TERRAFORM_CLOUD_WORKSPACE = env.TF_WORKSPACE
                                sh "env | grep INFRACOST"
                                sh 'infracost configure set api_key $INFRACOST_API_KEY'
                                sh "infracost breakdown --path ."
                            }
                        }
                    }
                }
            }
        }

The command runs successfully and it does provide me with the cost, however I get a warning:

+ infracost breakdown --path .

2024-04-08T18:35:56-04:00 INF Evaluating Terraform directory at .

2024-04-08T18:35:56-04:00 INF Starting: Downloading Terraform modules

2024-04-08T18:35:56-04:00 INF Starting: Evaluating Terraform directory

2024-04-08T18:35:56-04:00 WRN Input values were not provided for following Terraform variables: "variable.required_aws_tags", "variable.eks_vpc_id", "variable.eks_vpc_private_subnet_ids", "variable.aws_region", "variable.rancher_eks_user_cloud_credential_names", "variable.eks_devopstools_cluster_name". Use --terraform-var-file or --terraform-var to specify them.

2024-04-08T18:35:57-04:00 INF Starting: Retrieving usage defaults for 5 resources from Infracost Cloud

2024-04-08T18:35:57-04:00 INF Starting: Retrieving cloud prices to calculate costs

For some reason it looks like it's not fetching the variables I've defined on my Terraform Enterprise Workspace. Even though I specified all the necessary variables:

env.INFRACOST_TERRAFORM_CLOUD_HOST = "terra.tmx.cloud"
env.INFRACOST_TERRAFORM_CLOUD_WORKSPACE = env.TF_WORKSPACE

Solution

In fact, I was able to make it work while writing this. I was missing the INFRACOST_TERRAFORM_CLOUD_ORG environment variable. However, the documentation mentions the following:

image

So I believe this is a bit misleading and should be corrected. Because my cloud block is valid (I was able to run a terraform init and terraform plan successfully right before running the infracost stage), that's why I hadn't set that environment variable in the first place.

The infracost utility was probably not able to parse the organization from my cloud block in versions.tf. The problem may be that in my cloud block I have this:

cloud {
    organization = "REDACTED"
    hostname     = "REDACTED"
  }

Meaning, I don't set the workspace in there. Whenever I run an init/plan, the workspace is retrieved from the TF_WORKSPACE variable instead. So maybe infracost deemed my cloud block invalid because it's "imcomplete". But it's still a valid cloud block nonetheless.

@aliscott aliscott added the bug Something isn't working label Apr 9, 2024
@aliscott
Copy link
Member

aliscott commented Apr 9, 2024

Thanks for reporting @vitorstone. It looks like there's a few env variables that can set any missing values in the cloud block (https://developer.hashicorp.com/terraform/cli/cloud/settings#tf_cloud_organization) so we should look at supporting them all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants