Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for specifying target load balancer for a replicated VM #7237

Open
martenbohlin opened this issue Jun 5, 2020 · 1 comment
Open

Comments

@martenbohlin
Copy link
Contributor

martenbohlin commented Jun 5, 2020

Community Note

  • Please vote on this issue by adding a 馃憤 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

Currently it is not specify that a azurerm_site_recovery_replicated_vm should be part of a load-balancer backend pool in the secondary region. This makes it impossible to fully automate the setup of a environment with site recovery.

New or Affected Resource(s)

  • azurerm_site_recovery_replicated_vm

Potential Terraform Configuration

resource azurerm_site_recovery_replicated_vm "example" {
 ....
 network_interface {
   source_network_interface_id = azurerm_network_interface.example.id
   target_subnet_name               = azurerm_subnet.example.name
   target_loadbalancer_backend_pool_id = azurerm_lb_backend_address_pool.example.id
 }
}

References

@DazButterworth
Copy link

DazButterworth commented Oct 14, 2022

Since this useful request above hasn't been getting any love or attention, I created a PowerShell script to apply after my Terraform deployment (better than nothing, I guess) and I thought I'd share with others who arrived here for the same reason I did.

It's not that refined, but will hopefully help as a starting point, if you need to add target Load Balancers to newly Terraform-deployed Replicated VMs.

There are a few assumptions (eg. that you are logged in and in correct subscription context) and you will need to adapt how the $recoveryLBBackendAddressPoolId variable is derived (or you could even explicitly define it, depending on your circumstances). We use similarly named LBs and Resource Groups across our regions so only the region code differs in our case - hence the use of the "Replace" function against our Source VM's LB ID.

Anyway, hope it helps someone...

# Set Variables
$vmName = "VM Name"
$rsvName = "Name of your Recovery Services Vault"
$rsvResourceGroupName "Name of the Resource Group for your Recovery Services Vault"
$sourceRegion = "eg. 'West Europe'"
$sourceRegionShort = "Short code used for source region - for use in resource names"
$targetRegionShort = "Short code used for target region - for use in resource names"

# Get VM details
Write-Host "Getting VM Details for $vmName..." -ForegroundColor Green
$vm = $null
$vm = Get-AzVM -ErrorAction Stop | Where-Object {$_.Name.ToLower() -eq $vmName.ToLower()}
if (!$vm) {
    Write-Host "Failed to find VM - Stopping Script" -ForegroundColor Red
    Exit
}

# Get Load Balancer & Backend Pool Config
$nicName = ($vm.NetworkProfile.NetworkInterfaces.id).split('/')[-1]
$nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -name $nicName
$vmPrimaryIPConfig = $nic.IpConfigurations | Where-Object Primary -eq True
$vmLoadBalancerBackendID = $vmPrimaryIPConfig.LoadBalancerBackendAddressPools[0].Id

#### you may want to change this next line to suit your needs ####
$recoveryLBBackendAddressPoolId = $vmLoadBalancerBackendID.replace($sourceRegionShort,$targetRegionShort)

# Get ASR Vault details
Write-Host "Getting Recovery Services Vault Details..." -ForegroundColor Green
$vault = Get-AzRecoveryServicesVault -Name $rsvName -ResourceGroupName $rsvResourceGroupName
Set-AsrVaultSettings -Vault $vault | Out-Null
$sourceFabric = get-AzRecoveryServicesAsrFabric -FriendlyName $sourceRegion
$container = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $sourceFabric

# Get Replicated Item
Write-Host "Getting Replicated Item Details..." -ForegroundColor Green
$asrReplicatedItem = Get-AzRecoveryServicesAsrReplicationProtectedItem -FriendlyName $vmName -ProtectionContainer $container -ErrorAction Stop
$asrNicId = $asrReplicatedItem.NicDetailsList[0].NicId
$asrIpConfig = $asrReplicatedItem.NicDetailsList[0].IpConfigs[0]
$asrRecoveryVMNetworkId = $asrReplicatedItem.NicDetailsList[0].RecoveryVMNetworkId

Write-Host "Setting ASR IP Configuration..." -ForegroundColor Green
$asrIpConfigs = @(New-AzRecoveryServicesAsrVMNicIPConfig `
    -IpConfigName $asrIpConfig.Name `
    -RecoverySubnetName $asrIpConfig.RecoverySubnetName `
    -RecoveryStaticIPAddress $asrIpConfig.RecoveryStaticIPAddress `
    -RecoveryLBBackendAddressPoolId $recoveryLBBackendAddressPoolId `
    -IsSelectedForFailover)

Write-Host "Setting ASR NIC Configuration..." -ForegroundColor Green
$asrNicConfig = New-AzRecoveryServicesAsrVMNicConfig `
    -NicId $asrNicId `
    -ReplicationProtectedItem $asrReplicatedItem `
    -RecoveryVMNetworkId $asrRecoveryVMNetworkId `
    -EnableAcceleratedNetworkingOnRecovery `
    -IPConfig $asrIpConfigs

Write-Host "Setting Replicated Item Configuration..." -ForegroundColor Green
Set-AzRecoveryServicesAsrReplicationProtectedItem -InputObject $asrReplicatedItem -ASRVMNicConfiguration $asrNicConfig

# Check Site Recovery Jobs in the Portal to establish success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants