Sebastian Balle
Sebastian Balle

Reputation: 61

Interact with Terraform resources behind firewall on Azure

This challenge is regarding Azure and Azure DevOps but I would imagine this happening on similar platforms (AWS, GCP, Github, Gitlab, etc.)

I am currently using Azure DevOps Pipelines but I am facing a problem with interacting with resources behind firewalls (either IP restricted or virtual network restricted). As Azure Pipeline spins up a new VM it requires me to whitelist that given public IP for that newly spun up machine each time I do a run. It is very janky to accommodate for this whitelisting as I am creating Azure Pipelines as submodules for reproducibility purpose extending templates from one project and using it in multiple. Terraform state needs to access configurations on restricted resources, hence throwing access denied messages.

I have looked into the following to solve the challenge and my thoughts about them:

What are your thoughts on solving this challenge?

Upvotes: 0

Views: 2161

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30383

You can use scripts to get the ip of the cloud agents. And dynamically whitelist the ip address for your azure storage account using Azure PowerShel or Azure Cli. See below example:

1, Add Azure Powershell task before Terraform task in your azure devops pipeline to get the agent's ip address and add whitelist for azure storage account.

- task: AzurePowerShell@5
  displayName: 'Azure PowerShell script: InlineScript copy'
  inputs:
    azureSubscription: 'Microsoft-Azure'
    ScriptType: InlineScript
    Inline: |
     $ip = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip #get agent ip
     #add ip to whitelist
     Add-AzStorageAccountNetworkRule -ResourceGroupName "myresourcegroup" -AccountName "mystorageaccount" -IPAddressOrRange $ip

    azurePowerShellVersion: LatestVersion

2, Add another azure powershell task at the end of your pipeline to remove the whitelist.

- task: AzurePowerShell@5
  displayName: 'Azure PowerShell script: InlineScript copy'
  inputs:
    azureSubscription: 'Microsoft-Azure'
    ScriptType: InlineScript
    Inline: |
     $ip = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
     
     Remove-AzStorageAccountNetworkRule -ResourceGroupName "myresourcegroup" -AccountName "mystorageaccount" -IPAddressOrRange $ip

    azurePowerShellVersion: LatestVersion

Check document here for more information.

The IP ranges for cloud agents changes weekly. You can also check the weekly file and update the whitelist ip address manually. Check here for more information.

Upvotes: 1

Related Questions