Terraform Combine Variable and String

You can’t use interpolation in a tfvars file. Instead you could either join it directly in your Terraform like this: terraform.tfvars hosted_zone = “example.com” domain = “my” main.tf resource “aws_route53_record” “regional” { zone_id = data.aws_route53_zone.selected.zone_id name = “${var.domain}.${var.hosted_zone}” type = “A” ttl = “300” records = [“4.4.4.4”] } Or, if you always need to compose … Read more

Fix “Interpolation-only expressions are deprecated” warning in Terraform

Warning: Interpolation-only expressions are deprecated on main.tf line 3, in provider “aws”: 3: region = “${var.region}” I also got the above warning, which is due to the changed syntax for declaring variables in terraform. See the example below -: Old syntax– region = “${var.region}” # you will get Interpolation-only warning New Synatx– region = var.region … Read more

Conditional attributes in Terraform

Terraform 0.12 (yet to be released) will also bring support for HCL2 which allows you to use nullable arguments with something like this: resource “aws_ebs_volume” “my_volume” { availability_zone = “xyz” size = 30 snapshot_id = var.staging_mode ? local.a_specific_snapshot_id : null } Nullable arguments are covered in this 0.12 preview guide. For version of Terraform before … Read more

Variable keys in terraform maps

There’s (now) a lookup function supported in the terraform interpolation syntax, that allows you to lookup dynamic keys in a map. Using this, I can now do stuff like: output “image_bucket_name” { value = “${lookup(var.image_bucket_names, var.environment, “No way this should happen”)}” } where: variable “image_bucket_names” { type = “map” default = { development = “bucket-dev” … Read more

Can Terraform be used to provision on-premises servers?

Terraform operates by calling into the APIs of various service providers and systems. Thus in principle Terraform can manage anything that has an API, and in practice it has existing support for a few different on-premises-capable systems, including: OpenStack VMWare vSphere CloudStack If the compute resources in your existing datacenter infrastructure are already managed with … Read more

terraform.tfvars vs variables.tf difference [duplicate]

The distinction between these is of declaration vs. assignment. variable blocks (which can actually appear in any .tf file, but are in variables.tf by convention) declare that a variable exists: variable “example” {} This tells Terraform that this module accepts an input variable called example. Stating this makes it valid to use var.example elsewhere in … Read more

Terraform variables within variables

Terraform does not support variables inside a variable. If you want to generate a value based on two or more variables then you can try Terraform locals. You can define the locals like this: locals { tags = { Environment = “${var.EnvironmentName}” CostCentre = “C1234” Project = “TerraformTest” Department = “Systems” } } And then … Read more