
Terraform on AWS: Modules and Remote State
A hands-on guide to reusable Terraform modules, isolated environments, and S3 state.
Terraform on AWS: Modules and Remote State
Introduction
When I started learning Terraform, my first goal was simple: create AWS resources using code instead of manually configuring them in the AWS Management Console.
After experimenting with Amazon S3, Amazon EC2, VPCs, and security groups, I discovered that creating infrastructure is only one part of Infrastructure as Code (IaC).
The next challenge was organizing Terraform code, reusing infrastructure modules, and managing state across multiple environments.
In this article, I'll share how I approached these concepts through a hands-on AWS learning project.
1. The Problem with Local Terraform State
By default, Terraform stores its state locally in a file named
terraform.tfstate .This file helps Terraform track the infrastructure it manages.
However, local state introduces challenges when working across multiple machines or collaborating with a team:
- State can be lost if the local machine fails.
- Team members may have different state files.
- Concurrent infrastructure changes can cause conflicts.
I wanted to explore a more reliable way to manage Terraform state.
2. Moving Terraform State to Amazon S3
I created a separate bootstrap configuration to provision an Amazon S3 bucket for storing Terraform state.
The bucket was configured with versioning to support recovery of previous state versions.
In my application configuration, I used the S3 backend:
terraform { backend "s3" { bucket = "replace-with-your-state-bucket" key = "projects/dev/terraform.tfstate" region = "eu-central-1" encrypt = true use_lockfile = true }}Here:
-
bucketidentifies the existing S3 bucket.
-
keyspecifies where the state object is stored.
-
encryptenables server-side encryption for state storage.
-
use_lockfileenables S3-based state locking.
State locking helps prevent conflicting operations on the same state.
After configuring the backend, Terraform can migrate existing local state during
terraform init .Key lesson: The state bucket should be managed separately from the infrastructure that depends on it.
3. Creating Reusable Terraform Modules
My next step was understanding Terraform modules.
Instead of copying the same VPC, subnet, and security group configuration into every environment, I organized reusable infrastructure in a network module.
My learning project follows this structure:
17-aws-project/├── modules/│ └── network/│ ├── main.tf│ ├── variables.tf│ └── outputs.tf├── dev/│ ├── main.tf│ └── ec2.tf└── test/ └── main.tfA module accepts input variables, manages resources, and exposes selected values through outputs.
For example, the DEV environment can call the network module:
module "network" { source = "../modules/network"
name = "lab-dev" vpc_cidr = "10.17.0.0/16" subnet_cidr = "10.17.1.0/24"}The same module can be reused by the TEST environment with different input values.
This helps reduce duplicated configuration and keeps infrastructure definitions consistent.
4. Separating DEV and TEST Environments
One important lesson was that reusing a module does not mean sharing the same Terraform state.
Each independently managed environment should have an appropriate state boundary.
For example:
S3 State Bucket├── projects/│ ├── dev/│ │ └── terraform.tfstate│ └── test/│ └── terraform.tfstateWith separate state files, Terraform can independently track the resources managed by each environment.
For stronger production isolation, separate AWS accounts and IAM permissions may also be appropriate. State separation alone is not a complete security boundary.
5. Testing the Terraform Workflow
For each root module, the basic workflow is:
terraform initterraform fmtterraform validateterraform planterraform applyI also practiced using:
terraform state listterraform outputterraform plan -destroyThese commands helped me understand which resources Terraform manages, how outputs expose useful information, and what changes Terraform plans to make.
Before applying any infrastructure change, it is important to review the execution plan carefully.
After completing a lab, I clean up disposable resources to avoid unnecessary AWS costs. The state bucket must remain available while other Terraform configurations depend on it.
6. Lessons Learned
This project helped me understand several practical Terraform concepts:
- Terraform state matters as much as Terraform configuration. Losing state can make existing infrastructure difficult to manage.
- Modules improve reusability. A shared module can be used with different inputs for different environments.
- Remote state improves collaboration. S3 provides centralized state storage, while locking helps coordinate operations.
- Infrastructure should be reviewed before deployment.
terraform planis a critical part of a safe workflow.
- Security matters. IAM roles and temporary credentials are preferable to using AWS root credentials or hardcoded access keys.
For my next step, I plan to explore automating Terraform validation, planning, and deployment using GitHub Actions and AWS IAM roles.
Conclusion
Learning Terraform changed how I think about cloud infrastructure.
Instead of treating AWS resources as individual objects created through a console, I now see them as parts of a system that can be defined, reviewed, reused, and managed through code.
My biggest takeaway is that Infrastructure as Code is not just about creating resources. It is about managing infrastructure consistently and safely throughout its lifecycle.
I hope these lessons help other developers who are beginning their Terraform journey on AWS.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article