Skip to content

Instantly share code, notes, and snippets.

@xbalajipge
Last active November 20, 2025 07:03
Show Gist options
  • Select an option

  • Save xbalajipge/62668f3c1aa895774995ba1ae67f2840 to your computer and use it in GitHub Desktop.

Select an option

Save xbalajipge/62668f3c1aa895774995ba1ae67f2840 to your computer and use it in GitHub Desktop.
terraform-tfe-modules.md

xbalajipge: Describe the design of the terraform code in tfe/modules/{projects,teams,workspaces} folder; give a high level over view as bullet points, suitable for presentationn

Workspace: Collecting workspace information# High-Level Design Overview: TFE Modules

response

Based on the Terraform code in tfe/modules/{projects,teams,workspaces}, here's a presentation-ready overview:


Module Architecture

Three Core Modules

  • tfe/modules/projects - Project management
  • tfe/modules/teams - Team management
  • tfe/modules/workspaces - Workspace management

Projects Module (tfe/modules/projects)

Purpose

  • Creates and manages Terraform Cloud (TFC) projects
  • Organizes workspaces under logical project boundaries

Key Features

  • Simple project resource creation via tfe_project
  • Sub-module: project-access for managing team permissions
  • Supports read-only access for AD groups
  • Returns project ID and metadata for downstream dependencies

Key Resources

  • tfe_project.this - Main project resource
  • tfe_team_project_access.read - Team access control

Teams Module (tfe/modules/teams)

Purpose

  • Provisions TFC teams aligned with Active Directory groups
  • Manages organization-level permissions

Key Features

  • Creates teams with tfe_team resource
  • Configurable permissions via properties variable:
    • Workspace management
    • Policy management
    • VCS settings
    • Module/provider management
    • Project access
  • Sub-module: team-access for workspace-level permissions

Team Access Levels

  • Read-only: View workspaces and runs
  • Apply: Execute runs, manage variables, write state

Workspaces Module (tfe/modules/workspaces)

Purpose

  • Core module for creating and configuring TFC workspaces
  • Manages workspace settings, variables, and integrations

Key Features

Workspace Configuration

  • VCS integration (GitHub repo connection)
  • Terraform version management
  • Working directory specification
  • Tag-based organization

Variable Management

  • Terraform variables (tfe_variable.tf_vars)
  • Environment variables (tfe_variable.env_vars)
  • Auto-injection of workspace metadata (ID, name)
  • Cloud provider-specific variables (AWS account, Azure subscription)

Security & Compliance

  • Policy set attachments (tfe_workspace_policy_set)
  • Run task integration (e.g., Prisma Cloud scanning)
  • Drift detection support
  • Auto-destroy scheduling

Execution Settings

  • Execution mode (remote/agent-based)
  • Auto-apply configuration
  • Queue behavior
  • File trigger patterns

Common Design Patterns

Multi-Cloud Support

  • AWS-specific variables (account_num)
  • Azure-specific variables (arm_subscription_id, arm_tenant_id)
  • Conditional logic based on csp variable

Lifecycle Management

  • Ignore changes on queue_all_runs and tag_names (managed by OIDC lambda)
  • Preconditions for required cloud provider variables

Tagging Strategy

  • Lowercase enforcement for tags
  • Environment-based tagging
  • Application ID tracking

Workspace Settings

  • Separate resource (tfe_workspace_settings) for execution mode
  • Environment-specific run task enforcement (advisory for dev/test, mandatory for qa/prod)

Integration Points

Version Requirements

  • Terraform >= 1.6.1
  • TFE provider >= 0.49.2

External Dependencies

  • app.terraform.io/pgetech/utils/aws//modules/workspace-info - Workspace metadata
  • Active Directory groups for team membership
  • VCS OAuth tokens for repository access

Outputs

  • Workspace ID, name, and full object
  • Team IDs and metadata
  • Project IDs and configurations
  • Variable sets for downstream consumption

Usage Pattern

# Example from tfe/modules/tfc-workspace
module "workspaces" {
  source       = "./modules/workspaces"
  name         = "my-workspace"
  organization = "pgetech"
  vcs_repo     = { identifier = "org/repo", branch = "main" }
  tf_vars      = { key = "value" }
  project_name = "my-project"
}

Key Takeaways

Modular Design - Separation of concerns (projects, teams, workspaces)
Enterprise-Ready - Multi-cloud, policy enforcement, drift detection
Security-First - Team access controls, run task integration
Automation-Friendly - Variable injection, OIDC integration, metadata tracking
Scalable - Supports hundreds of workspaces via YAML-driven configuration

xbalajipge: Combine the above content for a unified design solution with

Terraform on Terraform: Automated TFC Management

