Terraform Backend Config interpolation using Jenkinsfile

  1. As per terraform’s default execution steps, any configured backend must be initialized before usage. This is done by running “terraform init”.
  2. GCS backend type supports “credentials” as one of the configuration options.
$ cat backend.tf
terraform {
backend "gcs" {
credentials = var.gcp_sandbox
}
}
$ cat backend.tfvars
bucket = "terraform-demo"
prefix = "terraform/demo"
$ terraform init -backend-config "credentials=$TF_VAR_GCP_SANDBOX" -backend-config=backend.tfvars
$ cat Jenkinsfile
pipeline {
agent none
stages {
stage('Terraform Plan') {
agent { label 'terraform' }
steps {
script {
withCredentials([file(credentialsId: 'SANDBOX_CRED', variable: 'TF_VAR_GCP_SANDBOX')]) {
sh '''
terraform init -backend-config
"credentials=$TF_VAR_GCP_SANDBOX" -backend-config=backend.tfvars
terraform plan --var-file=sandbox.tfvars
'''
}
}
}
}
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ravish Kumar

Ravish Kumar

3 Followers

DevOps | Cloud Migration | Terraform | K8’s | Docker | Certified Rancher Operator | Never-Give-Up