I've configured the following variable in module/variables.tf
variable "instance_name" {
type = string
default = "instance-1"
description = "Name of the instance."
}
I refer to the variable in the same module module/main.tf as below
resource "google_compute_instance" "cloud_instance" {
name = var.instance_name
}
However, when I run terraform init, I get the following error-
Error: Error parsing /module/main.tf: At 15:12: Unknown token: 15:12 IDENT var.instance_name
Any idea why this is happening?
You need to refer variable as below to ensure expansion works correctly -
resource "google_compute_instance" "cloud_instance" {
name = "${var.instance_name}"
}
Using terraform 0.12.9 - I do not get this error on init or plan or validate, only on 0.12upgrade.
Also the documentation shows that if the variable is not in a string, it is possible to pass it without quotation marks and curly braces.
So which is correct?
Related
New to terraform, and have been building out the infrastructure recently.
I am trying to pull secrets from azure key vault and assign the keys to the variables.tf file depending on the environment(dev.tfvars, test.tfvars, etc). However when I execute the plan with the tfvar file as the parameter, I get an error with the following message:
Error: Variables not allowed
Here are the files and the relevant contents of it.
variables.tf:
variable "user_name" {
type = string
sensitive = true
}
data.tf (referencing the azure key vault):
data "azurerm_key_vault" "test" {
name = var.key_vault_name
resource_group_name = var.resource_group
}
data "azurerm_key_vault_secret" "test" {
name = "my-key-vault-key-name"
key_vault_id = data.azurerm_key_vault.test.id
}
test.tfvars:
user_name = "${data.azurerm_key_vault_secret.test.value}" # Where the error occurrs
Can anyone point out what I'm doing wrong here? And if so is there another way to achieve such a thing?
In Terraform a variable can be used for user input only. You can not assign to them anything dynamically computed from your code. They are like read-only arguments, for more info see Input Variables from the doc.
If you want to assign a value to something for later use, you must use locals. For example:
locals {
user_name = data.azurerm_key_vault_secret.test.value
}
Local values can be changed dynamically during execution. For more info, see Local Values.
You can't create dynamic variables. All variables must have known values before execution of your code. The only thing you could do is to use local, instead of variabile:
locals {
user_name = data.azurerm_key_vault_secret.test.value
}
and then refer to it as local.user_name.
I am using Terraform Cloud for my Backend with version 1.1
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
cloud {
hostname = "app.terraform.io"
organization = "MyOrg"
workspaces {
name = "MyWorkspace"
}
}
}
I have a variable in my HCL
variable "app_version" {
description = "The application version to deploy"
type = string
}
I am attempting to set it when I call terraform apply like so:-
terraform apply -var="app_version=v0.0.1"
I get the following error though.
1 error occurred:
* Invalid HCL in variable "app_version": At 1:15: Unknown token: 1:15 IDENT v0.0.1
What does this mean?
I think the Terraform docs for command line variables do a poor job of explaining that, but you basically need to wrap the value in double quotes. As the value passed after = is interpreted as Terraform expression, not constant (which opens up for other possibilities).
So, try:
terraform apply -var 'app_version="v0.0.1"'
Also notice there should be no = after -var
Grzegorz was correct but if you are on a windows machine you will need to escape the double quotes but adding a \ before each double quote.
According to the documentation .. global_parameter is indeed a valid argument. However when trying to use it, my validate command reports that An argument named "global_parameter" is not expected here. Did you mean to define a block of type "global_parameter"?
My config is simply
resource "azurerm_data_factory" "adf" {
name = "adf-${var.project}"
location = var.az_location
resource_group_name = azurerm_resource_group.rg.name
tags = {
environment = var.environment
project = var.project
}
global_parameter = {}
}
I am using version 2.81 of the azurerm provider.
The documentation doesn't seem correct. It's not supposed to be an argument; it's actually supposed to be declared as a block. Hence this is incorrect:
global_paramter = {}
and this is correct:
global_parameter {}
(No equals assignment here)
The documentation says it's an argument while the error suggests you probably wanted a block instead of an argument.
I have a use case where I need two AWS providers for different resources. The default aws provider is configured in the main module which uses another module that defines the additional aws provider.
By default, I'd like both providers to use the same AWS credentials unless explicitly overridden.
I figured I could do something like this. In the main module:
locals {
foo_cloud_access_key = aws.access_key
foo_cloud_secret_key = aws.secret_key
}
variable "foo_cloud_access_key" {
type = string
default = local.foo_cloud_access_key
}
variable "foo_cloud_secret_key" {
type = string
default = local.foo_cloud_secret_key
}
where variables foo_cloud_secret_key and foo_cloud_access_key would then be passed down to the child module like this:
module foobar {
...
foobar_access_key = var.foo_cloud_access_key
foobar_secret_key = var.foo_cloud_secret_key
...
}
Where module foobar would then configure its additional was provide with these variables:
provider "aws" {
alias = "foobar_aws"
access_key = var.foobar_access_key
secret_key = var.foobar_secret_key
}
When I run the init terraform spits out this error (for both variables):
Error: Variables not allowed
on variables.tf line 66, in variable "foo_cloud_access_key":
66: default = local.foo_cloud_access_key
Variables may not be used here.
Is it possible to achieve something like this in terraform or is there any other way to go about this?
Having complex, computed default values of variables is possible, but only with a workaround:
define a dummy default value for the variable, e.g. null
define a local variable, its value is either the value of the variable or the actual default value
variable "something" {
default = null
}
locals {
some_computation = ... # based on whatever data you want
something = var.something == null ? local.some_computation : var.something
}
And then only only use local.something instead of var.something in the rest of the terraform files.
I want to add a content to the file using terraform using a local provider. Here is the example script which I am using
terraform {
required_version = "~>0.13"
required_providers {
local = "~>1.4"
}
}
resource "local_file" "literature" {
filename = "art_of_war.txt"
content = <<EOT
Hello
world
EOT
}
I am getting the following error Expected the start of an expression, but found an invalid expression token. .Can you please point what might be error.
It seems that in your example you were using tabs instead of spaces (or you have it configured in your editor). I recreated your example with using only spaces and it worked. Here's the code snippet that works:
resource "local_file" "literature" {
filename = "art_of_war.txt"
content = <<EOT
Hello
World
EOT
}
Note that EOT is left-aligned to the same level as resource.
EDIT: Actually, it seems there's a whitespace after the <<EOT, if you delete it it should work.