Implemented approach uses Terraform to manage Terraform Cloud (TFC) resources programmatically.
Automates lifecycle of Projects, Teams, and Workspaces.

Two-Layer Architecture

Foundational Layer
    Creates Projects for workspace grouping.
    Creates Teams linked to AD groups for SSO.
    Assigns teams to projects with high-level permissions.
Workspace Layer
    Handles individual workspaces and their permissions.

Dynamic Workspace Provisioning

Workspace definitions sourced from external data files.
Loop-driven process creates hundreds of workspaces from a reusable template.
Module configures VCS repo, Terraform version, and auto-apply settings.

Granular Access Control

Dedicated module assigns AD groups to read-only and apply roles per workspace.

Workflow

Workspace requirements defined in a simple format.
Orchestration converts requirements into Terraform-ready data.
Foundational Layer ensures projects and teams exist.
Workspace Layer creates/updates workspaces and applies permissions.

Terraform-Managed TFC Infrastructure - Unified Design Solution

response


Overview: Terraform on Terraform

Core Concept

  • Self-Service Infrastructure: Uses Terraform to manage Terraform Cloud (TFC) resources programmatically
  • Automation-First: Eliminates manual workspace creation and permission management
  • Scalable Architecture: Supports hundreds of workspaces through declarative configuration

Architecture: Two-Layer Design

Layer 1: Foundational Infrastructure

Projects → Teams → Project Access

Components

  • projects

    • Logical grouping containers for workspaces
    • Namespace isolation for different applications/teams
    • Foundation for permission inheritance
  • teams

    • AD group integration via SSO
    • Organization-level permissions (workspace admin, policy management)
    • Reusable security principals across projects
  • Project-Level Access

    • High-level team-to-project assignments
    • Read-only access for AD groups (e.g., viewers, auditors)
    • Establishes baseline permissions before workspace creation

Layer 2: Workspace Infrastructure

Workspaces → Variables → Run Tasks → Team Access

Components

  • workspaces

    • Individual workspace provisioning
    • VCS integration (GitHub repository connection)
    • Terraform version pinning and working directory specification
  • Variable Management

    • Terraform variables (application-specific)
    • Environment variables (credentials, settings)
    • Auto-injected metadata (workspace ID, cloud provider accounts)
  • Security & Compliance

    • Policy set attachments (Sentinel policies)
    • Run task integration (Prisma Cloud, cost estimation)
    • Drift detection and auto-destroy scheduling
  • Workspace-Level Access

    • Granular AD group permissions per workspace
    • Read-Only: View runs and state
    • Apply: Execute runs, manage variables, write state

Dynamic Provisioning Model

Data-Driven Workflow

YAML/JSON Definitions → Orchestration → Terraform Modules → TFC Resources

Step 1: Define Requirements

# workspace-definitions.yaml
workspaces:
  - name: "app-prod-network"
    project: "networking"
    vcs_repo: "pgetech/terraform-network"
    branch: "main"
    read_teams: ["network-viewers"]
    apply_teams: ["network-admins"]

Step 2: Orchestration Layer

  • External data processing (Lambda, GitHub Actions, etc.)
  • Converts YAML/JSON into Terraform-compatible structures
  • Enriches with environment-specific defaults
  • Validates against organizational standards

Step 3: Loop-Driven Provisioning

# example workspace creation
module "workspaces" {
  source   = "./tfe/modules/workspaces"
  for_each = local.workspace_definitions
  
  name         = each.value.name
  project_name = each.value.project
  vcs_repo     = each.value.vcs_repo
  # ...
}

Step 4: Access Control Application

# example workspace access
module "workspace_access" {
  source   = "./tfe/modules/teams/modules/team-access"
  for_each = local.workspace_permissions
  
  workspace_id = module.workspaces[each.key].id
  team_id      = data.tfe_team.read_only[each.value.team].id
  access       = "read"
}

Granular Access Control

Multi-Tier Permission Model

Organization → Project → Workspace → Resource

Team Types & Permissions

Team Type Scope Capabilities
Platform Teams Organization Manage all projects, teams, policies
Project Admins Project Create workspaces, assign teams
Workspace Admins Workspace Configure settings, manage runs, apply changes
Workspace Apply Workspace Execute runs, update variables
Workspace Read Workspace View runs, download state (read-only)

AD Group Mapping

# Example: Platform Infrastructure Team
module "platform_team" {
  source = "./tfe/modules/teams"
  name   = "ad-platform-infrastructure"
  
  properties = {
    manage_workspaces    = true
    manage_policies      = true
    manage_vcs_settings  = true
  }
}

# Example: Application Team
module "app_team_read" {
  source = "./tfe/modules/teams"
  name   = "ad-app-developers-read"
  
  properties = {
    manage_workspaces = false  # Read-only access
  }
}

