Terraform Cloud + CICD

732 views Asked by At

I am struggling to get Terraform Cloud to work remotely as I would like it to.

I have the following to work with (changing these is not an option):

  • Infrastructure code (Terraform) in a GitLab repo
  • App code in a BitBucket repo
  • CICD using BitBucket Pipelines

Terraform Cloud is used for running plan and apply. Currently manually. I want to be able to run terraform plan and apply from CICD.

I've got a terraform.tf file in the root of my app repo. It defines Terraform Cloud as my backend and provides a token.

My bitbucket pipelines is like so:

    - step:
        name: "Terraform testing"
        image: hashicorp/terraform:0.15.3
        script:
          - export TF_LOG=trace
          - export TF_WORKSPACE=testworkspace
          - terraform init
          - terraform validate
          - terraform plan

When I run this pipeline, Terraform is expecting the terraform config files to exist, but they are stored in the GitLab repo. The Terraform Workspace "testworkspace" is connected to the GitLab repo. I was hoping that when I ran terraform plan it would remotely run the workspaces plan, just as if I had clicked on it via the UI.

Do you have any suggestions regarding the above?

0

There are 0 answers