Mô tả mã nguồn

Chi tiết các file Terraform và scripts chính trong dự án

main.tf - File chính

File main.tf gọi tất cả các modules theo thứ tự dependency:

terraform/main.tf (excerpt)
# main.tf - Stage deployment
terraform {
  required_version = ">= 1.5"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# Network layer
module "vpc" {
  source      = "./modules/01-vpc"
  stage_index = var.stage_index
  # ...
}

module "subnets" {
  source = "./modules/02-subnets"
  vpc_id = module.vpc.vpc_id
  # ...
}

# Load Balancer layer
module "alb" {
  source     = "./modules/06-alb"
  vpc_id     = module.vpc.vpc_id
  subnet_ids = module.subnets.subnet_ids
  # ...
}

# Compute layer
module "ec2_instances" {
  source           = "./modules/17-ec2-instances"
  create_instances = var.create_instances
  instance_counts  = var.instance_counts
  # ...
}

variables.tf - Định nghĩa biến

Các biến quan trọng thường cần điều chỉnh:

BiếnKiểuMô tả
aws_regionstringRegion AWS (vd: ap-southeast-1)
stage_indexnumberSố thứ tự stage → tự suy CIDR
peer_build_vpc_idstringVPC ID để peering (để trống nếu không peer)
ec2_ami_idstringAMI mặc định cho EC2
create_instancesboolBật/tắt module 17-ec2-instances
instance_countsmap(number)Số instance mỗi role
asg_min_sizenumberASG min size (0 = không launch)

terraform.tfvars - Giá trị biến

File chứa giá trị cụ thể cho deployment:

terraform/terraform.tfvars
# terraform.tfvars
aws_region  = "ap-southeast-1"
az_1        = "ap-southeast-1a"
az_2        = "ap-southeast-1b"
stage_index = 0

# VPC Peering
peer_build_vpc_id   = "vpc-xxxxxxxxx"
peer_build_vpc_cidr = "172.31.0.0/16"
peer_region         = ""

# EC2
ec2_ami_id        = "ami-xxxxxxxxx"
ec2_instance_type = "t3.medium"
ec2_key_name      = ""

# Fixed EC2 instances (module 17)
create_instances = false
instance_counts = {
  web        = 1
  node_commu = 1
  node_world = 1
}

# ASG (module 09)
asg_min_size         = 0
asg_max_size         = 4
asg_desired_capacity = 0

# Tags
project     = "MILU2"
environment = "stage"

deploy.ps1 - Script deploy tự động

Script PowerShell thực hiện 7 bước tự động:

StepViệcTương tác
0AWS Login (SSO)Confirm trên browser
1Confirm biếnEnter để giữ, gõ mới để đổi
2Sync shared regionTự động
3Preflight checkEnter để tiếp tục
4Deploy SharedSkip nếu existing = true
5Deploy StageAuto import → plan → Enter → apply
6Verify outputsTự động
deploy.ps1 usage
# Usage
cd d:\terraform_svn\terraform-resource\terraform

# Deploy
.\deploy.ps1

# Destroy (keep shared)
.\deploy.ps1 -DestroyOnly

preflight-check.ps1 - Kiểm tra trước deploy

Kiểm tra 10 hạng mục trước khi deploy:

CheckMô tảAuto-fix?
0AWS CredentialsNo
1S3 Buckets (4)Yes — cập nhật existing_s3
2IAM Instance ProfileYes — cập nhật existing_iam
3CloudFront (2)Yes — cập nhật existing_cloudfront
4ECR Repositories (7)No (info only)
5AMINo
6ACM CertificateNo (info only)
7VPC PeeringYes — validate, auto-fix CIDR
7bCIDR ScanYes — đề xuất stage_index trống
8Region consistencyNo (warning only)

Mẹo

Preflight check tự động cập nhật terraform.tfvars với các giá trị đúng. Chỉ cần review và Enter để tiếp tục.