Terraform variable files - azure

I am trying to use a variables file to deploy resource groups in Azure using Terraform but it works if I only have one variable. If I use two I get an error:
"invalid value "variables.tf" for flag -var-file: multiple map
declarations not supported for variables"
The variables file is as below :
variable "resource_group_name" {
description = "The name of the resource group in which the resources will be created"
default = "im-from-the-variables-file"
}
variable "location" {
description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
default = "west europe"
}
The main file used to deploy is as below:
resource "azurerm_resource_group" "vm" {
name = "${var.resource_group_name}"
location = "${var.location}"
}

You've confused the variable definition syntax to the variable setting syntax.
Terraform will concatenate all the .tf files in a directory so your variables.tf file (assuming it's in the same directory as your main.tf (or whatever contains your azurerm_resource_group resources etc) is already included.
You need to define every variable before it can be used so things like:
resource "azurerm_resource_group" "vm" {
name = "${var.resource_group_name}"
location = "${var.location}"
}
by themselves would not be valid as the variables resource_group_name and location are not defined.
You define variables with the syntax you've used in your variables.tf file:
variable "location" {
description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
default = "west europe"
}
To override the defaults (if wanted or if a default isn't provided) then you need to either pass the variable in at run time (using TF_VAR_location environment variables or by using -var location="west us") or you can define vars files that take the form:
location = "west us"
resource_group_name = "im-from-the-variables-file"
Terraform will automatically load any files in the directory called terraform.tfvars or *.auto.tfvars and you can also define extra vars files at any point by using -var-file=myvars.tfvars as you have attempted to do (but with a .tf file containing HCL instead of key-pairs.

Adding to what ydaetskcoR has mentioned in above answer. If you have already specified default values in variable file for all variables defined and you need just that default values you don't even need to pass -var-file since default values will be used if you don't pass values

Related

how do i substitute text for a variable in a terraform?

I'm new to terraform and wanted to substitute the value devcert below for a variable value called env, how do i format the below to include the variable value instead of devcert?
pfx_blob = data.azurerm_key_vault_secret.devcert.value
// Get Certificate from External KeyVault
resource "azurerm_app_service_certificate" "cert" {
name = "sslCertificate"
resource_group_name = "rg1"
location = "uk west"
pfx_blob = data.azurerm_key_vault_secret.devcert.value
You can't do that. Such operation is not supported in terraform. Instead you should use for_each to create multiple instances of azurerm_key_vault_secret, rather then fully separate data sources. Then you can reference it using:
pfx_blob = data.azurerm_key_vault_secret.devcert["myinstance"].value

Terraform how to skip argument for 1 workspace

Is it possible to skip 1 argument for 1 workspace in terraform?
resource "azurerm_application_gateway" "appgw" {
name = var.appgw
resource_group_name = var.resource_group
location = var.location
**zones = var.aks_zones**
sku {
name = var.app_gateway_sku
tier = var.app_gateway_tier
}
I am setting a DR environment in a region where availability zones are not supported, so for the script to pass the "Zones" argument needs to be skipped for one workspace only. Is this possible?
To fix this, I added an availability_zone = "No-Zone" argument to my AppGW IP address block.
Since the azurerm_application_gateway resource's zones variable is Optional (source), you can set the default value for your aks_zones variable to null:
variable "aks_zones" {
default = null
}
This way, you can skip specifying the aks_zones variable for your one workspace while setting a value for the other workspaces.

Terraform - Variable name for CosmosDB

I'm using Terraform to create the resources in Azure and have split the files as main.tf, variables.tf and terraform.tfvars
In order to have a standard naming convention, I'm following the process below when naming the resources.
prefix-environment-resourcename
For example, in main.tf I'm creating it as below:
resource "azurerm_resource_group" "rg" {
name = "${var.prefix}-${var.environment}-${var.resource_group_name}"
location = "westus"
}
The variables will be declared in variables.tf and the terraform.tfvars will contain
prefix = "sample"
environment = "dev"
resource_group_name = "rg"
and when the Terraform is executed, I'll get the resource name created as "sample-dev-rg"
This will come in handy when I'm creating other resources or deploy the code to other environments. Since I just need to modify the tfvars alone.
Another example:
resource "azurerm_app_service" "example" {
name = "${var.prefix}-${var.environment_name}-${var.appservice_name}"
}
My issue is:
How do I use the logic above for CosmosDb? I need the name in the main.tf to be
created without special characters.
How do I create something like
this: sampledevcosmosdbname
If you're using Terraform 0.13 and above, you can make use of regex validation for each of the variables that make up your resource names, and ensure that none of them use special/unusual characters. Here's an example prefix variable that can only use A-Z, a-z, 0-9, and - characters:
variable "prefix" {
type = string
description = "Prefix ID"
validation {
condition = can(regex("^[A-Za-z0-9-]*$", var.prefix))
error_message = "The prefix cannot use special characters."
}
}
To create something like sampledevcosmosdbname (prefix, environment, dbname), you can just place several interpolations next to one another like so - no separation is needed:
resource "azurerm_cosmosdb_sql_database" "example" {
...
name = "${var.prefix}${var.environment}${var.dbname}"
}

Terraform . How to pass multiple values in command line using list (string) in variable.tf file?

I have a simple main and variable files for deploying webapp for containers in Azure.
But I would like that terraform plan uses variables from the command line to choose names like follows:
terraform plan -var resource_group_name=my-rg
This worked perfectly commenting the name of the default value for the RG like this.
main.tf
data "azurerm_resource_group" "my-rg" {
name = var.resource_group_name
}
variable.tf
variable "resource_group_name" {
# default = "Search-API"
}
But if I want to do the same for a list string I don´t know how to do it. I want to be able to do something that If I put 2 names 2 webapps are going to be created, if I put 3, 3 webapps and so.
I tried with this (also commenting default value) :
main.tf
resource "azurerm_app_service" "azure-webapp" {
count = length(var.webapp_server_name)
name = var.webapp_server_name[count.index]
variable.tf
variable "webapp_server_name" {
description = "Create Webapp with following names"
type = list(string)
#default = ["webapp-a", "webapp-b", "webapp-c"]
But I´m getting:
terraform plan -var webapp_server_name=webapp-a
Error: Variables not allowed
on <value for var.webapp_server_name> line 1:
(source code not available)
Variables may not be used here.
I also tried with empty string like:
variable "webapp_server_name" {
description = "Create Webapp with following names"
type = list(string)
default = []
}
Is there a way to do such a thing with terraform? to define an empty list and pass values (one, two, or more) from command?
thanks
UPDATE
Tried like this, following this post but now is asking to put the value even though I´m passing it through command line
terraform plan -var 'listvar=["webapp-a"]'
var.webapp_server_name
Create Webapp with following names
Enter a value:
If there is a variable declaration:
variable "webapp_server_name" {
description = "Create Webapp with following names"
type = list(string)
#default = ["webapp-a", "webapp-b", "webapp-c"]
}
You could use it like this with \ to escape the quotes".
terraform plan -var 'webapp_server_name=[\"webapp-a\", \"webapp-b\", \"webapp-c\"]'
For example, it worked with using the latest terraform provider version Terraform v0.13.4.
When you pass in the variable from the command line with -var webapp_server_name=webapp-a you are passing it in as a string.
But you've defined the variable as a list. So based on the docs you'll want the command line to look something like:
terraform plan -var='webapp_server_name=["webapp-a"]'

Creating two VMs in the same resource group without Terraform wanting to destoy the first one

I'm trying to deploy two virtual machines within the same resource group to our Azure platform with Terraform. After successfully creating the first one Terraform then wants to destroy the first one to create the second one after I've changed the second VM name and Azure tag.
I've been following the Terraform guide: https://www.terraform.io/docs/providers/azurerm/r/virtual_machine.html
resource "azurerm_virtual_machine" "main" {
location = "${var.location}"
name = "${var.vm_name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
resource_group_name = "${var.resourcegroup_vm}"
vm_size = "${var.vm_size}"
tags {
application = "${var.tag}"
}
I expected Terraform to just create the second VM after changing its variable name and tag. Not wanting to destory the first one because of the name and tag change.
Terraform is based on HCL (Hashicorp Configuration Language), which is the format *.tf files are written in. It is a declarative language (as opposed to imperative), which means that you describe the desired state you want your infrastructure to be and Terraform will figure out what changes are needed to take it to that point.
If you first create an instance and then change its name you are telling Terraform that you no longer want your instance to have the old name but the new one.
To deploy a number of instances you can use the count attribute. You could then use interpolation to get names and tags based in the counter, something similar to this:
resource "azurerm_virtual_machine" "main" {
location = "${var.location}"
name = "${var.vm_name}-${count.index + 1}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
resource_group_name = "${var.resourcegroup_vm}"
vm_size = "${var.vm_size}"
tags {
application = "${var.tag}-${count.index + 1}"
}
count = 2
}
Note the attached -${count.index + 1} to name and the application tag.

Resources