Search code examples
terraform

Print out Terraform variable/data information without applying


Different Terraform data sources return different types of data (lists, sets, maps, etc). The terraform code I'm analyzing is quite complex and there are many data transformations being done to the retrieved data.

Examples:

domain               = join(".", slice(local.dns_parts, length(local.dns_parts) - 2, length(local.dns_parts)))
...
mail_key             = trimsuffix(element(split(":", module.email_suppression_rds[0].db_instance_master_user_secret_arn), 6),substr(element(split(":", module.email_suppression_rds[0].db_instance_master_user_secret_arn), 6), -7, -1)

and other complex data transforms.

I want to be able to change some things, add different functions etc. I have no idea what these complex data transformations do to a string, list, set, map. I have to go through the Terraform provider documentation and reverse engineer every single step of this data transformation in order to get some semblance of what this code is doing.

Is there a way to test the data retrieval + data transformation done by Terraform and printing the output without applying the terraform configuration ? A printf-like functionality where Terraform will print the data to the screen after all the data transformations are done.

Is terraform plan the only available way to test this code ?

It is hard to use while writing complex terraform code with multiple modules because you may break something by providing some incorrect value and it will output some random error message hardly related to the main issue. It also often says "known-after-apply" which is very annoying and not helpful for development purposes.


Solution

  • For that purpose you can use the terraform console command. It will allow you to use any of the language-specific functions (e.g., join etc) and will display the results. In theory, you would enter the console by typing terraform console and then enter the transformation you wish, e.g:

    $ terraform console
    > join(".", slice(local.dns_parts, length(local.dns_parts) - 2, length(local.dns_parts)))
    

    Which should display the result. If you are using a variable which doesn't have a default value assigned, then you can just replace it with the value you know it will have or a similar one. Alternatively, you could also define outputs and run terraform plan, which can in some cases show the results if you have already run terraform apply previously. For the local variables, outputs should work without any issues unless they are also relying on an data source/resource/module.