17-EC2 Instances

Module tạo fixed EC2 instances cho 12 roles khác nhau

Stage Modulemodules/17-ec2-instances

Roles

RoleMô tảDefault Count
webWeb frontend1
webphpWeb PHP1
pushPush notification1
mongoMongoDB1
mysqlMySQL primary1
mysql_mirrorMySQL mirror/slave1
redisRedis cache1
node_centerNode center1
node_gameNode game1
node_worldNode world1
node_chatNode chat1
node_commuNode community1

Configuration

EC2 Configuration
# terraform.tfvars

# Enable/disable this module
create_instances = true    # false = infra only, no EC2

# Scale instances per role
instance_counts = {
  web          = 2    # Scale web to 2
  node_commu   = 3    # Scale node_commu to 3
  node_world   = 2
  # Omit key = default 1
  # Set 0 = skip role
}

# DB AMI override (pre-baked snapshots)
ec2_ami_ids_db = {
  mysql        = "ami-prebaked-mysql"
  mysql_mirror = "ami-prebaked-mysql"
}

# Second EBS for mysql/mysql_mirror
ebs_home2_size_gb = 100    # Set 0 to disable

Nguy hiểm

user_data_replace_on_change = true: Thay đổi scripts/*.sh sẽ DESTROY + CREATE lại EC2. Backup dữ liệu trước!

Target Apply

Target Apply
# Replace specific instance
terraform apply -replace='module.ec2_instances.aws_instance.this["mysql-1"]'

# Replace multiple instances
terraform apply \
  -replace='module.ec2_instances.aws_instance.this["mysql-1"]' \
  -replace='module.ec2_instances.aws_instance.this["mysql_mirror-1"]'

Outputs

TênMô tả
instance_idsMap role → instance IDs
private_ipsMap role → private IPs