Skip to main content
    listerineh.dev
    • About
    • Experience
    • Projects
    • Skills
    • Blog
    • Talks
    • Contact
    • Why
    • About
    • Experience
    • Projects
    • Skills
    • Blog
    • Talks
    • Contact
    • Why

    © 2026 Sebastian Alvarez

    Back to Blog

    Terraform: Scaling Infrastructure Without Breaking the Bank

    How I run dev, staging and prod from one Terraform codebase, size each environment for what it actually needs, and grow infrastructure only when the product does.

    Sebastian Alvarez

    January 14, 2026·8 min read·
    Loading...
    • Terraform
    • Infrastructure as Code
    • DevOps
    • Cloud
    • AWS
    • Scaling
    • Cost Optimization
    Terraform: Scaling Infrastructure Without Breaking the Bank
    Table of Contents

    The console era

    My first real AWS setup was built entirely by clicking. A VPC here, a security group there, an RDS instance configured from memory at 11 PM. It worked, right up until I needed a second environment and realized I couldn't tell you exactly what I had done to build the first one.

    That's the problem Terraform actually solves for me. Not "infrastructure as code" as a slogan, but being able to answer "what's running in production, and why?" by opening a folder.

    This post is how I use it today: one codebase for dev, staging and prod, resources sized for each environment, modules for whatever repeats, and a habit of starting small that has saved me a lot of money.

    What clicking around costs you

    If you've managed infrastructure by hand, you know the list. A new environment takes a day and still ends up slightly different from the last one. Nobody remembers why a security group has that one odd rule. There's no history, so nobody wants to touch prod, so prod slowly drifts away from everything else.

    None of that is dramatic on its own. Together it means slow deploys, bugs that only happen in one environment, and a cloud bill you don't fully understand.

    Same code, three environments

    The setup I keep coming back to is one set of Terraform files and a folder of variables per environment:

    infra/
    ├── modules/
    │   ├── microservice/
    │   ├── database/
    │   └── api-gateway/
    └── environments/
        ├── dev/terraform.tfvars
        ├── staging/terraform.tfvars
        └── prod/terraform.tfvars
    

    I build in dev first, where everything is small and breaking things is fine. Once dev is stable, staging gets the exact same architecture with its own state. Prod is the same code again with bigger numbers: larger instances, autoscaling, Multi-AZ, stricter security groups, more monitoring.

    So I'm not maintaining three infrastructures. I'm maintaining one, plus three small files that say how big each copy should be. A change goes dev → staging → prod, and by the time it reaches prod it has already been applied twice.

    You can get the same result with terraform workspace instead of folders. I prefer folders: each environment's state lives somewhere obvious, and it's harder to run apply against prod by accident.

    Paying for what each environment needs

    Dev doesn't need prod's database. Obvious, sure, but when environments are copied by hand it's very common to end up with three identical, expensive setups. With Terraform, size is just a variable:

    variable "db_instance_class" {
      type = map(string)
      default = {
        dev        = "db.t3.micro"    # cheap, good enough to develop against
        staging    = "db.t3.small"    # prod's shape, smaller
        production = "db.r5.large"    # memory-optimized
      }
    }
    
    resource "aws_db_instance" "main" {
      instance_class = var.db_instance_class[var.environment]
      # ... other config
    }
    

    Dev runs on something that costs almost nothing, staging keeps prod's shape at a smaller scale, and prod gets the instance it needs. Doing this across every resource is where most of my savings came from. No clever tricks, just not paying for production-sized hardware in an environment three people use.

    Terraform for everything, not just servers

    Early on I only managed EC2 and RDS with Terraform and left "the small stuff" to the console. The small stuff is exactly what bites you later. These days pretty much everything lives in code:

    • networking: VPCs, subnets, NAT gateways, security groups
    • compute: ECS/Fargate services, autoscaling, load balancers
    • API Gateway routes, CORS and custom domains
    • IAM roles and policies, secrets
    • Route53, certificates, CloudFront
    • WAF rules and rate limits
    • backups and failover

    If it can be created in the console, it can drift in the console. Keeping all of it in Git is what makes an environment reproducible for real.

    Modules: where it starts paying off

    Around the third service you notice you're writing the same 200 lines every time: task definition, service, target group, alarms. That's when a module is worth it:

    module "user_service" {
      source = "./modules/microservice"
    
      service_name    = "user-service"
      environment     = var.environment
      container_image = "myapp/user-service:latest"
      cpu             = var.environment == "production" ? 1024 : 256
      memory          = var.environment == "production" ? 2048 : 512
    
      enable_autoscaling = var.environment == "production"
      min_instances      = var.environment == "production" ? 2 : 1
      max_instances      = var.environment == "production" ? 10 : 2
    }
    

    A new microservice is now a dozen lines, and the decisions (how much CPU in prod, when to autoscale) live in one place. When I fixed a logging setting in the module, every service picked it up on the next apply. I ended up with modules for microservices, databases, API Gateway, monitoring and networking, and adding a service went from most of a day to a few minutes.

    One warning from experience: don't extract a module out of a single use. Wait until you've written the thing twice and actually know which parts change.

    Making production changes less scary

    Applying changes to production still makes me a little nervous, so the workflow is boring on purpose. First, a saved plan:

    terraform plan -out=tfplan
    

    The plan shows exactly what will be created (+), changed (~) or destroyed (-). I read it every time, and I read the destroys twice: a renamed resource can quietly mean "delete the database and create a new one". Then I apply that same saved plan, so what runs is exactly what I reviewed:

    terraform apply tfplan
    

    In CI, terraform fmt -check, terraform validate and tflint run on every pull request, and the plan is posted to the PR, so infrastructure changes get reviewed like any other code.

    Start small, grow when it's justified

    This is the habit that saved me the most. For a new product I don't start with the architecture I'd want at scale. I start with what today's traffic needs and let Terraform make the next step cheap:

    • MVP: one instance, basic monitoring, scaling by hand. Around $50 a month.
    • Growth: autoscaling, a load balancer, better monitoring. Around $200 a month.
    • Scale: Multi-AZ, a CDN, caching. Around $800 a month, once revenue justifies it.
    variable "scaling_tier" {
      type    = string
      default = "mvp" # or "growth" or "scale"
    }
    
    locals {
      instance_count = {
        mvp    = 1
        growth = 2
        scale  = 5
      }
    }
    
    resource "aws_instance" "app" {
      count = local.instance_count[var.scaling_tier]
      # ... config
    }
    

    Moving up a tier means changing one variable and running apply. Because the jump is that easy, I don't feel the urge to over-provision "just in case", which is where a lot of cloud money quietly goes.

    A real example

    The project that convinced me was a platform with eight microservices, an API Gateway, three databases, a message queue and a monitoring stack, across three environments. With the modules and per-environment variables above, bringing up a complete environment took about two hours, and most of that was sorting out permissions and access, not Terraform.

    Before, the same thing took around two weeks. Everything was done by hand, and we depended on people from several teams finding the time to set up what we needed, so most of those two weeks was simply waiting.

    The other numbers: monthly cost went down 63% after right-sizing dev and staging (that one comes from the cost report, not from my memory), and a new service was live in about ten minutes, which is just the time to commit a copy of the service module and let the pipelines run. Worth more than either: every infrastructure change went through a pull request, so we always knew who changed what, and why.

    What I'd tell myself earlier

    Use remote state from day one. S3 for the state and DynamoDB for locking, so two people can't apply at the same time:

    terraform {
      backend "s3" {
        bucket         = "my-terraform-state"
        key            = "prod/terraform.tfstate"
        region         = "us-east-1"
        dynamodb_table = "terraform-locks"
      }
    }
    

    Keep environments in separate state. Dev and prod never share a state file.

    Pin module versions. A module update you didn't ask for is a nasty surprise in the middle of a plan:

    module "vpc" {
      source  = "terraform-aws-modules/vpc/aws"
      version = "~> 5.0"
    }
    

    Don't hardcode. If a value changes between environments, it's a variable.

    Give every module a README. Future you, or the next person on the team, won't remember what enable_xray does.

    If you're starting today

    Don't try to import your whole account in one go. Pick one service, write its Terraform, bring it up in dev, copy it to staging, and only then promote it to production. When you catch yourself repeating code, make a module. Then move on to the next service.

    It's slower the first week and much faster every week after that. And you finally get to answer "what's running in prod?" by opening a folder instead of a browser tab.

    What did you think?

    Loading reactions...

    Found this article helpful? Share it!

    Help others discover this content by sharing it on your favorite platform.

    Previous articleFrom Junior to Senior: The Shift Nobody Tells You AboutNext articleAdapting to New Jobs: From Well-Structured Teams to Chaotic Startups

    Keep reading

    More Articles
    • May 21, 2026 · 8 min read

      Floci: The AWS Local Emulator That Starts in 24ms and Costs $0

      When LocalStack went paid I tried Floci, a free, MIT-licensed AWS emulator. It starts in 24ms and runs real engines. How I use it with Terraform, and where it still falls short.

    • September 1, 2025 · 6 min read

      Let's Talk Microservices: Lessons Learned in the Real World

      I have split monoliths into microservices and merged microservices back into a monolith. Using a bank as the example: when they pay off, when they don't, and what to get right.

    • April 28, 2026 · 9 min read

      Observability When Code Ships Faster Than You Can Read It

      AI tools get code to production faster than ever. How Datadog, Splunk and Sentry changed the way I run backend, frontend and DevOps work, with the incidents that taught me why.

    Don't miss the next post

    Secure · Private · Unsubscribe

    listerineh.dev

    Full-stack engineer building AI agents, cloud platforms and web products. Based in Quito, working remotely.

    Open to new projects

    Work

    • Projects
    • Experience
    • Skills
    • Blog
    • Talks

    About me

    • About
    • Why Listerineh?
    • Contact

    Connect

    • GitHub
    • LinkedIn
    • Instagram
    • Email

    © 2026 Sebastian Alvarez · All rights reserved.

    • Privacy Policy
    • Terms of Use
    • Unsubscribe from the newsletter
    • Component Library