how can i get a list of azure vm ids in terraform and then read them back one value at a time? - azure

I need to get a list of all VM ids in an Azure subscription using Terraform then read them back one by one and feed one id at a time to a module to perform some tasks on it, how can i do this?

You can use the feature multiple instances of the data source to get the list of all the VM ids. See the data source azurerm_virtual_machine, it requires the name and the resource group name. So if the VMs in the same group, you just need to create a list variable for all the VM names, then the data block will be like this:
variable "vm_names" {
type = list(string)
default = [
...
]
}
data "azurerm_virtual_machine" "example" {
count = length(var.vm_names)
name = element(var.vm_names, count.index)
resource_group_name = var.resource_group_name
}
output "vm_ids" {
value = data.azurerm_virtual_machine.example.*.id
}
The output is all the VM ids.

Related

Dynamic data source in Terraform 12

I'm creating alerts (azurerm_monitor_scheduled_query_rules_alert) in Azure using Terraform. You can include a list of action groups (i.e. the groups that you send the alerts to).
Within the TFVars file I will pass in a variable value of a list of the names of the action groups. However, the alert module needs the ID's of the resources, not the names. So I have a data source that would get the info of an action group. The Alert resource can then refer to the data source to acquire the azure resource id.
This works fine if I have just one action group, but the size of the list with action group names can vary. I'm trying to figure out how I can convert all action group names to id for ingestion by the resource.
resource "azurerm_monitor_scheduled_query_rules_alert" "tfTestAlertExample" {
for_each = {for alert in var.scheduled_query_alerts : alert.name => alert}
name = each.value["name"]
location = data.azurerm_resource_group.resource_group.location
resource_group_name = data.azurerm_resource_group.resource_group.name
action {
# --This part here. How do I get make this dynamic?--
action_group = [
data.azurerm_monitor_action_group.action_group.id
]
email_subject = each.value["email_subject"]
custom_webhook_payload = "{}"
}
data_source_id = ................ etc
So in the above example, there will only be one action{} block, but the Action_group list within that needs to be dynamic, with ID's retrieved from a data source. Or maybe there's another way of doing this that I've not considered.
Any help would be greatly appreciated.
If you just want to convert the list of action group names to its Ids, you can do it like this:
# declare the variables
variable "action_group_names" {
default = ["nancyAG1","nancyAG2"]
}
# retrieve the Id of action group
data "azurerm_monitor_action_group" "example" {
count = length(var.action_group_names)
resource_group_name = "existingRG"
name = element(var.action_group_names,count.index)
}
# output the result to the terminal
output "groups_id" {
value = data.azurerm_monitor_action_group.example[*].id
}
Then pass Ids to the resource like this:
resource "azurerm_monitor_scheduled_query_rules_alert" "example" {
name = format("%s-queryrule", var.prefix)
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
action {
action_group = data.azurerm_monitor_action_group.example[*].id
email_subject = "Email Header"
custom_webhook_payload = "{}"
}
Check the action_group Ids.

proper way to use nested variables in terraform

In my terraform script, I have
resource "azuread_application" "main" {
count = "${length(var.sp_names)}"
name = "${sp_prefix}-${var.sp_names[count.index]}"
available_to_other_tenants = false
}
resource "azuread_service_principal" "main" {
count = "${length(var.sp_names)}"
application_id = "${azuread_application.main.["${sp_prefix}"-"${var.sp_names[count.index]}"].application_id}"
}
when I ran terraform init I get the following error:
An attribute name is required after a dot.
what is the right way to use nested variables and a list object?
In order for a resource to be represented as a map of instances rather than a list of instances, you need to use for_each instead of count:
resource "azuread_application" "main" {
for_each = { for n in var.sp_names : n => "${var.sp_prefix}-${n}" }
name = each.value
available_to_other_tenants = false
}
The for_each expression above is a for expression that transforms your list or set of names into a mapping from the given names to the prefixed names. In the other expressions in that block, each.key would therefore produce the original given name and each.value the prefixed name.
You can then similarly use for_each to declare the intent "create one service principal per application" by using the application resource's map itself as the for_each expression for the service principal resource:
resource "azuread_service_principal" "main" {
for_each = azuread_application.main
application_id = each.value.application_id
}
In this case, the azuread_application.main value is a map from unprefixed names to objects representing each of the declared applications. Therefore each.key in this block is the unprefixed name again, but each.value is the corresponding application object from which we can access the application_id value.
If your var.sp_names had a string "example" in it, then Terraform would interpret the above as a request to create two objects named azuread_application.main["example"] and azuread_service_principal.main["example"], identifying these instances by the var.sp_names values. This is different to count where the instances would have addresses like azuread_application.main[0] and azuread_service_principal.main[0]. By using for_each, we ensure that adding and removing items from var.sp_names will add and remove corresponding instances from those resources, rather than updating existing ones that happen to share the same numeric indices.
I am assuming you are using a version older that 0.12.x. If not the answer from Martin is the best one.
You need to leverage the splatting.
resource "azuread_service_principal" "main" {
count = "${length(var.sp_names)}"
application_id = "${azuread_application.main.*.application_id}"
}

How to add a resource using the same module?

Terraform newbie here. I've a module which creates an instance in GCP. I'm using variables and terraform.tfvars to initialize them. I created one instance successfully - say instance-1. But when I modify the .tfvars file to create a second instance (in addition to the first), it says it has to destroy the first instance. How can I run the module to 'add' an instance, instead of 'replacing the instance'? I know the first instance which was created is in terraform.tfstate. But that doesn't explain the inability to 'add' an instance.
Maybe I'm wrong, but I'm looking at 'modules' (and its config files) as functions- such that I can call them anytime with different parameters. That does not appear to be the case.
Terraform will try to maintain the deployed resources matching your resources definition.
If you want two instances at the same time, then you should describe them both in your .tf file.
Ex. same instances, add a count to your definition
resource "some_resource" "example" {
count = 2
name = "example-${count.index}"
}
Ex. two different resources with specific values
resource "some_resource" "example-1" {
name = "example-1"
size = "small"
}
resource "some_resource" "example-2" {
name = "example-2"
size = "big"
}
Better you can set the specific values in tfvars for each resource
resource "some_resource" "example" {
count = 2
name = "example-${count.index}"
size = ${vars.mysize[count.index]}
}
variable mysize {}
with tfvars file:
mysize = ["small", "big"]

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 route53 resolver setup

Just been trying to use the new terraform aws_route53_resolver_endpoint resource. It takes the subnet ids as a block type list. Unfortunately there appears to be no way to populate this from a list of subnets read from an output variable from the previous step.
Basically I have a set of subnets created using the count on the subnet resources in a previous step. Im trying to use these and setup aws_route53_resolver_endpoint in each of these subnets:
resource "null_resource" "management_subnet_list" {
count = "${length(var.subnet_ids)}"
triggers {
subnet_id = "${element(data.terraform_remote_state.app_network.management_subnet_ids, count.index)}"
}
}
resource "aws_route53_resolver_endpoint" "dns_endpoint" {
name = "${var.environment_name}-${var.network_env}-dns"
direction = "OUTBOUND"
security_group_ids = ["${var.security_groups}"]
ip_address = "${null_resource.management_subnet_list.*.triggers}"
}
The above when run, results in an error: ip_address: should be a list
If I modify the code as follow:
ip_address = ["${null_resource.management_subnet_list.*.triggers}"]
I get the error: ip_address: attribute supports 2 item as a minimum, config has 1 declared
I can't seem to figure out any other way to create the resource list dynamically from a list of subnets.
Any help will be appreciated.
Per the resource reference for aws_route53_resolver_endpoint, the subnet_id in the ip_address block is a single string value.
To specify multiple subnets, you need to have multiple ip_address blocks.
Since you state that you're creating subnets with a count argument, you could potentially reference each individually with the index like: aws_subnet.main[0].id, aws_subnet.main[1].id and so on, each in it's own ip_address block. (or for Terraform 0.11, I think it was "${aws_subnet.main.0.id}".)
However, a better way would be to use the Dynamic Blocks available in Terraform 0.12 +
Dynamic Blocks allow you to create repeatable nested blocks within top-level blocks.(resource, data, provider, and provisioner blocks currently support dynamic blocks).
A dynamic ip_address block within the aws_route53_resolver_endpoint resource could look like:
dynamic "ip_address" {
for_each = aws_subnet.main[*].id
iterator = subnet
content {
subnet_id = subnet.value
}
}
Which would result in a separate ip_address nested block for each subnet created in the aws_subnet.main resource.
The for_each argument is the complex value to iterate over. It accepts accepts any collection or structural value, typically a list or map with one element per desired nested block.
For complete info on the dynamic nested block expression, see the Terraform documentation at: https://www.terraform.io/docs/language/expressions/dynamic-blocks.html

Resources