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ến | Kiểu | Mô tả |
|---|---|---|
| aws_region | string | Region AWS (vd: ap-southeast-1) |
| stage_index | number | Số thứ tự stage → tự suy CIDR |
| peer_build_vpc_id | string | VPC ID để peering (để trống nếu không peer) |
| ec2_ami_id | string | AMI mặc định cho EC2 |
| create_instances | bool | Bật/tắt module 17-ec2-instances |
| instance_counts | map(number) | Số instance mỗi role |
| asg_min_size | number | ASG 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:
| Step | Việc | Tương tác |
|---|---|---|
| 0 | AWS Login (SSO) | Confirm trên browser |
| 1 | Confirm biến | Enter để giữ, gõ mới để đổi |
| 2 | Sync shared region | Tự động |
| 3 | Preflight check | Enter để tiếp tục |
| 4 | Deploy Shared | Skip nếu existing = true |
| 5 | Deploy Stage | Auto import → plan → Enter → apply |
| 6 | Verify outputs | Tự động |
deploy.ps1 usage
# Usage
cd d:\terraform_svn\terraform-resource\terraform
# Deploy
.\deploy.ps1
# Destroy (keep shared)
.\deploy.ps1 -DestroyOnlypreflight-check.ps1 - Kiểm tra trước deploy
Kiểm tra 10 hạng mục trước khi deploy:
| Check | Mô tả | Auto-fix? |
|---|---|---|
| 0 | AWS Credentials | No |
| 1 | S3 Buckets (4) | Yes — cập nhật existing_s3 |
| 2 | IAM Instance Profile | Yes — cập nhật existing_iam |
| 3 | CloudFront (2) | Yes — cập nhật existing_cloudfront |
| 4 | ECR Repositories (7) | No (info only) |
| 5 | AMI | No |
| 6 | ACM Certificate | No (info only) |
| 7 | VPC Peering | Yes — validate, auto-fix CIDR |
| 7b | CIDR Scan | Yes — đề xuất stage_index trống |
| 8 | Region consistency | No (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.