I have created resource, network and compute module in terraform, now want to pass output of vm_id to site recovery module here are the files I am using currently.
Continue to subject: in resource "azurerm_site_recovery_replicated_vm" "vm-replication": source_vm_id= module.compute.vm_id
This is directory structure that I am following currently,
.
├── main.tf
└── modules
├── compute
│ ├── main.tf
│ ├── outputs.tf_bk
│ ├── variable.tf
│ └── variable.tfvars
├── network
│ ├── main.tf
│ ├── variable.tf
│ └── variable.tfvars
├── resource
│ ├── main.tf
│ ├── variable.tf
│ └── variable.tfvars
└── site_recovery
├── main.tf
├── variable.tf
└── variable.tfvars
root module main.cf file:
#Select provider
provider "azurerm" {
subscription_id = "xxxxxxxxxxxxxxxxxxxxxxxx"
version = "~> 2.4"
features {}
}
module "resource" {
source = "./modules/resource"
resource_group_name = "devops_primary"
location = "southeastasia"
}
module "network" {
source = "./modules/network"
virtual_network = "primaryvnet"
subnet = "primarysubnet"
address_space = "192.168.0.0/16"
address_prefix = "192.168.1.0/24"
public_ip = "backendvmpip"
location = "southeastasia"
primary_nic = "backendvmnic"
primary_ip_conf = "backendvm"
resource_group_name = "module.resource.primary_group_name"
}
module "compute" {
source = "./modules/compute"
#resource_group_name = "devops_primary"
#location = "southeastasia"
vm_name = "backendvm-primary"
vm_size = "standard_d2s_v3"
vm_storage_od_disk_name = "backend-vm-os-disk-primary"
computer_name = "backendserver"
username = "terraform"
ssh_key_path = "/home/terraform/.ssh/authorized_keys"
keys_data = "~/.ssh/id_rsa.pub"
sa_name = "primarysa"
disk_name = "backenddisk_primary"
}
module "site_recovery" {
source = "./modules/site_recovery"
#resource_group_name = "devops_primary"
#location = "southeastasia"
sec_resource_group = "devops_secondary"
recovery_vault_name = "recovery-vault"
primary_fabric = "devops_primary-fabric"
seconday_fabric = "devops_secondary-fabric"
primary_container = "primary-protection-container"
secondary_container = "secondary-protection-container"
policy_name = "policy"
container_mapping = "container-mapping"
replicated_vm = "backendvm-replication"
}
compute main.cf :
#Create VM in Primary resource
resource "azurerm_virtual_machine" "primary" {
name = "var.vm_name"
location = "module.resource.azurerm_resource_group.primary.location"
resource_group_name = "module.resource.azurerm_resource_group.primary.name"
vm_size = "var.vm_size"
network_interface_ids = ["module.resource.azurerm_network_interface.primary.id"]
storage_os_disk {
name = "var.vm_storage_od_disk_name"
os_type = "Linux"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"
}
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
os_profile {
computer_name = "var.computer_name"
admin_username = "var.username"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/terraform/.ssh/authorized_keys"
key_data = file("~/.ssh/id_rsa.pub")
}
}
tags = {
environment = "Test"
}
output "vm_ids" {
description = "Virtual machine ids created."
value = azurerm_virtual_machine.primary.id
#depends_on = [azurerm_virtual_machine.primary.primary]
}
site recovery main.cf
#Create Site Recovery Replicated VM
resource "azurerm_site_recovery_replicated_vm" "vm-replication" {
name = var.replicated_vm
resource_group_name = azurerm_resource_group.secondary.name
recovery_vault_name = azurerm_recovery_services_vault.vault.name
source_recovery_fabric_name = azurerm_site_recovery_fabric.primary.name
#source_vm_id = site recovery main.cf
#Create Site Recovery Replicated VM
resource "azurerm_site_recovery_replicated_vm" "vm-replication" {
name = var.replicated_vm
resource_group_name = azurerm_resource_group.secondary.name
recovery_vault_name = azurerm_recovery_services_vault.vault.name
source_recovery_fabric_name = azurerm_site_recovery_fabric.primary.name
#source_vm_id = "module.compute.azurerm_virtual_machine.primary.id"
source_vm_id = module.compute.vm_ids
recovery_replication_policy_id = azurerm_site_recovery_replication_policy.policy.id
source_recovery_protection_container_name = azurerm_site_recovery_protection_container.primary.name
target_resource_group_id = azurerm_resource_group.secondary.id
target_recovery_fabric_id = azurerm_site_recovery_fabric.secondary.id
target_recovery_protection_container_id = azurerm_site_recovery_protection_container.secondary.id
managed_disk {
disk_id = "[module.resource.azurerm_virtual_machine.primary.storage_os_disk[0].managed_disk_id]"
staging_storage_account_id = "module.resource.azurerm_storage_account.primary.id"
target_resource_group_id = azurerm_resource_group.secondary.id
target_disk_type = "Premium_LRS"
target_replica_disk_type = "Premium_LRS"
}
managed_disk {
disk_id = "[module.resource.azurerm_managed_disk.primary.id]"
staging_storage_account_id = "[module.resource.azurerm_storage_account.primary.id]"
target_resource_group_id = azurerm_resource_group.secondary.id
target_disk_type = "Premium_LRS"
target_replica_disk_type = "Premium_LRS"
}
depends_on = ["module.compute.vm_ids"]
}
Used depends_on for input to site_recovery module, again will you please suggest, how can I output managed disks ids and Os disks ids from compute module and use input in site recovery module.