pass output from terraform to Azure Devops Pipeline with state file in azure backend store

I cannot seem to retrieve the public ip address output of Terraform for next step in build pipeline in AzureDevops.

Terraform state pull works and outputs to json file, cannot grep on output.

Terraform state show [options] ADDRESS does not support azure backend so cannot use or grep or filter the output

also tried to store as file and read in the value.

resource "local_file" "foo" {
    content     = "foo!"
    filename = "${path.module}/foo.bar"
}

data "azurerm_public_ip" "buildserver-pip" {
  name                = "${azurerm_public_ip.buildserver-pip.name}"
  resource_group_name = "${azurerm_virtual_machine.buildserver.resource_group_name}"
}

output "public_ip_address" {
  value = "${data.azurerm_public_ip.buildserver-pip.ip_address}"
}

expect the public ip address to be passed out so can be used in ansible playbooks, bash or python script in next step

Building on @JleruOHeP answer above the following solution will automatically create a variable for every output provided by the terraform script

  1. Create a PowerShell step in your release and insert the following inline PowerShell:
$json = Get-Content $env:jsonPath | Out-String | ConvertFrom-Json

foreach($prop in $json.psobject.properties) {
    Write-Host("##vso[task.setvariable variable=$($prop.Name);]$($prop.Value.value)")
}
  1. Make sure you have provided the environment variable jsonPath like this:

Terraform has many backends that store this state file including Azure. The lab mentioned above, makes you create the storage account that will persist this state file. With these instructions, the pipeline will do it instead. Set-up. First define the following variables in your pipeline.

If I understand your question correctly, you wanted to provision something (public ip) with terraform and then have this available for further steps via a variable. All of it in a single Azure DevOps pipeline.

It can be done with a simple output and powershell script (can be inline!):

1) I assume you already use terraform task for the pipeline (https://github.com/microsoft/azure-pipelines-extensions/tree/master/Extensions/Terraform/Src/Tasks/TerraformTaskV1)

2) Another assumption that you have an output variable (from your example - you do)

3) You canspecify the output variable from this task:

4) And finally add a powershell step as the next step with the simplest script and set up its environment variable to be the $(TerraformOutput.jsonOutputVariablesPath)

$json = Get-Content $env:jsonPath | Out-String | ConvertFrom-Json

Write-Host "##vso[task.setvariable variable=MyNewIp]$($json.public_ip_address.value)"

5) ....

6) PROFIT! You have the IP address available as a pipeline variable MyNewIp now!

The file name can be anything since we haven’t yet put any state in Azure–this is where Terraform saves the state once we do. I do not need to provide any credentials inside the source code. Even if you are still on Terraform 11 and using the storage account access key, Terraform does not require hard-coded credentials.

For your purpose, I will suggest you store the terraform in Azure storage account. Then you can use the remote state in another terraform file. Here is an example:

Create public IP and store the state in Azure Storage account blob:

terraform {
    backend "azurerm" {
        storage_account_name = "yourAccountName"
        container_name       = "yourContainerName"
        key                  = "terraform.tfstate"
    }
}

resource "azurerm_public_ip" "main" {
    name            = "terraform_backend_pip"
    location        = "East US"
    resource_group_name = "yourResourceGroup"
    allocation_method = "Static"
}

# this is important, you can get the remote outputs for this
output "public_address" {
    value = "${azurerm_public_ip.main.ip_address}"
}

Quote the remote state in another Terraform file:

data "terraform_remote_state" "azure" {
        backend = "azurerm"
        config = {
                storage_account_name = "charlescloudshell"
                container_name       = "terraform"
                key                  = "terraform.tfstate"
        }
}

# the remote state outputs contain all the output that you set in the above file
output "remote_backend" {
        value = "${data.terraform_remote_state.azure.outputs.public_address}"
}

The result below:

You can follow the steps about How to store state in Azure Storage here.

Hope it helps. And if you have any more questions, please let me know. If it works for you, please accept it as the answer.

Azure Remote Backend for Terraform: we will store our Terraform state file in a remote backend location. We will need a Resource Group, Azure Storage Account and a Container.

Container name that the Terraform tfstate configuration file should reside in. There are two terms in the code for the YAML pipeline that DevOps teams should understand: Task-- The API call that Terraform makes to Azure for creating the resources. inputs-- All values that are passed in, either for the initialization or creation process.

Azure DevOps is a hosted service to deploy CI/CD pipelines and today we are going to create a pipeline to deploy a Terraform configuration using an Azure DevOps pipeline. This is an updated version…

Terraform state is used to reconcile deployed resources with Terraform configurations. State allows Terraform to know what Azure resources to add, update, or delete. By default, Terraform state is stored locally when you run the terraform apply command. This configuration isn't ideal for the following reasons: Local state doesn't work well in a

Comments
  • wondering if this may assist terraform.io/docs/providers/terraform/d/remote_state.html
  • Not clear what you want to do, if you want to get the info from a remote state? Or just want to output the public IP?
  • looking to get the Public IP address from either the output or the remote state. azure devops is not exporting the value for me
  • Charles this is what I have already and is correct, the issue is the value is never passed or retrieved within the azure devops agent , the next step is terraform output and nothing is returned, believe this may be an issue with azure devops and terraform plugin
  • @DeclanG It shows that the remote state just gives in its outputs if you set the output when you create the resource. Even if the output cannot display in the DevOps.