Terraform modules và configurations để deploy infrastructure trên AWS.
Dự án này cung cấp:
- VPC Module: Tạo VPC với public/private subnets, NAT gateway, Internet Gateway
- EC2 Module: Deploy EC2 instances với EIP, security groups
- RDS Module: Tạo RDS database instances (MySQL, PostgreSQL, MariaDB)
- Security Groups Module: Quản lý security groups cho app và database
- Project Module: Composition module gom EC2 + RDS + Security Groups cho một project
.
├── environments/ # Environment-specific configurations
│ └── prod/
│ └── us-east-1/ # Region-specific config
│ ├── main.tf
│ ├── variables.tf
│ ├── outputs.tf
│ ├── providers.tf
│ ├── data.tf
│ ├── terraform.tfvars.example
│ └── backend.hcl.example
├── modules/ # Reusable Terraform modules
│ ├── vpc/
│ ├── ec2/
│ ├── rds/
│ ├── security-groups/
│ └── project/
└── README.md
- Terraform >= 1.6.0
- AWS CLI configured với credentials
- S3 bucket và DynamoDB table cho Terraform state (xem
backend.hcl.example)
- Tạo S3 bucket cho Terraform state (bucket name phải unique globally):
# Lấy AWS Account ID để tạo unique bucket name
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
REGION="us-east-1"
BUCKET_NAME="terraform-state-${ACCOUNT_ID}-${REGION}"
# Tạo bucket
aws s3 mb s3://${BUCKET_NAME} --region ${REGION}
# Enable versioning (required for state management)
aws s3api put-bucket-versioning \
--bucket ${BUCKET_NAME} \
--versioning-configuration Status=Enabled
# Enable encryption (recommended)
aws s3api put-bucket-encryption \
--bucket ${BUCKET_NAME} \
--server-side-encryption-configuration '{
"Rules": [{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "AES256"
}
}]
}'
# Block public access (security best practice)
aws s3api put-public-access-block \
--bucket ${BUCKET_NAME} \
--public-access-block-configuration \
"BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"
echo "Bucket created: ${BUCKET_NAME}"Lưu ý: Nếu bucket name đã tồn tại, bạn có thể:
- Thêm suffix:
terraform-state-${ACCOUNT_ID}-${REGION}-${RANDOM_SUFFIX} - Hoặc dùng tên công ty:
terraform-state-${COMPANY_NAME}-${REGION} - Hoặc check bucket hiện có:
aws s3 ls | grep terraform-state
- Tạo DynamoDB table cho state locking:
aws dynamodb create-table \
--table-name terraform-state-lock \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--billing-mode PAY_PER_REQUEST \
--region ${REGION}- Copy và config backend:
cd environments/prod/us-east-1
cp backend.hcl.example backend.hcl
# Edit backend.hcl với bucket name và region của bạn- Copy và edit terraform.tfvars:
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars với config của bạn- Initialize Terraform:
terraform init -backend-config=backend.hcl- Plan:
terraform plan- Apply:
terraform applyXem environments/prod/us-east-1/terraform.tfvars.example để biết các variables cần config.
Mỗi project trong projects map sẽ tạo:
- EC2 instances (số lượng configurable)
- RDS database instance
- Security groups (app và database)
Example:
projects = {
myapp = {
project_short = "myapp"
project_full = "my-application"
ec2_purpose = "api"
ec2_instance_type = "t3.small"
ec2_count = 2
rds_engine = "mysql"
rds_instance_class = "db.t3.small"
db_name = "myapp_db"
db_username = "myapp_user"
password_ssm_param = "/myapp/prod/mysql/password"
}
}- Database passwords: Lưu trong AWS Systems Manager Parameter Store (SSM)
- SSH access: Chỉ allow từ specified CIDR blocks
- Encryption:
- RDS storage encrypted
- EC2 root volumes encrypted
- IAM: EC2 instances có thể attach IAM instance profile (configurable)
Sau khi deploy, xem outputs:
terraform outputOutputs bao gồm:
- VPC information (ID, CIDR, ARN)
- Project resources (EC2 instances, RDS endpoints, security groups)
Tạo VPC với:
- Public và private subnets
- Internet Gateway
- NAT Gateway(s) - single hoặc multi-AZ
- Route tables
Tạo EC2 instances với:
- Auto-assign public IP
- Optional Elastic IP
- Encrypted root volume
- Optional IAM instance profile
- User data support
Tạo RDS instances với:
- Multi-engine support (MySQL, PostgreSQL, MariaDB)
- Encryption enabled
- Backup và maintenance windows
- Optional Performance Insights
- Optional enhanced monitoring
Tạo security groups:
- App SG: HTTP/HTTPS từ specified CIDRs, optional SSH
- RDS SG: Database access từ App SG only
Composition module gom:
- Security Groups
- RDS instance
- EC2 instances (multiple, distributed across AZs)
- State Management: Luôn dùng S3 backend với DynamoDB locking
- Secrets: Không commit
terraform.tfvarshoặc files chứa secrets - Versioning: Pin Terraform và provider versions
- Tags: Tất cả resources đều có tags cho cost allocation
- Multi-AZ: EC2 instances được distribute across availability zones
- Tạo feature branch
- Make changes
- Test với
terraform plan - Submit pull request
[Your License Here]
Platform Team