skip to Main Content

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.

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search