Skip to main content

atmos terraform generate files

Use this command to generate auxiliary configuration files for Terraform components based on the generate section in stack configuration.

atmos terraform generate files --help

Usage

Execute the terraform generate files command like this:

# Generate files for a single component
atmos terraform generate files <component> -s <stack>

# Generate files for all components
atmos terraform generate files --all

This command generates files from the generate section of component configurations. Files are created in the component directory with content serialized based on file extension or processed as Go templates.

tip

Run atmos terraform generate files --help to see all the available options

Examples

# Generate files for a single component in a stack
atmos terraform generate files vpc -s prod-ue2

# Generate files for all components across all stacks
atmos terraform generate files --all

# Preview what would be generated without writing files
atmos terraform generate files vpc -s prod-ue2 --dry-run

# Generate files for specific stacks only
atmos terraform generate files --all --stacks="prod-*"

# Generate files for specific components only
atmos terraform generate files --all --components="vpc,eks"

# Delete generated files
atmos terraform generate files --all --clean

Configuration

The generate section is defined at the component level in stack configuration:

components:
terraform:
vpc:
vars:
environment: prod
generate:
# Map values are serialized based on file extension
locals.tf:
locals:
environment: "{{ .vars.environment }}"

# String values are treated as Go templates
README.md: |
# VPC Component
Environment: {{ .vars.environment }}

Extension-Aware Serialization

  • .json files are serialized as JSON with 2-space indentation
  • .yaml and .yml files are serialized as YAML with 2-space indentation
  • .tf and .hcl files generate valid HCL
  • Other extensions default to JSON

Template Context

String values and map string values are processed as Go templates with access to:

  • .vars - Component variables
  • .settings - Component settings
  • .env - Component environment variables
  • .backend - Backend configuration
  • .backend_type - Backend type
  • .providers - Provider configurations
  • .metadata - Component metadata
  • .namespace, .tenant, .environment, .stage, .region - Context variables
  • .atmos_component - Atmos component name
  • .atmos_stack - Atmos stack name
  • .component - Final component name
  • .workspace - Terraform workspace

Auto-Generation

Enable automatic file generation during terraform commands:

# atmos.yaml
components:
terraform:
auto_generate_files: true

Configure File Generation

Learn how to configure the generate section in your stack manifests to create auxiliary files for Terraform components.

Working Example

Generate Files

This example demonstrates how to use atmos terraform generate files to generate an entire Terraform component from stack configuration.

Overview

The generate section in component configuration defines files that Atmos will create. This example generates a complete Terraform component including:

  • variables.tf - Variable definitions
  • outputs.tf - Output definitions
  • versions.tf - Terraform version constraints
  • locals.tf - Local values
  • terraform.tfvars - Variable values
  • config.json - Environment-specific configuration
  • README.md - Component documentation

Content Types

Content TypeBehavior
String (multiline)Written as-is, supports Go templates
Map with .json extensionSerialized as pretty-printed JSON
Map with .yaml/.yml extensionSerialized as YAML
Map with .tf/.hcl extensionSerialized as HCL blocks
Map with .tfvars extensionSerialized as HCL attributes (no blocks)

HCL Generation

When using map syntax for .tf files, Atmos automatically handles:

  • Labeled blocks (variable, output, resource, data, module, provider):

    variable:
    app_name:
    type: string
    description: "Application name"

    Generates: variable "app_name" { type = "string" ... }

  • Unlabeled blocks (terraform, locals):

    terraform:
    required_version: ">= 1.0.0"

    Generates: terraform { required_version = ">= 1.0.0" }

Note: For blocks that need HCL expressions (like value = var.app_name), use string templates instead of map syntax.

Template Variables

Available in templates via {{ .variable }}:

VariableDescription
atmos_componentComponent name
atmos_stackStack name
varsComponent variables (map)
vars.stageStage from component vars

Auto-Generation

Enable automatic file generation on any terraform command:

# atmos.yaml
components:
terraform:
auto_generate_files: true

With this enabled, files are regenerated before init, plan, apply, etc.

Usage

Generate the component

cd examples/generate-files
mkdir -p components/terraform/demo
atmos terraform generate files demo -s dev

Preview without writing (dry-run)

atmos terraform generate files demo -s dev --dry-run

Delete generated files (clean)

atmos terraform generate files demo -s dev --clean

Generated Files

After running atmos terraform generate files demo -s dev:

versions.tf (HCL map syntax):

terraform {
required_version = ">= 1.0.0"
}

locals.tf (HCL map syntax with templates):

locals {
app_name = "myapp-dev"
environment = "dev"
}

variables.tf (string template):

variable "app_name" {
type = string
description = "Application name"
}

terraform.tfvars (flat HCL attributes):

app_name = "myapp-dev"
version = "1.0.0-dev"

config.json (JSON from map):

{
"app": "myapp-dev",
"stage": "dev",
"version": "1.0.0-dev"
}

Project Structure

generate-files/
├── atmos.yaml # Atmos configuration (auto_generate_files: true)
├── components/ # Generated (gitignored)
│ └── terraform/
│ └── demo/
│ ├── variables.tf # Generated
│ ├── outputs.tf # Generated
│ ├── versions.tf # Generated
│ ├── locals.tf # Generated
│ ├── terraform.tfvars # Generated
│ ├── config.json # Generated
│ └── README.md # Generated
└── stacks/
├── catalog/
│ └── demo.yaml # Component with generate section
└── deploy/
├── dev.yaml # Dev environment
└── prod.yaml # Prod environment

Try It

cd examples/generate-files

# Create component directory
mkdir -p components/terraform/demo

# Generate files for dev
atmos terraform generate files demo -s dev

# See what was generated
ls components/terraform/demo/
cat components/terraform/demo/versions.tf
cat components/terraform/demo/locals.tf

# Generate for prod (different values)
atmos terraform generate files demo -s prod
cat components/terraform/demo/config.json

# Clean up
atmos terraform generate files demo -s dev --clean

Arguments

component (optional)

Atmos terraform component. Required when not using --all.

Flags

--stack (alias -s)

Atmos stack. Required when specifying a component.

--all

Process all components in all stacks. Cannot be used with a component argument.

atmos terraform generate files --all
--stacks

Filter stacks by glob pattern. Only used with --all.

atmos terraform generate files --all --stacks="prod-*"
--components

Filter components by comma-separated list. Only used with --all.

atmos terraform generate files --all --components="vpc,eks"
--dry-run

Show what would be generated without writing files.

atmos terraform generate files vpc -s prod-ue2 --dry-run
--clean

Delete generated files instead of creating them.

atmos terraform generate files --all --clean