Why These Prompts Matter
Terraform has become the de facto tool for provisioning cloud infrastructure, but writing efficient, maintainable IaC often requires more than just memorizing syntax. Whether you're structuring modules, managing remote state, or orchestrating multi-cloud deployments, the right prompt can save hours of debugging. Below are 12 battle-tested prompts you can use with any AI assistant to accelerate your Terraform workflow.
1. Generate a Reusable Module Skeleton
Prompt: "Create a Terraform module skeleton for an AWS VPC with variables for CIDR block, availability zones, and tags. Include outputs for subnet IDs and NAT gateway IPs."
Example:
# modules/aws-vpc/main.tf
resource "aws_vpc" "this" {
cidr_block = var.cidr_block
tags = var.tags
}
This prompt forces the AI to produce a standard module structure (variables, resources, outputs) that you can drop into a modules/ directory.
2. Refactor a Monolithic Configuration into Modules
Prompt: "I have a single main.tf with 500 lines of EC2, RDS, and S3 resources. Break it into separate modules for compute, database, and storage. Show the refactored module calls and any necessary renames."
Example output: Splits code into modules/compute, modules/database, and updates main.tf to call each module with appropriate variables.
3. Write a Remote State Configuration
Prompt: "Generate a Terraform configuration to store state in an S3 bucket with DynamoDB locking. Include backend block, required version, and instructions for initial setup."
Example:
terraform {
backend "s3" {
bucket = "my-tfstate-bucket"
key = "prod/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
This eliminates manual setup of state backends.
4. Design a Multi-Region Deployment
Prompt: "Create a Terraform configuration that deploys an identical set of resources (VPC, ALB, EC2) in us-east-1 and eu-west-1 using a single variable-driven module."
Example: Use count or for_each on regions:
module "region" {
source = "./modules/region"
for_each = toset(["us-east-1", "eu-west-1"])
region = each.value
}
5. Convert an Existing Resource to Data Source
Prompt: "I currently define a security group inline in my Terraform code. Rewrite it to use a data source that references an existing security group by name or tags."
Example:
data "aws_security_group" "existing" {
filter {
name = "group-name"
values = ["web-sg"]
}
}
This avoids duplicate creation and improves reliability.
6. Create a CI/CD Pipeline Validation Prompt
Prompt: "Write a GitHub Actions workflow that runs terraform fmt, terraform validate, and checkov on every pull request. Include caching for plugins."
Example: YAML workflow that runs on pull_request events and fails if formatting or security checks fail.
7. Explain and Fix a Complex for_each / count Error
Prompt: "Explain why this Terraform code fails: resource "aws_instance" "web" { count = length(var.subnet_ids) } when I later try to reference aws_instance.web.subnet_id. Show a working version."
Explanation: The error occurs because count creates a list of resources, so you must use aws_instance.web[0].subnet_id or iterate properly.
8. Generate a Multi-Cloud Module (AWS + GCP)
Prompt: "Create a Terraform module that provisions a load balancer on both AWS (ALB) and GCP (HTTP Load Balancer) using a single interface with cloud_provider variable."
Example: Use conditional resources with count based on var.cloud_provider.
9. Write a Sentinel Policy for Cost Control
Prompt: "Produce a HashiCorp Sentinel policy that denies any Terraform plan creating an m5.2xlarge or larger EC2 instance unless it's in a sandbox workspace."
Example: deny = instance_types.filter(...) logic.
10. Migrate State Between Backends
Prompt: "I'm moving my Terraform state from a local file to an Azure Storage backend. Provide the exact terraform init -reconfigure command and steps to avoid state loss."
Output: Step-by-step using terraform init -migrate-state.
11. Debug a Provider Version Conflict
Prompt: "My Terraform plan fails with 'Error: Inconsistent provider lock file'. Explain the cause and show how to update to the latest compatible version using terraform providers lock."
Solution: Run terraform providers lock to regenerate .terraform.lock.hcl.
12. Optimize Large State Files
Prompt: "My Terraform state file is 10 MB with thousands of resources. Suggest strategies to reduce its size: splitting into substates, using terraform state rm for non-critical resources, or using separate workspaces."
Recommendation: Partition by environment or component using Terraform workspaces or separate root modules.
Bonus: 13. Create a Diagram from .tf Files
Prompt: "Generate a Mermaid.js flowchart from this Terraform configuration that shows resource dependencies. Use depends_on as edges."
Output: A Mermaid code block you can render.
Final Thoughts
These prompts cover the most common IaC pain points: modularization, state management, cross-cloud patterns, and debugging. Use them as templates and adapt to your cloud provider. For deeper learning, refer to the Terraform Documentation and HashiCorp Learn. Start with prompt #1 today and see how much faster you can scaffold your infrastructure.
Comments