Complete Workflow: End-to-End

Phase 1: Infrastructure Foundation

  1. Project Creation

    • Define logical boundaries (networking, data, applications)
    • Execute projects
  2. Team Provisioning

    • Map AD groups to TFC teams
    • Execute teams
    • Assign organization-level permissions
  3. Project Access

    • Link teams to projects
    • Establish baseline read permissions

Phase 2: Workspace Provisioning

  1. Requirement Definition

    # Simple format for developers
    workspace:
      name: my-app-prod
      cloud: aws
      repository: pgetech/my-app-infra
  2. Orchestration Processing

    • Validate against standards
    • Enrich with defaults (Terraform version, policies)
    • Generate Terraform variables
  3. Workspace Creation

    • Loop through definitions
    • Execute workspaces
    • Configure VCS, variables, run tasks
  4. Permission Assignment

    • Apply workspace-level team access
    • Enforce least-privilege principle

Phase 3: Continuous Management

  1. Change Detection

    • Monitor workspace definition files
    • Trigger Terraform apply on updates
  2. Drift Remediation

    • Detect manual changes in TFC
    • Re-apply Terraform to restore desired state
  3. Lifecycle Automation

    • Auto-destroy for ephemeral environments
    • Tag management via OIDC Lambda integration

Key Design Benefits

Scalability

Template-Based: One module supports hundreds of workspaces
Loop-Driven: for_each eliminates repetitive code
Data-Driven: Adding workspaces = updating data files

Security

AD Integration: SSO with existing identity provider
Least Privilege: Granular permissions at workspace level
Policy Enforcement: Sentinel policies attached automatically
Audit Trail: All changes tracked in Terraform state

Developer Experience

Self-Service: Teams request workspaces via simple YAML
Consistency: Standardized workspace configuration
Fast Provisioning: Automated creation in minutes

Operations

Infrastructure as Code: TFC infrastructure versioned in Git
Idempotent: Safe to re-run without side effects
Multi-Cloud: AWS/Azure support with conditional logic
Compliance: Run tasks (Prisma Cloud) enforce security standards


Technical Implementation Details

Module Hierarchy

tfe/
├── modules/
│   ├── projects/
│   │   ├── main.tf              # Project resource
│   │   └── modules/
│   │       └── project-access/  # Team-to-project binding
│   ├── teams/
│   │   ├── main.tf              # Team resource
│   │   └── modules/
│   │       └── team-access/     # Team-to-workspace binding
│   └── workspaces/
│       ├── main.tf              # Workspace resource
│       ├── variables.tf         # Multi-cloud variables
│       └── outputs.tf           # Workspace metadata

Data Flow

External Data Source (YAML/JSON)
    ↓
Orchestration Layer (Lambda/Actions)
    ↓
Terraform Variables (tfvars)
    ↓
[Layer 1] Projects + Teams + Project Access
    ↓
[Layer 2] Workspaces + Variables + Team Access
    ↓
Terraform Cloud Infrastructure

Multi-Cloud Support

# Conditional variable injection
locals {
  aws_vars = var.csp == "aws" ? {
    account_num = var.account_num
  } : {}
  
  azure_vars = var.csp == "azure" ? {
    arm_subscription_id = var.arm_subscription_id
    arm_tenant_id       = var.arm_tenant_id
  } : {}
}

Example: Complete Workspace Lifecycle

# 1. Create Project
module "data_platform" {
  source = "./tfe/modules/projects"
  name   = "data-platform"
}

# 2. Create Teams
module "data_engineers" {
  source = "./tfe/modules/teams"
  name   = "ad-data-engineers"
}

# 3. Assign Team to Project
module "project_access" {
  source     = "./tfe/modules/projects/modules/project-access"
  project_id = module.data_platform.id
  team_id    = module.data_engineers.id
  access     = "read"
}

# 4. Create Workspace
module "glue_crawler" {
  source       = "./tfe/modules/workspaces"
  name         = "data-glue-crawler-prod"
  project_name = "data-platform"
  vcs_repo     = {
    identifier = "pgetech/terraform-glue"
    branch     = "main"
  }
  csp         = "aws"
  account_num = "123456789012"
}

# 5. Assign Workspace Permissions
module "workspace_access" {
  source       = "./tfe/modules/teams/modules/team-access"
  workspace_id = module.glue_crawler.id
  team_id      = module.data_engineers.id
  access       = "apply"
}

Summary: Unified Design Principles

🎯 Declarative: Infrastructure defined as code
🎯 Modular: Reusable components with clear boundaries
🎯 Automated: Minimal manual intervention
🎯 Secure: AD-integrated, policy-enforced, least-privilege
🎯 Scalable: Supports enterprise-scale workspace management
🎯 Auditable: Complete change history in version control

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment