GitHub - 5a6io/OliveSafety: Cloud Wave 3๊ธฐ ํ๋ก์ ํธ olivesafety
Cloud Wave 3๊ธฐ ํ๋ก์ ํธ olivesafety. Contribute to 5a6io/OliveSafety development by creating an account on GitHub.
github.com
Cloud Wave์์ ํ๋ก์ ํธ๋ฅผ ์ํํ์ ๋ ์ฝ์๋ก ์์ ํด Terraform ์ฝ๋๋ก ์์ฑํด๋ณด๋ ค๊ณ ํ๋ค. ๋จผ์ Network์ ๊ด๋ จ๋ ๋ชจ๋์ ์์ฑํด๋ณด๊ฒ ๋ค.
๐Network ๋ชจ๋ ๊ตฌ์ฑ์ ํ์ํ ๋ฆฌ์์ค
VPC: aws_vpcSubnet: aws_subentInternet Gateway&NAT: aws_internet_gateway, aws_route_table, aws_route, aws_eip, aws_nat_gateway, aws_route_table_association
โ๏ธVPC ๋ชจ๋
main.tf
resource "aws_vpc" "this" {
cidr_block = var.vpc_cidr #"10.0.1.0/24"
enable_dns_support = true
enable_dns_hostnames = false
instance_tenancy = "default"
tags = merge(var.common_tags, {
Name = "${var.project_name}-vpc"
})
}
outputs.tf
output "vpc_id" {
value = aws_vpc.this.id
}
variables.tf
variable "project_name" {
type = string
}
variable "common_tags" {
type = map(string)
default = {}
}
variable "vpc_cidr" {
type = string
}
โ๏ธSubnet ๋ชจ๋
main.tf
resource "aws_subnet" "public" {
for_each = { for idx, cidr in var.pub_sub_cidr : idx => { cidr = cidr, az = var.availability_zones[idx]}}
vpc_id = var.vpc_id
cidr_block = each.value.cidr
availability_zone = each.value.az
map_public_ip_on_launch = true
tags = merge(var.common_tags, {
Name = "${var.project_name}-pub-sub-${each.key + 1}"
})
}
resource "aws_subnet" "private" {
for_each = { for idx, cidr in var.pri_sub_cidr : idx => {cidr = cidr, az = var.availability_zones[idx]}}
vpc_id = var.vpc_id
cidr_block = each.value.cidr
availability_zone = each.value.az
tags = merge(var.common_tags, {
Name = "${var.project_name}-pri-sub-${each.key + 1}"
})
}
outputs.tf
output "public_subnets" {
value = values(aws_subnet.public[*].id)
}
output "private_subnets" {
value = values(aws_subnet.private[*].id)
}
vaiables.tf
variable "project_name" {
type = string
description = "ํ๋ก์ ํธ ์ด๋ฆ"
}
variable "common_tags" {
type = map(string)
default = {}
}
variable "vpc_id" {
type = string
}
variable "availability_zones" {
type = list(string)
}
variable "pub_sub_cidr" {
type = list(string)
}
variable "pri_sub_cidr" {
type = list(string)
}
โ๏ธInternet Gateway์ NAT ๋ชจ๋
main.tf
# Internet Gateway
resource "aws_internet_gateway" "this" {
vpc_id = var.vpc_id
tags = merge(var.common_tags, {
Name = "${var.project_name}-igw"
})
}
# Public Route Table + Internet Gateway
resource "aws_route_table" "rtb_pub" {
vpc_id = var.vpc_id
tags = merge(var.common_tags, {
Name = "${var.project_name}-route-pub-${each.key + 1}"
})
}
resource "aws_route" "rt_pub_route" {
route_table_id = aws_route_table.rtb-pub-01
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
resource "aws_route_table_association" "pub_association" {
count = length(var.pub_sub_ids)
route_table_id = aws_route_table.rtb_pub.id
subnet_id = each.value.id[count.index]
}
# NAT
resource "aws_eip" "this" {
count = var.multi_nat ? lenght(var.pub_sub_ids) : 1
domain = "vpc"
tags = merge(var.common_tags, {
Name = "${var.project_name}-eip-nat-${count.index + 1}"
})
}
resource "aws_nat_gateway" "this" {
count = var.multi_nat ? length(var.pub_sub_ids) : 1
subnet_id = var.pub_sub_ids[count.index]
allocation_id = aws_eip.this[count.index].id
tags = merge(var.common_tags, {
Name = "${var.project_name}-nat-${count.index + 1}"
})
depends_on = [ aws_internet_gateway.this ]
}
# Private Route Table + NAT
resource "aws_route_table" "rtb_pri" {
count = var.multi_nat ? length(var.pri_sub_ids) : 1
vpc_id = var.vpc_id
tags = merge(var.common_tags, {
Name = "${var.project_name}-rtb-pri-${count.index + 1}"
})
}
resource "aws_route" "rt_pri_rtb" {
count = var.multi_nat ? length(var.pri_sub_ids) : 1
route_table_id = aws_route_table.rtb_pri[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.this[var.multi_nat ? count.index : 0].id
}
resource "aws_route_table_association" "pri_association" {
count = length(var.pri_sub_ids)
route_table_id = aws_route_table.rtb_pri[var.multi_nat ? count.index : 0].id
subnet_id = var.pri_sub_ids[count.index]
}
outputs.tf
output "private_route_table_ids" {
value = aws_route_table.rtb_pri[*].id
}
output "public_route_table_ids" {
value = aws_route_table.rtb_pub[*].id
}
vaiables.tf
variable "project_name" {
type = string
}
variable "common_tags" {
type = map(string)
default = {}
}
variable "vpc_id" {
type = string
}
variable "pub_sub_ids" {
type = list(string)
}
variable "pri_sub_ids" {
type = list(string)
}
variable "multi_nat" {
type = bool
}
outputs.tf ํ์ผ์ ์๋ ๋ด์ฉ์ ๋ชจ๋์ ํธ์ถํ์ ๋ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํ๋ ๊ฒ์ด๋ค. ๊ทธ๋ฆฌ๊ณ variables.tf ํ์ผ์ ์๋ ๋ด์ฉ์ ํ๊ฒฝ๋ณ์๋ฅผ ํตํด ๋ฐ์์ค๋ ค๊ณ ํ๋ ๊ฒ์ด๋ค.
๐๋ชจ๋ ์์ฑ
์์์ ์ ์ํ ๋ชจ๋์ ์๋์ ๊ฐ์ด ํธ์ถํ์ฌ ์ฌ์ฉํ๋ฉด ๋๋ค.
stacks/02_network.tf
module "vpc" {
source = "../modules/network/vpc"
project_name = var.project_name
common_tags = var.common_tags
vpc_cidr = var.vpc_cidr
}
module "subnets" {
source = "../modules/network/subnets"
project_name = var.project_name
common_tags = var.common_tags
vpc_id = module.vpc.vpc_id
availability_zones = var.availability_zones
pub_sub_cidr = var.pub_sub_cidr
pri_sub_cidr = var.pri_sub_cidr
}
module "igw_nat" {
source = "../modules/network/igw-nat"
project_name = var.project_name
common_tags = var.common_tags
vpc_id = module.vpc.vpc_id
pub_sub_ids = module.subnets.public_subnets
pri_sub_ids = module.subnets.private_subnets
multi_nat = var.multi_nat
}
๊ทธ๋ฆฌ๊ณ ์ฌ๊ธฐ์๋ ๋ง์ฐฌ๊ฐ์ง๋ก variables.tf๋ก ์ธ๋ถ์์ ๋ฐ์์ฌ ๋ณ์๋ฅผ ์ ์ธํ๊ณ terraform applyํ ๋ ํ๊ฒฝ๋ณ์๋ฅผ ์ ๋ ฅํ๋ฉด ๋๋ค.
๋ค์์ IAM๊ณผ Security Group๊ณผ ๊ด๋ จํด ์์ฑํด๋ณด๊ฒ ๋ค.
์์ฑํ ์ฝ๋๋ ์๋ ๊นํ๋ธ์์ ๋ณผ ์ ์์ต๋๋ค.
GitHub - 5a6io/Cloud-Wave-Project-Terraform: Cloud Wave 3๊ธฐ ํ๋ก์ ํธ Terraform ์ฝ๋
Cloud Wave 3๊ธฐ ํ๋ก์ ํธ Terraform ์ฝ๋. Contribute to 5a6io/Cloud-Wave-Project-Terraform development by creating an account on GitHub.
github.com
'Cloud > IaC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [IaC] Terraform Module๋ก AWS IAM๊ณผ Security Group ์์ฑ (0) | 2025.11.12 |
|---|---|
| [IaC] Terraform module ์์ฑ (1) | 2025.07.18 |
| [IaC] Terraform์ผ๋ก AWS ์ธํ๋ผ ๊ด๋ฆฌ ๋ฐ ์๋ํ (0) | 2025.06.30 |
| [IaC] ํ ๋ผํผ(Terraform)์ด๋? (0) | 2025.06.28 |