Terraform how to skip argument for 1 workspace - azure

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.

Related

Terraform workspace specific lines of code in Azure

I'm having an issue using terraform workspaces between a region pair. One workspace for South Central US and one for North Central US. Everything works great until it comes to zones. Public IP for example. South I’m setting zones, North won’t accept zone configuration empty, 0, or 1,2, or 3 because it doesn’t support zones yet. Hoping to set zones in SCU and eventually doing the same in NCU when they become available.
How do I use the same code in both regions? I know I can use workspace vars for values, but in this case it is an entire line of code. Seems like there should be an easy answer I’m just not thinking of.
Public IP code as an example below, but it the solution I would apply for VM deployments as well.
resource "azurerm_public_ip" "example" {
name = "acceptanceTestPublicIp1"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Static"
zones = [1]
tags = {
environment = "Production"
}
}
You can check the terraform.workspace variable, and set the value to null if you don't want to set it to anything in a specific workspace:
zones = terraform.workspace == "south" ? [1] : null
Change "south" to whatever the name of your Terraform workspace is that needs to set the zones value.
#Mark B, you're brilliant! Such a simple answer that works. Bravo!
Here's an example of my variables.tfvars
firewalla-AvailabilityZone = {
hub-ncu = null
hub-scu = [1]
}
firewallb-AvailabilityZone = {
hub-ncu = null
hub-scu = [3]
}
Then in the resource:
zones = var.firewalla-AvailabilityZone[terraform.workspace]

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

if clauses in terraform 0.12

I am new to Terraform, using Azure...I am trying to build a module "compute" where I can deploy a single vm or a vm set.
For a single VM I need a network interface, a security group association, and a azurerm_linux_virtual_machine. For a vm set I need to provision only azurerm_linux_virtual_machine_scale_set. Is it possible to pass a boolean variable to this module to select which resources get executed?
I've checked this post but apparently there is no such thing.
Should I simply divide the module into compute/vm and compute/scale_set and actually have two modules, one for single vms and one for vm sets? Not sure if this will be a pain to maintain in the future.
Yes, you can use a boolean to select what is built. Generally speaking, you can use the count control:
resource "azurerm_linux_virtual_machine" "vm" {
count = var.single_only ? 1 : 0
... (rest of config)
}
resource "azurerm_linux_virtual_machine_scale_set" "vm_set" {
count = var.single_only ? 0 : 1
... (rest of config)
}
(see the end of this section on count resources)
Yes, it's possible and you don't need to divide the module.
As the statements in the posted link:
You can accomplish the resource creating selection by using the count parameter and conditional expression.
In this case, you can declare two bool variables like this:
variable "create_vm" {
description = "If set to true, it will create vm"
type = bool
}
variable "create_vmss" {
description = "If set to true, it will create vmss"
type = bool
}
and define the resource azurerm_linux_virtual_machine and azurerm_linux_virtual_machine_scale_set in the same VM module.
resource "azurerm_linux_virtual_machine" "example" {
count = var.create_vm ? 1 : 0
name = "example-machine"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
size = "Standard_F2"
...
resource "azurerm_linux_virtual_machine_scale_set" "example" {
count = var.create_vmss ? 1 : 0
name = "example-vmss"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku = "Standard_F2"
instances = 1
admin_username = "adminuser"
....
Then call the submodule like this,
module "vm" {
source = "./modules/vm"
create_vm = true
create_vmss = false
...
}
Hope this helps.

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.

Terraform retrieve CIDR/Prefix from existing VNETs/subnets

In Terraform, I want to create a route table for an existing subnet. To achieve the desired end result, I need to pull the CIDR/Prefix for the VNET. The VNET CIDR value is not known beforehand, the only values I know before launch is the VNET's name and Resource Group.
I would like to take the VNET CIDR/Prefix and insert it as a destination in the route table.
data "azurerm_virtual_network" "vnet" {
name = "${var.vnet_name}"
resource_group_name = "${var.vnet_rg}"
}
module "routetable" {
source = "modules/routetable"
route_table_name = "${var.route_table_name}"
resource_group_name =
"${data.azurerm_resource_group.vnet.name}"
location = "eastus"
route_prefixes = ["0.0.0.0/0", "${EXISTING_VNET_CIDR_HERE}"]
route_nexthop_types = ["VirtualAppliance", "VirtualAppliance"]
route_names = ["route1", "route2"]
}
just use data you are getting from the vnet:
${data.azurerm_virtual_network.vnet.address_spaces}
the only issue - assress_spaces is an array (i think its called list in terraforms terms).

Resources