function Get-LocalNlbIp { <# .SYNOPSIS Gets the Ip for the NLB NIC which is in the same AZ as the server from which it's run. .DESCRIPTION Gets the Ip for the NLB NIC which is in the same AZ as the server from which it's run. Uses the current availability zone, ENI description, and interfacetype to determine the appropriate IP .EXAMPLE Get-LocalNlbIp -verbose VERBOSE: [Get-LocalNlbIp] : Current Instance AZ Read as us-east-1b VERBOSE: [Get-LocalNlbIp] : Environment Read as qa [Get-DesignationTagNameByEnvironment] : Checking designation value for environment qa VERBOSE: [Get-LocalNlbIp] : Read designation tag value Smith VERBOSE: [Get-LocalNlbIp] : Using Expected NLB Name ELB net/Smith-qa-nlb for Filtering VERBOSE: Invoking Amazon Elastic Compute Cloud operation 'DescribeNetworkInterfaces' in region 'us-east-1' Returning IP Address for ENI with Description: ELB net/smith-qa-nlb/93947386b64a5aac, Id: eni-0718dc98cdcec5e18 10.26.91.212 #> [CmdletBinding()] param() $logLead = (Get-LogLeadName) Import-AWSModule # EC2 if (!(Test-IsAws)) { Write-Warning "$logLead : This function can only be executed on an AWS server" return } # Get the current instance and AZ $currentInstance = Get-CurrentInstance; $currentAz = $currentInstance.Placement.AvailabilityZone; Write-Verbose "$logLead : Current Instance AZ Read as $currentAz" # Check the current server's role $serverRole = $currentInstance.Tag | Where-Object {$_.Key -eq $Global:AlkamiTagKeyRole} if ($serverRole.Value -eq 'app:app') { # App servers should use 127.0.0.1 Write-Warning "This is currently running on an app server. The IP returned shouldn't be used in the host file." } # Get the expected designation tag name $environment = $currentInstance.Tag | Where-Object { $_.Key -eq $Global:AlkamiTagKeyEnvironment; }; Write-Verbose "$logLead : Environment Read as $($environment.Value)" $targetTag = Get-DesignationTagNameByEnvironment $environment.Value if ($null -ne $targetTag) { # Pull the Designation Tag Value $environmentTagValue = $currentInstance.Tag | Where-Object {$_.Key -eq "alk:$targetTag" } Write-Verbose "$logLead : Read designation tag value $($environmentTagValue.Value)" } else { Write-Warning "$logLead : Unable to pull $Global:AlkamiTagKeyEnvironment for the current instance. Execution cannot continue." return $null; } $cleanedName = $environmentTagValue.Value.replace('.','-'); $nlbName = "ELB net/" + $cleanedName + '-' + $environment.Value + '-nlb'; Write-Verbose "$logLead : Using Expected NLB Name $nlbName for Filtering" $nlbNics = Get-EC2NetworkInterface -Filter @( @{name='availability-zone';values=$currentAz} ); [array]$filteredNics = $nlbNics | Where-Object { $_.InterfaceType -eq 'network_load_balancer' -and $_.Description -match $nlbName} $matchCount = $filteredNics.Count Write-Verbose "$logLead : Found $matchCount Matching ENIs with InterfaceType: network_load_balancer, Description: $nlbName, Availability Zone $currentAz" if ($null -ne $filteredNics -and $filteredNics.Count -eq 1) { $nic = $filteredNics | Select-Object -First 1 Write-Host ("Returning IP Address for ENI with Description: {0}, Id: {1}" -f $nic.Description, $nic.NetworkInterfaceId) return (($nic | Select-Object -First 1).PrivateIpAddress); } if ($null -eq $filteredNics) { Write-Warning "$logLead : No ENIs found with Description $nlbName for AZ $currentAz" return $null } Write-Warning ("$logLead : {0} ENIs found with Description $nlbName for AZ $currentAz. Execution cannot continue." -f $filteredNics.Count) return $null }