Making Terraform Workspaces in the same project
drewmullen

drewmullen @drewmullen

About: Full-stack YAML engineer

Joined:
Oct 8, 2018

Making Terraform Workspaces in the same project

Publish Date: Jun 27
0 0

I am working on a artifact for helping TFE users provide some self service (to be unveiled soon!). One of the things I needed to do was provide a repeatable way to have a tfe workspace create workspaces in the same project. At first I thought I'd need a new data source for this since the tfe data sources require organization and at the very least.

Turns out I can do it today with some magic runtime env vars:

variable "TFC_WORKSPACE_SLUG" {}
variable "TFC_PROJECT_NAME" {}
locals {
  org_name = split("/",var.TFC_WORKSPACE_SLUG)[0]
}

data "tfe_project" "current" {
  name         = var.TFC_PROJECT_NAME
  organization = local.org_name
}

resource "tfe_workspace" "ws" {
  name         = "new-ws"
  organization = local.org_name
  project_id   = data.tfe_project.current.id
}
Enter fullscreen mode Exit fullscreen mode

Executing that code in a TFE workspace will let you create workspaces without knowing the organization name or project! Niche but pretty nifty!

Comments 0 total

    Add comment