<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lalit Bagga</title>
    <description>The latest articles on DEV Community by Lalit Bagga (@lbagga).</description>
    <link>https://dev.to/lbagga</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3970432%2F2c430d1f-c46a-41e9-9311-d5fce9dfd5c2.png</url>
      <title>DEV Community: Lalit Bagga</title>
      <link>https://dev.to/lbagga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lbagga"/>
    <language>en</language>
    <item>
      <title>Refactoring Terraform: From One File to Modules</title>
      <dc:creator>Lalit Bagga</dc:creator>
      <pubDate>Mon, 08 Jun 2026 13:31:33 +0000</pubDate>
      <link>https://dev.to/lbagga/refactoring-terraform-from-one-file-to-modules-3cgj</link>
      <guid>https://dev.to/lbagga/refactoring-terraform-from-one-file-to-modules-3cgj</guid>
      <description>&lt;p&gt;My three-tier AWS architecture worked. VPC, subnets, bastion host, app server, RDS, all deployed and running. But my &lt;code&gt;main.tf&lt;/code&gt; was a flat file with everything mixed together. Security groups next to route tables next to RDS instances next to IAM roles.&lt;/p&gt;

&lt;p&gt;It worked for a learning project. It would not work in a real team environment where multiple people need to understand, maintain, and extend the infrastructure.&lt;/p&gt;

&lt;p&gt;So I refactored it into modules. Here is what I learned.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is a Module
&lt;/h2&gt;

&lt;p&gt;A module is just a folder with its own Terraform files. Nothing magic about it. You move related resources into that folder, define what it needs as inputs, define what it exposes as outputs, and then call it from your root configuration.&lt;/p&gt;

&lt;p&gt;The root &lt;code&gt;main.tf&lt;/code&gt; becomes an orchestrator, it calls each module and wires them together by passing outputs from one into inputs of another.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Final Structure
&lt;/h2&gt;

&lt;p&gt;Before refactoring everything lived in one file. After:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;three-tier/
├── main.tf               ← calls all modules, wires them together
├── variables.tf
├── outputs.tf
└── module/
    ├── networking/
    │   ├── main.tf
    │   ├── variable.tf
    │   └── outputs.tf
    ├── security/
    │   ├── main.tf
    │   ├── variable.tf
    │   └── outputs.tf
    ├── compute/
    │   ├── main.tf
    │   ├── variable.tf
    │   └── output.tf
    └── database/
        ├── main.tf
        ├── variable.tf
        └── output.tf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each module owns one concern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;networking  → VPC, subnets, IGW, NAT gateway, route tables
security    → security groups and all ingress/egress rules
compute     → IAM roles, instance profile, SSM, key pair, EC2 instances
database    → RDS instance, DB subnet group
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  The Core Pattern: Outputs and Variables
&lt;/h2&gt;

&lt;p&gt;This is the most important thing to understand before you start. Modules cannot reach outside themselves. If the compute module needs the VPC ID, it cannot just reference &lt;code&gt;aws_vpc.main.id&lt;/code&gt; that resource lives in the networking module now.&lt;/p&gt;

&lt;p&gt;The pattern is always three steps:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1 Output it from the source module:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# module/networking/outputs.tf&lt;/span&gt;
&lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="s2"&gt;"vpc_id"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;aws_vpc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2 Declare it as a variable in the receiving module:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# module/security/variable.tf&lt;/span&gt;
&lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="s2"&gt;"vpc_id"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;description&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"VPC ID from networking module"&lt;/span&gt;
  &lt;span class="nx"&gt;type&lt;/span&gt;        &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3 Pass it through the root main.tf:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.tf&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"security"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./module/security"&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;networking&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;vpc_id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every cross-module reference follows this exact pattern. Once you internalize it the errors stop being confusing.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Dependency Order
&lt;/h2&gt;

&lt;p&gt;Modules depend on each other in a specific order. Networking has no dependencies so it goes first. Security needs the VPC ID from networking. Compute and database both need outputs from networking and security.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;networking  → no dependencies
    ↓
security    → needs vpc_id from networking
    ↓
compute     → needs subnet IDs from networking
            → needs bastion_sg_id, private_sg_id from security
database    → needs db subnet IDs from networking
            → needs db_sg_id from security
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terraform figures out the order automatically based on these references. You do not need to use &lt;code&gt;depends_on&lt;/code&gt; explicitly as soon as you reference &lt;code&gt;module.networking.vpc_id&lt;/code&gt;, Terraform knows networking must complete before security starts.&lt;/p&gt;




&lt;h2&gt;
  
  
  How I Approached the Refactor
&lt;/h2&gt;

&lt;p&gt;I did it one module at a time, starting with networking. The process for each module was:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create the module folder and files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Move the relevant resources into &lt;code&gt;module/networking/main.tf&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a &lt;code&gt;module "networking"&lt;/code&gt; call in root &lt;code&gt;main.tf&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;terraform plan&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fix the errors — usually missing outputs or undeclared variables&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repeat for next module&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The errors I kept hitting all looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: Reference to undeclared resource
  on main.tf line 38, in resource "aws_security_group" "bastion_sg":
  vpc_id = aws_vpc.main.id

