BÀI 5: Bài lab Terraform triển khai EC2 theo hướng Module Chuyên mục Devops 2025-05-15 1 Lượt xem 0 Lượt thích 0 Bình luận
🎯 Mục tiêu
-
Tạo một EC2 instance sử dụng module riêng biệt
-
Học cách tổ chức project Terraform theo dạng module
-
Hiểu quá trình gọi module, truyền biến, và xuất output
📁 1. Cấu trúc thư mục dự án
terraform-ec2-module-lab/
├── main.tf
├── variables.tf
├── outputs.tf
├── terraform.tfvars
├── modules/
│ └── ec2/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
🔧 2. Viết module EC2
modules/ec2/main.tf
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = var.instance_type
subnet_id = var.subnet_id
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
key_name = var.key_name
associate_public_ip_address = true
tags = {
Name = "Terraform-EC2"
}
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow SSH"
vpc_id = var.vpc_id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
modules/ec2/variables.tf
variable "ami_id" {}
variable "instance_type" {}
variable "key_name" {}
variable "subnet_id" {}
variable "vpc_id" {}
modules/ec2/outputs.tf
output "instance_id" {
value = aws_instance.web.id
}
output "public_ip" {
value = aws_instance.web.public_ip
}
🌐 3. File ở thư mục gốc (root module)
main.tf
provider "aws" {
region = var.region
}
data "aws_vpc" "default" {
default = true
}
data "aws_subnet" "default" {
id = var.subnet_id
}
module "ec2_instance" {
source = "./modules/ec2"
ami_id = var.ami_id
instance_type = var.instance_type
key_name = var.key_name
subnet_id = var.subnet_id
vpc_id = data.aws_vpc.default.id
}
variables.tf
variable "region" {
default = "us-east-1"
}
variable "ami_id" {}
variable "instance_type" {}
variable "key_name" {}
variable "subnet_id" {}
terraform.tfvars
ami_id = "ami-026c39f4021df9abe"
key_name = "liveapp-dev-key"
instance_type = "t2.micro"
subnet_id = "subnet-07c430403014d1538"
outputs.tf
output "instance_id" {
value = module.ec2_instance.instance_id
}
output "public_ip" {
value = module.ec2_instance.public_ip
}
▶️ 4. Thực thi Terraform
Chạy các lệnh sau trong thư mục terraform-ec2-module-lab:
terraform init
terraform plan
terraform apply
Khi apply xong, bạn sẽ thấy output:
Outputs:
instance_id = "i-xxxxxxxxxxxxxxxxx"
public_ip = "3.88.xxx.xxx"
💡 Giải thích nhanh:
Thành phần | Vai trò |
---|---|
module "ec2_instance" | Gọi module từ ./modules/ec2 |
variables.tf + terraform.tfvars | Khai báo biến và giá trị |
outputs.tf | Hiển thị thông tin sau khi tạo EC2 |
data "aws_vpc" | Tự động lấy VPC mặc định |
data "aws_subnet" | Lấy subnet đã chỉ định sẵn |
✅ Kết quả mong đợi
Bạn đã triển khai thành công EC2 instance qua module với:
-
Tách code gọn gàng
-
Dễ dàng tái sử dụng module ec2 cho các môi trường khác
-
Có output để truy cập IP public
🔁 Nâng cao thêm
Bạn có thể:
-
Gói module thành module Terraform riêng biệt và dùng qua Git
-
Viết thêm module vpc, s3, rds để mở rộng hệ thống
Bình luận (0)