Home > Blockchain >  How to manipulate remote Terraform state files on Azure Blob storage
How to manipulate remote Terraform state files on Azure Blob storage

Time:12-02

I'm working with a subscription that has a few different deployed environments (dev, test, staging, etc.). Each environment has its own storage account, containing an associated Terraform state file. These environments get deployed via Azure DevOps Pipelines.

It's easy enough to get at the .tfstate files that have been created this way, through the portal, CLI, etc.

But is it possible to access these state files using the 'terraform state' commands, for example using Azure Cloud Shell? If so, how do you point them at the right location?

I've tried using the terraform state commands in a Cloud Shell, but it's not clear how to point them to the right location or if this is indeed possible.

CodePudding user response:

For these requirement, you need AzurePowerShell task to achieve your requirement.

1, First, if you can achieve your requirement via powershell feature in azure portal, then it is possible using the AzurePowerShell task to achieve the same thing(AzurePowerShell is running on the agent based on the service connection/service principal you provided.).

- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'testbowman_in_AAD' #This service connection related to service principal on Azure side.
    ScriptType: 'InlineScript'
    Inline: |
      # Put your logic here.
      # Put your logic here.
    azurePowerShellVersion: 'LatestVersion'

2, Second, you can use AzCopy to download the file and then do operations to it. DevOps microsoft host agent support this tool.

CodePudding user response:

running this command : terraform state pull > state.tfstate (you can give like thils dev.tfstate extension tfstate is important)in the Azure cloud shell.

All you need to move to the terraform file directory enter image description here

and run this command terraform state pull > dev.tfstate

enter image description here

  • Related