ljfranklin / terraform-resource

A concourse resource to create infrastructure via Terraform
MIT License
185 stars 85 forks source link

var_files not being used during terraform plan #147

Closed ralphpi closed 3 years ago

ralphpi commented 3 years ago

I have a task that generates a file json file variables.tfvars as output in vars/

I have the - put: terraform with var_files: set. Below is the job.

This works locally for me when ran with the --var-file parameter. But when ran in this job it fails with image

I verified the file is there in vars/ with the same content as the file that works locally. What am I not possibly understanding here ? image

variables.tfvars

{
  "regions": [
    {
      "env": "prod",
      "firewall": {
        "instance_group_type": "unmanaged",
        "machine_type": "n1-standard-8",
        "nodecount": 2
      },
      "name": "us-east1",
      "networking": {
        "vpcs": [
          {
            "cidr": "0.0.0.0/0",
            "vpc": "untrust"
          },
          {
            "cidr": "0.0.0.0/0",
            "vpc": "resource"
          },
          {
            "cidr": "0.0.0.0/0",
            "vpc": "gs"
          },
          {
            "cidr": "0.0.0.0/0",
            "vpc": "mgmt"
          }
        ]
      }
    }
  ]
}

task

platform: linux
image_resource:
  type: registry-image
  source: {repository: golang}
outputs:
  - name: vars
inputs:
  - name: gcp-region-config
params:
  REGION_CONFIG_PATH: gcp-region-config/gcp
run:
  path: /bin/sh
  args:
    - -cx
    - |
      chmod +x ./gcp-region-config/ci/scripts/convert-merge.sh
      ./gcp-region-config/ci/scripts/convert-merge.sh

Job


- name: terraform-plan
  plan:
  - get: gcp-region-config
    trigger: true
  - task: gcp-auth
    config:
    file: gcp-region-config/ci/tasks/gcp-auth.yml
    params:
      CREDS_CONTENT: ((gcp-json-key))
  - task: convert-merge
    file: gcp-region-config/ci/tasks/convert-merge.yml
  - put: terraform
    inputs: all
    params:
      env_name: production
      terraform_source: gcp-region-config/ci/tf/gcp
      plan_only: true
      var_files:
         - vars/variables.tfvars
    on_success:
      put: notify
      params:
        alert_type: success
    on_failure:
      put: notify
      params:
        alert_type: failed
ralphpi commented 3 years ago

Fixed typos, added error

ralphpi commented 3 years ago

setting the json file extension in the output as well as the var_files seems to resolve my problem. Readme states that it must end in .tfvars but it does in fact accept double extensions.

file_name: variables.tfvars.json


params:
      env_name: production
      terraform_source: gcp-region-config/ci/tf/gcp
      plan_only: true
      var_files:
         - vars/variables.tfvars.json
ljfranklin commented 3 years ago

Oh, yeah the wording in the README is unfortunately ambiguous. What I meant was if the filename ends in .tfvars then that file will be parsed as HCL, otherwise it will be parsed as YAML/JSON. Updated the wording to hopefully make that more clear: https://github.com/ljfranklin/terraform-resource/commit/5cb79e0850105c04d96387f66888401bd497676a. Closing this out as it looks like you got it figured out, but feel free to re-open if there's more to discuss.

ralphpi commented 3 years ago

Thanks!