LogoTRUONG PHAM
Home
Projects
Blogs
YouTube
Contact

Newsletter

Stay updated with technical artifacts and engineering insights.

LogoTRUONG PHAM

Building scalable software and sharing insights on technology & life.

Sitemap

  • Home
  • Projects
  • Blogs
  • YouTube
  • Contact

Connect

  • GitHub
  • LinkedIn
  • Email
  • YouTube

© 2024 TRUONG PHAM. © All rights reserved.

Privacy PolicyTerms of Service
Back
Infra as Code (IaC) - Automate or Die in a Mountain of Config
Microservice Journey: Lessons & Trade-offs

Infra as Code (IaC) - Automate or Die in a Mountain of Config

Managing 10 databases and 10 services manually is a nightmare. Why we had to put infrastructure into code even when the team was only 2 people?

TP
Truong PhamSoftware Engineer
PublishedMarch 28, 2024
Stack
microservice ·infrastructure ·terraform ·devops

Doing Microservices without IaC is like trying to move a whole forest by carrying one handful of soil at a time. Sooner or later, you'll burn out or confuse an oak tree for a pine tree.


Context: The "Click-Ops" Trap

In the beginning, we only had 1 RDS database on AWS and 2 services running on EC2. Clicking through the AWS web interface to configure things felt relatively fast and intuitive.

But as we grew to 10 services, each service needed:

  • A separate Database user.
  • A queue on NATS.
  • A unique set of Environment Variables.
  • S3 access policies...

Forgetting to check a single checkbox or mistyping an environment variable began to happen more frequently. We'd spend an entire morning just trying to find out why Service A couldn't connect to the DB, only to realize someone had misconfigured a Security Group.

1. What is Infra as Code (IaC)?

Simply put, you describe your infrastructure (Servers, DB, Network) using code (usually Terraform .tf files or .yaml if using CloudFormation).

Why is it important for small teams?

  • Reproducibility: Want to create a Staging environment identical to Production? Just run one command. No need to remember everything you clicked on the console months ago.
  • Change History: Infrastructure code is also pushed to Git. Who changed the DB RAM? Who opened port 8080? It's all in the git log.
  • Safety: You can review infrastructure code just like business logic code before applying it.

2. Tool Choice: Don't Overload the Brain

We debated between Terraform and Pulumi.

  • Terraform: Most popular, huge community. But you have to learn HCL.
  • Pulumi: Let's you use the programming language you're already using (like TypeScript/Go) to write infrastructure.

Our choice: Terraform. Because when the system fails, you need a stable tool with the most answers on StackOverflow.

3. Trade-off: Initial Time Investment

Learning IaC and writing code for it takes 3-4 times longer than clicking through a web interface in the early stages. But trust me: This investment pays for itself by the second month, when you need to scale the system or create a new service.


Lessons Learned

  1. Tagging is Vital: Every resource created with code must be Tagged (e.g., Project, Env, Owner). It helps you know where the money is going when you look at the Cloud bill.
  2. Don't "Import" Manually: Once you use IaC, absolutely do not go to the Console to change anything. If you do, the state of reality and the state in the code will drift (State drift)—the source of many bizarre errors.
  3. Start Small: Don't try to code the entire system at once. Start by IaC-ifying the hardest things to remember, like Networking and Database users.

Conclusion

IaC is not just for large corporations. For a small team managing a complex microservice architecture, IaC is the "third hand" that helps you maintain sanity and precision in a world full of configurations.

Series • Part 11 of 20

Microservice Journey: Lessons & Trade-offs

NextThe Pain Named Cloud Bill - Cost Optimization for 'Broke Teams'
Idempotency - Why Every API Should Be 'Stubborn'?
06Authentication & Authorization - When API Gateway Carries the Team07Docker Compose is Not Enough - The Local Development Story08Database Separation - A Painful but Necessary 'Divorce'09BFF (Backend for Frontend) - The Savior of UX10Idempotency - Why Every API Should Be 'Stubborn'?11Infra as Code (IaC) - Automate or Die in a Mountain of ConfigReading12The Pain Named Cloud Bill - Cost Optimization for 'Broke Teams'13When the System Goes Silent - Lessons on Post-mortem and Blame-free Culture14Contract Testing - When Integration Testing Becomes a Burden15AI-Service Integration - Bringing LLM/RAG into Microservice Architecture16Feature Flags - Deploy and Release are No Longer One
TP

Written by Truong Pham

Software Engineer passionate about building high-performance systems and meaningful experiences.

Read more articles