Terraform Modules
Vibe Prompt
「幫我建立一個 Terraform Module 用於建立標準的 VPC 架構,包含公有/私有子網、NAT Gateway。」
# modules/vpc/main.tf
variable "vpc_cidr" {}
variable "public_subnets" { type = list(string) }
variable "private_subnets" { type = list(string) }
variable "environment" {}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = { Name = "vpc-${var.environment}" }
}
resource "aws_subnet" "public" {
count = length(var.public_subnets)
vpc_id = aws_vpc.main.id
cidr_block = var.public_subnets[count.index]
map_public_ip_on_launch = true
tags = { Name = "public-${var.environment}-${count.index}" }
}
resource "aws_subnet" "private" {
count = length(var.private_subnets)
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnets[count.index]
tags = { Name = "private-${var.environment}-${count.index}" }
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = { Name = "igw-${var.environment}" }
}
output "vpc_id" { value = aws_vpc.main.id }
output "public_subnet_ids" { value = aws_subnet.public[*].id }
output "private_subnet_ids" { value = aws_subnet.private[*].id }
使用 Module
module "vpc" {
source = "./modules/vpc"
vpc_cidr = "10.0.0.0/16"
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnets = ["10.0.10.0/24", "10.0.20.0/24"]
environment = "production"
}
output "vpc_id" { value = module.vpc.vpc_id }