Skip to content

Integrate with TFE

We will begin writing Terraform by setting up the backend for Terraform, which will essentially store a state file that keeps tracks and manages resources created by Terraform.

To begin, navigate to main.tf in the terraform folder of your repository. For convention and easier understanding, main.tf is used as the point of entry for a set of Terraform files and defines all of the resources that will be created. The resources are typically referenced here in order to have a central location, just like a main() function in a program.

Setup the state file in Terraform Enterprise

We first need to start by defining where Terraform will store its state to keep track of what resources are being created or deleted. We'll be using a remote type state and storing it within a Terraform Enterprise (TFE) instance.

What is Terraform state? Terraform must store state about your managed infrastructure and configuration. This state is used by Terraform to map real world resources to your configuration, keep track of metadata, and to improve performance for large infrastructures. (docs)

terraform {
  backend "remote" {
    workspaces {
      name = "dojo-terraform-workshop-dev"
    }
    hostname     = "tfe.com"
    organization = "Corporate-Functions"
  }
}

Backend Configuration is required

Make sure to include backend configuration in main.tf, otherwise you'll run into Azure auth errors

Note

This block of code must be added to the main.tf file

All teams will use the tfe.com hostname. We further need to define our organization and workspace in order to store our state file. For our workshop, we will use sandbox resources under the Corporate-Functions organization and use an available dojo-* workspace.

  • Generally, each line of business has its own organization
  • Each application and environment has its own workspace (ex: app-dev, app-model, app-prod)
  • Only the Cloud Platform team has the ability to create workspaces

Important! Check with your coach to determine your workspace name (the example used in the code snippet above is dojo-terraform-workshop-dev). For a team experience, either every person should have their own workspace, or a team will need to share their workspace and not run in parallel.