A managed resource "aws_vpc" "main" has not been declared in the root module.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means a resource is trying to reference something that has moved into a module. The fix is always the same , output it from the module, declare a variable in the receiving module, pass it through root.&lt;/p&gt;




&lt;h2&gt;
  
  
  The State Migration Problem
&lt;/h2&gt;

&lt;p&gt;Here is something nobody warns you about when refactoring Terraform into modules.&lt;/p&gt;

&lt;p&gt;When you move a resource from root into a module, its address in the state file changes. What was &lt;code&gt;aws_vpc.main&lt;/code&gt; becomes &lt;code&gt;module.networking.aws_vpc.main&lt;/code&gt;. Terraform sees this as a different resource, it thinks the old one was deleted and a new one needs to be created.&lt;/p&gt;

&lt;p&gt;Running &lt;code&gt;terraform plan&lt;/code&gt; after the refactor showed this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Plan: 27 to add, 0 to change, 27 to destroy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is not what you want. It would destroy and recreate all your infrastructure.&lt;/p&gt;

&lt;p&gt;The proper fix for a production environment is &lt;code&gt;terraform state mv&lt;/code&gt; , a command that tells Terraform a resource just moved, it was not deleted. You run one command per resource:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;terraform state &lt;span class="nb"&gt;mv &lt;/span&gt;aws_vpc.main module.networking.aws_vpc.main
terraform state &lt;span class="nb"&gt;mv &lt;/span&gt;aws_subnet.main_subnet_public_1 module.networking.aws_subnet.main_subnet_public_1
&lt;span class="c"&gt;# ... one for every resource&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a learning project with no real traffic or data at risk, the simpler path is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;terraform destroy
terraform apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Destroy everything, apply fresh from the new module structure. Same end result, no manual state migration required.&lt;/p&gt;

&lt;p&gt;The apply completed cleanly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Apply complete! Resources: 35 added, 0 changed, 0 destroyed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What the Root main.tf Looks Like Now
&lt;/h2&gt;

&lt;p&gt;The root &lt;code&gt;main.tf&lt;/code&gt; went from a flat list of 43+ resources to a clean orchestration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight hcl"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"networking"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;source&lt;/span&gt;     &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./module/networking"&lt;/span&gt;
  &lt;span class="nx"&gt;aws_region&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;var&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aws_region&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"security"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./module/security"&lt;/span&gt;
  &lt;span class="nx"&gt;vpc_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;networking&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;vpc_id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"compute"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;source&lt;/span&gt;                &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./module/compute"&lt;/span&gt;
  &lt;span class="nx"&gt;public_subnet_id&lt;/span&gt;      &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;networking&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;public_subnet_id&lt;/span&gt;
  &lt;span class="nx"&gt;private_subnet_id&lt;/span&gt;     &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;networking&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;private_subnet_id&lt;/span&gt;
  &lt;span class="nx"&gt;bastion_sg_id&lt;/span&gt;         &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bastion_sg_id&lt;/span&gt;
  &lt;span class="nx"&gt;private_sg_id&lt;/span&gt;         &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;private_sg_id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt; &lt;span class="s2"&gt;"database"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;source&lt;/span&gt;         &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"./module/database"&lt;/span&gt;
  &lt;span class="nx"&gt;db_subnet_1_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;networking&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db_subnet_1_id&lt;/span&gt;
  &lt;span class="nx"&gt;db_subnet_2_id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;networking&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db_subnet_2_id&lt;/span&gt;
  &lt;span class="nx"&gt;db_sg_id&lt;/span&gt;       &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db_sg_id&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read this and immediately understand the infrastructure. Four modules, clear dependencies, no hunting through hundreds of lines to find what you need.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Modules are just folders.&lt;/strong&gt; There is no magic. The mental shift is understanding that resources can no longer reference each other directly once they live in different modules. Everything goes through outputs and variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Start with networking.&lt;/strong&gt; It has no dependencies so there are no wiring errors to debug. Get networking working first, then add security, then compute and database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The state migration problem is real.&lt;/strong&gt; In production you would never destroy and recreate. You would use &lt;code&gt;terraform state mv&lt;/code&gt; or &lt;code&gt;moved&lt;/code&gt; blocks to migrate state without downtime. For a learning project, destroy and recreate is fine, but knowing why the problem exists is important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The root main.tf should be an orchestrator, not a resource file.&lt;/strong&gt; If you have resource blocks in your root main.tf alongside module calls, that is a signal something belongs in a module.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Next
&lt;/h2&gt;

&lt;p&gt;The next step is enabling RDS IAM Authentication, replacing the hardcoded database password with token-based access. Storing credentials directly in Terraform is a bad practice and there is a cleaner way to handle it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/"&gt;#aws #terraform #devops #infrastructureascode #modules&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>terraform</category>
      <category>infrastructure</category>
      <category>module</category>
    </item>
  </channel>
</rss>
