Resolving steps.plan.outputs.* returns Empty Issue with hashicorp/setup-terraform@v3

A quick blog post to include the fix/what is needed to resolve steps.plan.outputs.* is empty when using GitHub Action hashicorp/setup-terraform@v3

I recently came across a situation while working with the hashicorp/setup-terraform@v3 GitHub Action. I wanted to make use of steps.plan.outputs.stdout to display the Terraform Plan, but I noticed that the output was consistently empty.

Even though successful Terraform plans should have output as shown below, mine was not showing anything.

When attempting to reference the output ${{ steps.plan.outputs.stdout }} it was returning as empty, Example below:

This was my current example snippet from the GitHub Action workflow:

    - name: Setup Terraform
      uses: hashicorp/setup-terraform@v3

    - name: Terraform Init
      id: init
      run: terraform init -backend-config="resource_group_name=$tf_resource_group_name" -backend-config="storage_account_name=$tf_storage_account_name" -backend-config="container_name=$tf_state_container" -backend-config="key=$tf_state_key"
      env:
        ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }}
        ARM_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
        ARM_SUBSCRIPTION_ID: ${{ secrets.SUBSCRIPTION_ID }}
        ARM_TENANT_ID: ${{ secrets.TENANT_ID }}
      working-directory: ./terraform
      shell: bash
      
    - name: Terraform Plan  
      id: plan
      run: terraform plan -no-color -var deployment_subscription_id=$DEPLOYMENT_SUBSCRIPTION_ID
      env:
        ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }}
        ARM_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
        ARM_SUBSCRIPTION_ID: ${{ secrets.SUBSCRIPTION_ID }}
        ARM_TENANT_ID: ${{ secrets.TENANT_ID }}
        DEPLOYMENT_SUBSCRIPTION_ID: ${{ secrets.SUBSCRIPTION_ID }}
      working-directory: ./terraform
      shell: bash
      continue-on-error: false

    - name: View Plan Output
      run: echo "${{ steps.plan.outputs.stdout }}"
      working-directory: ./terraform
      shell: bash

The fix

The fix was very simple, after reviewing some documentation :

This action does not configure any outputs directly. However, when you set the terraform_wrapper input to true, the following outputs are available for subsequent steps that call the terraform binary:

  • stdout – The STDOUT stream of the call to the terraform binary.
  • stderr – The STDERR stream of the call to the terraform binary.
  • exitcode – The exit code of the call to the terraform binary.
https://github.com/marketplace/actions/hashicorp-setup-terraform#outputs

The hashicorp/setup-terraform@v3 action doesn’t directly configure any outputs. However, setting the terraform_wrapper input to true makes the following outputs available for subsequent steps calling the terraform binary.

Updating the GitHub Action with terraform_wrapper = true resolved my issue:

    - name: Setup Terraform
      uses: hashicorp/setup-terraform@v3
      with:
        terraform_wrapper: true

Now reviewing the output ${{ steps.plan.outputs.stdout }} it was returning as expected:

This fix proves useful when combining with various other actions and usages!

Leave a Reply

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading