terraform-code-generator

Auto-generate Terraform resource blocks by fetching latest schemas from Terraform Registry in real-time

5 stars

Best use case

terraform-code-generator is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Auto-generate Terraform resource blocks by fetching latest schemas from Terraform Registry in real-time

Teams using terraform-code-generator should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/terraform-code-generator/SKILL.md --create-dirs "https://raw.githubusercontent.com/GuicedEE/ai-rules/main/skills/.curated/terraform-code-generator/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/terraform-code-generator/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How terraform-code-generator Compares

Feature / Agentterraform-code-generatorStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Auto-generate Terraform resource blocks by fetching latest schemas from Terraform Registry in real-time

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Terraform Code Generator

You are a Terraform code generation expert. When this skill is invoked, you automatically generate complete, valid Terraform resource blocks by fetching the latest schemas and documentation from the Terraform Registry in real-time.

## Your Task

When a user requests Terraform code generation:

1. **Fetch Latest Schema**:
   - Query Terraform Registry for latest provider version
   - Retrieve complete resource schema
   - Get argument types and requirements
   - Fetch examples and defaults

2. **Parse Schema Data**:
   - Identify required arguments
   - Identify optional arguments with defaults
   - Understand argument types (string, number, bool, list, map, object)
   - Map nested blocks and their structure

3. **Generate Complete Code**:
   - Create valid HCL syntax
   - Include all required arguments
   - Add common optional arguments
   - Use intelligent defaults
   - Add helpful comments

4. **Provide Context**:
   - Explain what the resource does
   - List all available arguments
   - Show usage examples
   - Highlight important configurations

## How It Works

### Step 1: User Request

User asks for a specific resource:
```
"Generate code for azurerm_storage_account"
"Create an aws_s3_bucket resource"
"I need a google_compute_instance"
```

### Step 2: Fetch from Registry

The skill queries the Terraform Registry API:
```
GET https://registry.terraform.io/v2/providers/hashicorp/azurerm/latest
GET https://registry.terraform.io/v2/providers/hashicorp/azurerm/{version}/docs/resources/storage_account
```

### Step 3: Parse Schema

Example schema response:
```json
{
  "attributes": {
    "name": {
      "type": "string",
      "required": true,
      "description": "Specifies the name of the storage account"
    },
    "resource_group_name": {
      "type": "string",
      "required": true
    },
    "location": {
      "type": "string",
      "required": true
    },
    "account_tier": {
      "type": "string",
      "required": true,
      "description": "Defines the Tier to use for this storage account"
    },
    "account_replication_type": {
      "type": "string",
      "required": true
    },
    "enable_https_traffic_only": {
      "type": "bool",
      "optional": true,
      "default": true
    }
  },
  "block_types": {
    "network_rules": {
      "nesting_mode": "single",
      "block": {
        "attributes": {
          "default_action": {
            "type": "string",
            "required": true
          }
        }
      }
    }
  }
}
```

### Step 4: Generate Code

Output complete, production-ready code:

```hcl
# Azure Storage Account
# https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account

resource "azurerm_storage_account" "example" {
  # Required arguments
  name                     = "storageaccountname"  # Must be globally unique, 3-24 chars
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"  # Options: Standard, Premium
  account_replication_type = "LRS"       # Options: LRS, GRS, RAGRS, ZRS, GZRS, RAGZRS

  # Recommended security settings
  enable_https_traffic_only         = true
  min_tls_version                   = "TLS1_2"
  infrastructure_encryption_enabled = true
  allow_nested_items_to_be_public   = false

  # Optional: Network rules
  network_rules {
    default_action = "Deny"
    bypass         = ["AzureServices"]
  }

  # Optional: Blob properties
  blob_properties {
    versioning_enabled = true

    delete_retention_policy {
      days = 7
    }
  }

  tags = {
    Environment = "Production"
    ManagedBy   = "Terraform"
  }
}
```

## Supported Providers

### Azure (azurerm)
- Latest version auto-detected
- All resource types supported
- Security best practices included

**Common resources**:
- `azurerm_resource_group`
- `azurerm_storage_account`
- `azurerm_virtual_network`
- `azurerm_linux_virtual_machine`
- `azurerm_kubernetes_cluster`
- `azurerm_key_vault`
- `azurerm_mssql_server`

### AWS
- Latest version auto-detected
- All resource types supported
- Security best practices included

**Common resources**:
- `aws_vpc`
- `aws_s3_bucket`
- `aws_instance`
- `aws_rds_instance`
- `aws_eks_cluster`
- `aws_lambda_function`

### Google Cloud (google)
- Latest version auto-detected
- All resource types supported

**Common resources**:
- `google_compute_network`
- `google_storage_bucket`
- `google_compute_instance`
- `google_container_cluster`
- `google_sql_database_instance`

## Generation Modes

### Mode 1: Basic (Default)
Generates minimal working configuration with only required arguments:

```hcl
resource "azurerm_storage_account" "example" {
  name                     = "storageaccountname"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = "eastus"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}
```

### Mode 2: Recommended
Includes security best practices and common configurations:

```hcl
resource "azurerm_storage_account" "example" {
  # Required
  name                     = "storageaccountname"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = "eastus"
  account_tier             = "Standard"
  account_replication_type = "LRS"

  # Security (recommended)
  enable_https_traffic_only         = true
  min_tls_version                   = "TLS1_2"
  infrastructure_encryption_enabled = true
  allow_nested_items_to_be_public   = false

  tags = var.tags
}
```

### Mode 3: Complete
Includes all commonly-used optional arguments:

```hcl
resource "azurerm_storage_account" "example" {
  # All available configurations
  # (shows complete example with all blocks)
}
```

## Smart Defaults

The generator uses intelligent defaults based on:

1. **Provider Best Practices**
2. **Security Requirements**
3. **Common Usage Patterns**
4. **Type Constraints**

### String Arguments

```hcl
# Generic name
name = "{resource_type}-{purpose}"

# Location
location = "eastus"  # or var.location

# SKU/Tier
account_tier = "Standard"  # Most common default
```

### Boolean Arguments

```hcl
# Security flags default to secure
enable_https_traffic_only = true
public_network_access_enabled = false
```

### List Arguments

```hcl
# Empty list if optional
availability_zones = []

# Example values if required
address_space = ["10.0.0.0/16"]
```

### Object/Block Arguments

```hcl
# Common blocks included with defaults
network_rules {
  default_action = "Deny"
  bypass         = ["AzureServices"]
}
```

## Variable Integration

Generated code can use variables:

```hcl
resource "azurerm_storage_account" "example" {
  name                = var.storage_account_name
  resource_group_name = var.resource_group_name
  location            = var.location

  tags = var.tags
}
```

Generates corresponding variables.tf:

```hcl
variable "storage_account_name" {
  description = "Name of the storage account"
  type        = string

  validation {
    condition     = length(var.storage_account_name) >= 3 && length(var.storage_account_name) <= 24
    error_message = "Storage account name must be between 3 and 24 characters."
  }
}

variable "location" {
  description = "Azure region"
  type        = string
  default     = "eastus"
}

variable "tags" {
  description = "Resource tags"
  type        = map(string)
  default     = {}
}
```

## Batch Generation

Generate multiple resources at once:

**Request**: "Generate Azure web app infrastructure"

**Output**:
```hcl
# Resource Group
resource "azurerm_resource_group" "example" {
  name     = "rg-webapp-prod"
  location = "eastus"
  tags     = var.tags
}

# App Service Plan
resource "azurerm_service_plan" "example" {
  name                = "asp-webapp-prod"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  os_type             = "Linux"
  sku_name            = "P1v2"
  tags                = var.tags
}

# Web App
resource "azurerm_linux_web_app" "example" {
  name                = "app-webapp-prod"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  service_plan_id     = azurerm_service_plan.example.id

  site_config {
    minimum_tls_version = "1.2"
    always_on           = true
  }

  https_only = true
  tags       = var.tags
}

# Application Insights
resource "azurerm_application_insights" "example" {
  name                = "appi-webapp-prod"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  application_type    = "web"
  tags                = var.tags
}
```

## Script Integration

If `scripts/code-generator.js` exists, use it:

```bash
# Generate single resource
node scripts/code-generator.js \
  --provider azurerm \
  --resource storage_account \
  --mode recommended

# Generate with custom name
node scripts/code-generator.js \
  --provider azurerm \
  --resource virtual_network \
  --name main \
  --mode complete

# Generate multiple resources
node scripts/code-generator.js \
  --provider azurerm \
  --resources storage_account,key_vault,virtual_network

# Generate with variables
node scripts/code-generator.js \
  --provider aws \
  --resource s3_bucket \
  --use-variables

# Output to file
node scripts/code-generator.js \
  --provider azurerm \
  --resource storage_account \
  --output storage.tf
```

## Integration with Codex

When invoked in Codex, this skill:

1. **Understands intent**: "I need an Azure storage account"
2. **Fetches latest schema** from Terraform Registry
3. **Generates complete code** with best practices
4. **Explains the code** and available options
5. **Suggests related resources** that might be needed

### Example Interaction

**User**: "Create an Azure storage account with blob container"

**Codex with this skill**:
1. Fetches azurerm_storage_account schema
2. Fetches azurerm_storage_container schema
3. Generates both resources with proper dependencies
4. Includes security best practices
5. Adds comments and documentation links

## Code Quality Features

### Always Includes

- Proper HCL formatting
- Required arguments
- Security best practices
- Helpful comments
- Documentation links
- Tags support
- Validation rules
- Type constraints

### Never Includes

- No hardcoded secrets
- No insecure defaults
- No deprecated arguments
- No invalid syntax
- No missing required fields

## Real-Time Updates

Since the skill fetches from the Terraform Registry API:

- Always uses latest provider version
- Gets current resource schemas
- Includes new arguments automatically
- Reflects deprecation warnings
- Shows latest best practices

## Comparison with Other Tools

| Feature | terraform-code-generator | Manual Coding | Terraform Docs |
|---------|-------------------------|---------------|----------------|
| Latest schemas | Auto-fetched | Manual lookup | Available |
| Auto-generation | Yes | No | No |
| Best practices | Included | Manual | Basic |
| Security defaults | Yes | Manual | No |
| Variable generation | Yes | Manual | No |
| Batch creation | Yes | Manual | No |

## Reference Files

See `references/` for:
- Resource schema specifications
- Provider-specific best practices
- Generation templates
- Common patterns library

Related Skills

terraform-validator

5
from GuicedEE/ai-rules

Validate Terraform code for syntax errors, best practices, security issues, and compliance with standards

terraform-state-manager

5
from GuicedEE/ai-rules

Analyze, inspect, and safely manipulate Terraform state files with drift detection and resource management

terraform-security-scanner

5
from GuicedEE/ai-rules

Comprehensive security scanning for Terraform code including secrets detection, compliance checks, and vulnerability assessment

terraform-resource-fetch

5
from GuicedEE/ai-rules

Fetch latest Terraform provider resources and documentation from Terraform Registry

terraform-project-generator

5
from GuicedEE/ai-rules

Generate complete Terraform project structure with main.tf, variables.tf, outputs.tf, backend.tf, and terraform.tf following best practices

terraform-plan-analyzer

5
from GuicedEE/ai-rules

Parse, explain, and analyze terraform plan output for impact assessment, cost estimation, and risk evaluation

terraform-module-scaffold

5
from GuicedEE/ai-rules

Create production-ready, reusable Terraform modules with complete documentation, examples, and tests

terraform-doc-generator

5
from GuicedEE/ai-rules

Auto-generate comprehensive README.md documentation for Terraform modules with usage examples, inputs, outputs, and requirements

skill-installer

5
from GuicedEE/ai-rules

Install Codex skills into $CODEX_HOME/skills from a curated list or a GitHub repo path. Use when a user asks to list installable skills, install a curated skill, or install a skill from another repo (including private repos).

skill-creator

5
from GuicedEE/ai-rules

Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Codex's capabilities with specialized knowledge, workflows, or tool integrations.

jwebmp-webawesome

5
from GuicedEE/ai-rules

WebAwesome icon integration for JWebMP — modern, open-source icon library. Provides 1,500+ icons with solid/regular styles, sizing, rotation, animation, and CSS utilities. Drop-in FontAwesome alternative with fresh designs. Use when working with WebAwesome icons, modern icon designs, or as FontAwesome alternative in JWebMP applications.

jwebmp-webawesome-pro

5
from GuicedEE/ai-rules

WebAwesome Pro integration for JWebMP with premium icons and features. Extends jwebmp-webawesome with additional styles, premium icons, and advanced features. Use when working with WebAwesome Pro icons or premium WebAwesome features in JWebMP applications.