Get current working directory in terraform

Terraform has a built-in object path that contains attributes for various paths Terraform knows about: path.module is the directory containing the module where the path.module expression is placed. path.root is the directory containing the root module. path.cwd is the current working directory. When writing Terraform modules, we most commonly want to resolve paths relative to … Read more

Terraform – How to restrict an input variable to a list of possible choices

Stumbled across this question. Since v0.13.0 input validation has been possible directly via the input variables. Thus you can actually achieve this with a snippet such as below. variable “test_variable” { type = string description = “some test value” validation { condition = contains([“item1”, “item2”, “item3”], var.test_variable) error_message = “Valid values for var: test_variable are … Read more

Can Terraform watch a directory for changes?

In Terraform 0.12 and later, one can use a for expression combined with a fileset function and one of the hashing functions to calculate a combined checksum for files in a directory: > sha1(join(“”, [for f in fileset(path.cwd, “*”): filesha1(f)])) “77e0b2785eb7405ea5b3b610c33c3aa2dccb90ea” The code above will calculate a sha1 checksum for each file in the current … Read more

Terraform provider/variable sharing in modules

Right now it’s not possible to achieve that. There were previous discussions on github about the same topic in the following issues: https://github.com/hashicorp/terraform/issues/5480 https://github.com/hashicorp/terraform/issues/4585 https://github.com/hashicorp/terraform/issues/2714 https://github.com/hashicorp/terraform/issues/1742 https://github.com/hashicorp/terraform/issues/1478 TL;DR the sharing of variables between modules is against terraform core clarity/explicity principles. Workaround A workaround is to have the *shared* files in the parent directory and using … Read more

How to reference a resource created by a Terraform module

Since you’re using a module, you need to change the format of the reference slightly. Module Outputs use the form ${module.<module name>.<output name>}. It’s also important to note, you can only reference values outputted from a module. In your specific case, this would become ${module.vpc.vpc_id} based on the VPC Module’s Outputs.

Error refreshing state: state data in S3 does not have the expected content [closed]

There are 3 possible workarounds depending on your specific scenario in order to solve it: Case 1 If you have a backup of your AWS S3 terrform.tfstate file you could restored your state backend “s3” {key = “path/to/terraform.tfstate”} to an older version. Re try terraform init and validate if it works well. Case 2 Remove … Read more

Terraform, can a resource be passed as a variable into a module?

This is possible with terraform > 0.12. You can use the object type, butt you have to explicitly list fields that you use within your module. # module’s variables.tf variable “parent_vnet” { # List each field in `azurerm_route_table` that your module will access type = object({ name = string location = string resource_group_name = string … Read more