When I was new to DevOps, I used to be obsessed with setting up the server by hand. Every time I deployed I was a "run for your life" because I wasn't sure if everything would run stably. The application of Terraform infrastructure as code base has completely changed the landscape. It helps me turn the entire complex cloud infrastructure into simple, easy-to-read and easy-to-manage code files. Forget about risky manual clicking, now you can automate everything in the safest and most consistent way.
Escape from the "click-ops" life: Why are Infrastructure as Code (IaC) and Terraform true love?
Infrastructure as Code (IaC) and Terraform help replace manual configuration (click-ops) with programmable code, bringing absolute consistency, speed, and version control to cloud infrastructure.
Previously, to build an application running environment, we had to log in to the provider's web interface, click create network, create server, open security port... Extremely time-consuming. If you have ever had to Basic Linux server administration for developers, you will understand how easily configuring each server manually can lead to human error. When the system grows up, not being able to remember what you have configured on which server is a real "nightmare".
What is Infrastructure as Code (IaC)? Savior for sleepless nights.
Infrastructure as Code (IaC) is a method of managing and provisioning computer resources through machine-readable definition files, instead of configuring physical hardware or manual interactive tools.
Instead of manual operations, you write code. This source code uses a Declarative approach, meaning you just need to describe the "end result" you want (e.g. I need 3 servers), the IaC tool will figure out how to do it on its own. Furthermore, because infrastructure is now code, you can store it on a Version Control system. If you're not familiar with this field, Learning Git GitHub from scratch for developers is a great stepping stone to managing infrastructure change history.
What is Terraform? "Handbook" for your infrastructure.
Terraform is an open source tool developed by HashiCorp that allows you to define and provision data center infrastructure using a declarative configuration language.
As of March 2026, with stable versions like 1.14.x and beta 1.15, Terraform has become the undisputed industry standard. Unlike tools that only run on one platform, Terraform stands out thanks to its Cloud-agnostic (platform independent) capabilities. You can share a single workflow to manage resources across AWS, Azure, Google Cloud, and thousands of other services.
5 Terraform benefits your boss and team will love (with real-life examples).
Terraform benefits include infrastructure automation, Multi-cloud support, intelligent state management, code reuse, and easy integration into CI/CD pipelines.
At Pham Hai, I always encourage teams to apply Terraform for the following practical reasons:
- Tốc độ triển khai hạ tầng: Thay vì mất nửa ngày tạo môi trường staging, bạn chỉ cần chạy một lệnh và chờ 5 phút.
- Multi-cloud linh hoạt: Bạn không bị "khóa" vào một nhà cung cấp. Bạn có thể dùng AWS cho database và Cloudflare cho DNS trong cùng một dự án.
- Quản lý tài nguyên an toàn: Terraform biết chính xác những gì đang chạy nhờ file trạng thái Terraform.
- Tích hợp CI/CD mượt mà: Hạ tầng có thể được test và deploy tự động. Việc kết hợp Terraform với GitHub Actions tự động deploy test CI/CD giúp mọi thay đổi đều được kiểm duyệt chặt chẽ trước khi lên Production.
- Tái sử dụng code: Viết code một lần cho môi trường Dev, và dùng lại y hệt cho môi trường Prod chỉ bằng cách đổi vài tham số.
Instructions for installing and "Hello World" with Terraform in 15 minutes
You can easily download and install Terraform CLI directly from the HashiCorp homepage for Windows, macOS or Linux operating systems with just a few basic command lines.
Install Terraform CLI on Windows, macOS, and Linux: Easy as pie.
The simplest way to install Terraform is to use package managers like Homebrew for macOS, Chocolatey for Windows or apt/yum for Linux to download the latest Terraform CLI.
- macOS: Mở terminal và gõ
brew tap hashicorp/tapsau đóbrew install hashicorp/tap/terraform. - Windows: Nếu dùng Chocolatey, bạn chỉ cần chạy
choco install terraform. (Lưu ý: Từ năm 2026, Terraform đã hỗ trợ cả bản build cho Windows ARM64). - Linux (Ubuntu): Sử dụng
apt-getđể cài đặt khóa GPG của HashiCorp và tải góiterraformchính thức.
Sau khi cài xong, hãy gõ terraform -version để kiểm tra. Nếu màn hình in ra phiên bản (ví dụ v1.14.7), chúc mừng bạn đã thành công!
Structure of a basic Terraform file: Get familiar with HCL (HashiCorp Configuration Language).
HCL (HashiCorp Configuration Language) is a visual declarative language designed specifically for Terraform, helping to define infrastructure structures in a way that is easy to read for both humans and machines.
Một file code Terraform (thường có đuôi .tf) được cấu tạo từ các "block". Mỗi block có một loại (type), nhãn (labels) và thân (body) chứa các đối số (arguments). Ngôn ngữ này rất thân thiện, không cần ngoặc kép phức tạp như JSON, hỗ trợ comment rõ ràng, giúp team DevOps đọc vào là hiểu ngay hệ thống đang có những gì.
Basic Terraform example: Create an EC2 virtual machine on AWS.
Để tạo máy ảo EC2, bạn chỉ cần khai báo provider AWS và block resource "aws_instance" kèm theo AMI và loại instance mong muốn trong file main.tf.
Below is the classic "Hello World" code. Note, to run this example, you should refer to how to create a AWS account for beginners free tier so as not to incur costs:
provider "aws" {
region = "ap-southeast-1"
}
resource "aws_instance" "my_first_server" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t2.micro"
tags = {
Name = "HelloWorld-Terraform"
}
}
With just 11 lines of code above, you have defined an EC2 virtual machine located in the Singapore area.
Master Terraform's core concepts to confidently "code" the infrastructure
To master Terraform, you need to understand four core concepts: Providers, State, Resources/Data Sources and Modules. Master this part, you will never be confused when reading other people's code.
Terraform Providers: Bridge to the Cloud world (AWS, Azure, Google Cloud).
Terraform Providers are plugins that allow Terraform to interact with APIs of cloud platforms (like AWS, Azure) or other SaaS services.
Hiện tại có hơn 3,000 providers trên Terraform Registry. Không chỉ giới hạn ở các gã khổng lồ, nếu công ty bạn đang theo hướng đa nền tảng, bạn hoàn toàn có thể tìm hiểu Google Cloud Platform hướng dẫn cơ bản và sử dụng provider google cùng lúc với aws.
Terraform even manages more than just virtual machines. If you're working with containers, using Terraform to configure Docker for developers practical guide is completely feasible. At a higher level, the Terraform provider for Kubernetes also helps you automate cluster resource allocation synchronously. If your team is aiming for microservices, mastering Basic Kubernetes for Beginners combined with Terraform will be an extremely powerful "weapon".
What is Terraform State? The "logbook" tracks all changes to the infrastructure.
Terraform State là một file (thường là terraform.tfstate) lưu trữ trạng thái hiện tại của hạ tầng thực tế để so sánh với cấu hình code, giúp Terraform biết cần phải thay đổi những gì.
Đây là "trái tim" của Terraform. Khi bạn đổi instance type từ t2.micro sang t3.micro, Terraform đọc file State, nhận ra sự khác biệt và chỉ cập nhật máy chủ đó thay vì tạo lại từ đầu. Với các dự án thực tế, chúng ta không bao giờ lưu file này ở máy cá nhân mà sẽ lưu trên Remote Backend (như AWS S3) để cả team cùng làm việc mà không bị xung đột (State locking).
Resource & Data Source: Two indispensable pieces of the puzzle.
Resource is used to create new infrastructure components (such as VPC, EC2), while Data Source is used to query information about existing resources.
- Resource: Là những thứ bạn chủ động yêu cầu Terraform tạo ra. Ví dụ:
resource "aws_vpc" "main" {...}. - Data Source: Là cách bạn "đọc" thông tin từ Cloud mang về dùng trong code. Ví dụ bạn muốn lấy ID của một AMI Ubuntu mới nhất do AWS cung cấp mà không cần copy/paste thủ công, bạn sẽ dùng
data "aws_ami" "ubuntu" {...}.
What are Terraform Modules? The art of packaging and reusing infrastructure code.
Terraform Modules are collections of Terraform configuration files grouped in a folder, helping to package, reuse and share infrastructure code in a scientific way.
Thay vì viết đi viết lại đoạn code tạo VPC dài 200 dòng cho 5 dự án khác nhau, bạn đóng gói nó thành một Module my-vpc. Ở các dự án sau, bạn chỉ cần gọi lại module đó và truyền vào tham số (như dải IP). Nó hoạt động giống hệt như việc bạn viết các hàm (functions) trong lập trình phần mềm vậy.
Basic Terraform commands you'll use every day
Terraform's standard workflow revolves around four core commands: init, plan, apply and destroy, which help control the entire lifecycle of the cloud infrastructure.
terraform init: Bước khởi đầu cho mọi dự án.
Lệnh Terraform init có chức năng khởi tạo thư mục làm việc, tải xuống các Providers cần thiết và cấu hình backend lưu trữ State.
Mỗi khi bạn clone một project mới về hoặc thêm một provider mới vào code, đây là lệnh đầu tiên bạn bắt buộc phải chạy. Nó tạo ra thư mục ẩn .terraform để chứa các plugin.
terraform plan: Xem trước thay đổi, không lo "sập" server.
Lệnh Terraform plan tạo ra một bản kế hoạch thực thi, cho phép bạn xem trước chính xác những tài nguyên nào sẽ được tạo, sửa hoặc xóa mà chưa áp dụng thực tế.
Đây là tính năng "cứu mạng" của Terraform. Nó in ra màn hình một danh sách với các dấu + (tạo mới), - (xóa), và ~ (cập nhật). Bạn phải kiểm tra thật kỹ kết quả của lệnh này trước khi đi tiếp.
terraform apply: "Chấp thuận" và triển khai hạ tầng.
Lệnh Terraform apply sẽ thực thi các thay đổi đã được lên kế hoạch trong bước plan để chính thức tạo hoặc cập nhật tài nguyên trên môi trường Cloud.
Khi chạy lệnh này, Terraform sẽ hỏi bạn gõ yes để xác nhận lần cuối. Sau đó, nó sẽ gọi các API của nhà cung cấp đám mây để hiện thực hóa các dòng code của bạn.
terraform destroy: Dọn dẹp tài nguyên khi không cần nữa.
The Terraform destroy command is used to delete all infrastructure resources defined in the configuration, helping to save costs when no longer in use. Very useful for Test/Dev environments. After using it for the day, run this command at the end of the day to clean up, avoid forgetting to turn off the server, leading to a huge cloud computing bill at the end of the month.
Quick comparison: Terraform vs. CloudFormation - When to choose who?
As of 2026, Terraform stands out with its Multi-cloud capabilities and huge ecosystem, while CloudFormation is the optimal choice if you only use the AWS ecosystem.
Both are great tools for implementing IaC. However, the choice depends a lot on your company's long-term strategy.
Flexibility: What are Cloud-agnostic and Multi-cloud?
Cloud-agnostic means not being dependent on a single provider. Terraform has excellent Multi-cloud support with more than 3000 providers, while CloudFormation is "vendor locked-in" with AWS. If your company plans to use AWS for the backend, but uses Azure for Active Directory, Terraform is the only way to go. CloudFormation cannot manage non-AWS resources as easily and natively as Terraform.
Community and ecosystem: Who is more crowded?
Terraform owns a huge open source community and Terraform Registry contains thousands of available Modules, superior to CloudFormation's closed ecosystem. When you encounter an error with Terraform, you just need to copy the error to Google and there will be dozens of solutions on StackOverflow or GitHub. The popularity of Terraform in the DevOps world today is undeniable.
Getting started with Terraform infrastructure as code basics is not as scary as you think. It is an extremely valuable skill in the modern DevOps and Cloud world. At Pham Hai, I always advise young people not to just consider it a tool, but to see it as a new way of thinking about infrastructure management: automatic, safe and controllable. With this basic knowledge, I believe you are ready to "code" your first infrastructure yourself and say goodbye to boring manual work forever.
Are you ready to automate your infrastructure? Try the EC2 virtual machine creation example above and share your results in the comments!
Lưu ý: Các thông tin trong bài viết này chỉ mang tính chất tham khảo. Để có được lời khuyên tốt nhất, vui lòng liên hệ trực tiếp với chúng tôi để được tư vấn cụ thể dựa trên nhu cầu thực tế của bạn.