Skip to content

Commit e8e2a2e

Browse files
committed
[#1] feat - add vpc module
- vpc 생성을 위한 테라폼 모듈 정의
1 parent b6f1d09 commit e8e2a2e

File tree

7 files changed

+365
-0
lines changed

7 files changed

+365
-0
lines changed

terraform/aws/modules/vpc/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# VPC 생성 모듈
2+
3+
## Inputs
4+
5+
| Name | Description | Type | Default | Required |
6+
| ------------------ | ------------------------------------------- | :----: | :-----: | :------: |
7+
| name | 모듈에서 정의하는 모든 리소스 이름의 prefix | string | n/a | yes |
8+
| cidr | VPC에 할당한 CIDR block | string | n/a | yes |
9+
| public_subnets | Public Subnet IP 리스트 | list | n/a | yes |
10+
| private_subnets | Private Subnet IP 리스트 | list | n/a | yes |
11+
| database_subnets | Database Subnet IP 리스트 | list | n/a | yes |
12+
| availability_zones | 사용할 availability zones 리스트 | list | n/a | yes |
13+
| bastion_id | bastion instance id | string | n/a | yes |
14+
| nat_gateway_enable | nat gateway 설정 여부, false인 경우 instance 사용 | string | n/a | yes |
15+
| tags | 모든 리소스에 추가되는 tag 맵 | map | n/a | yes |
16+
17+
## Outputs
18+
19+
| Name | Description |
20+
| ------------------------- | ------------------------------- |
21+
| database_subnet_group_ids | Database Subnet Group ID 리스트 |
22+
| database_subnets_ids | Database Subnet ID 리스트 |
23+
| default_network_acl_id | VPC default network ACL ID |
24+
| default_security_group_id | VPC default Security Group ID |
25+
| igw_id | Interget Gateway ID |
26+
| private_route_table_ids | Private Route Table ID 리스트 |
27+
| private_subnets_ids | Private Subnet ID 리스트 |
28+
| public_route_table_ids | Public Route Table ID 리스트 |
29+
| public_subnets_ids | Public Subnet ID 리스트 |
30+
| vpc_cidr_block | VPC에 할당한 CIDR block |
31+
| vpc_id | VPC ID |
32+
33+
## Reference
34+
35+
Vpc 모듈과 Bastion을 생성하는 terraform code는 ausbubam님의 블로그를 참고하였습니다.
36+
37+
[ausbubam blog](https://blog.2dal.com/2017/10/28/aws-vpc-with-terraform-modules/)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# EIP for NAT gateway
2+
resource "aws_eip" "nat" {
3+
count = var.nat_gateway_enable ? length(var.availability_zones) : 0
4+
vpc = true
5+
}
6+
7+
# NAT gateway
8+
resource "aws_nat_gateway" "this" {
9+
count = var.nat_gateway_enable ? length(var.availability_zones) : 0
10+
allocation_id = aws_eip.nat.*.id[count.index]
11+
subnet_id = aws_subnet.public.*.id[count.index]
12+
}
13+
14+
# private route table
15+
resource "aws_route_table" "nat_gateway_private" {
16+
count = var.nat_gateway_enable ? length(var.availability_zones) : 0
17+
18+
vpc_id = aws_vpc.this.id
19+
20+
route {
21+
cidr_block = "0.0.0.0/0"
22+
23+
nat_gateway_id = aws_nat_gateway.this.*.id[count.index]
24+
}
25+
26+
tags = merge(var.tags, {
27+
"Name" = format("%s-private-%s", var.name, var.availability_zones[count.index])
28+
})
29+
}

terraform/aws/modules/vpc/output.tf

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# VPC
2+
output "vpc_id" {
3+
description = "VPC ID"
4+
value = aws_vpc.this.id
5+
}
6+
7+
output "vpc_cidr_block" {
8+
description = "VPC에 할당한 CIDR block"
9+
value = aws_vpc.this.cidr_block
10+
}
11+
12+
output "default_security_group_id" {
13+
description = "VPC default Security Group ID"
14+
value = aws_vpc.this.default_security_group_id
15+
}
16+
17+
output "default_network_acl_id" {
18+
description = "VPC default network ACL ID"
19+
value = aws_vpc.this.default_network_acl_id
20+
}
21+
22+
# internet gateway
23+
output "igw_id" {
24+
description = "Interget Gateway ID"
25+
value = aws_internet_gateway.this.id
26+
}
27+
28+
# subnets
29+
output "private_subnets_ids" {
30+
description = "Private Subnet ID 리스트"
31+
value = aws_subnet.private.*.id
32+
}
33+
34+
output "public_subnets_ids" {
35+
description = "Public Subnet ID 리스트"
36+
value = aws_subnet.public.*.id
37+
}
38+
39+
output "database_subnets_ids" {
40+
description = "Database Subnet ID 리스트"
41+
value = aws_subnet.database.*.id
42+
}
43+
44+
output "database_subnet_group_ids" {
45+
description = "Database Subnet Group ID 리스트"
46+
value = aws_db_subnet_group.database.*.id
47+
}
48+
49+
# route tables
50+
output "public_route_table_ids" {
51+
description = "Public Route Table ID 리스트"
52+
value = aws_route_table.public.*.id
53+
}
54+
55+
output "private_route_table_ids" {
56+
description = "Private Route Table ID 리스트"
57+
value = aws_route_table.private.*.id
58+
}
59+
60+
# NAT gateway
61+
output "nat_ids" {
62+
description = "NAT Gateway에 할당된 EIP ID 리스트"
63+
value = [aws_eip.nat.*.id]
64+
}
65+
output "nat_public_ips" {
66+
description = "NAT Gateway에 할당된 EIP 리스트"
67+
value = [aws_eip.nat.*.public_ip]
68+
}
69+
output "natgw_ids" {
70+
description = "NAT Gateway ID 리스트"
71+
value = [aws_nat_gateway.this.*.id]
72+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
variable "name" {
2+
description = "모듈에서 정의하는 모든 리소스 이름의 prefix"
3+
type = string
4+
}
5+
6+
variable "cidr" {
7+
description = "VPC에 할당한 CIDR block"
8+
type = string
9+
}
10+
11+
variable "public_subnets" {
12+
description = "Public Subnet IP 리스트"
13+
type = list(string)
14+
}
15+
16+
variable "private_subnets" {
17+
description = "Private Subnet IP 리스트"
18+
type = list(string)
19+
}
20+
21+
variable "database_subnets" {
22+
description = "Database Subnet IP 리스트"
23+
type = list(string)
24+
}
25+
26+
variable "availability_zones" {
27+
description = "사용할 availability zones 리스트"
28+
type = list(string)
29+
}
30+
31+
variable "bastion_id" {
32+
description = "bastion instance id"
33+
type = string
34+
}
35+
36+
variable "nat_gateway_enable" {
37+
description = "nat gateway enabled"
38+
default = false
39+
}
40+
41+
variable "tags" {
42+
description = "모든 리소스에 추가되는 tag 맵"
43+
type = map(string)
44+
}

terraform/aws/modules/vpc/versions.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# public subnet
2+
resource "aws_subnet" "public" {
3+
count = length(var.public_subnets)
4+
5+
vpc_id = aws_vpc.this.id
6+
cidr_block = var.public_subnets[count.index]
7+
availability_zone = var.availability_zones[count.index]
8+
9+
tags = merge(var.tags, {
10+
"Name" = format("%s-public-%s", var.name, var.availability_zones[count.index]),
11+
})
12+
}
13+
14+
# private subnet
15+
resource "aws_subnet" "private" {
16+
count = length(var.private_subnets)
17+
18+
vpc_id = aws_vpc.this.id
19+
cidr_block = var.private_subnets[count.index]
20+
availability_zone = var.availability_zones[count.index]
21+
22+
tags = merge(var.tags, {
23+
"Name" = format("%s-private-%s", var.name, var.availability_zones[count.index]),
24+
})
25+
}
26+
27+
# private database subnet
28+
resource "aws_subnet" "database" {
29+
count = length(var.database_subnets)
30+
31+
vpc_id = aws_vpc.this.id
32+
cidr_block = var.database_subnets[count.index]
33+
availability_zone = var.availability_zones[count.index]
34+
35+
tags = merge(var.tags, {
36+
"Name" = format("%s-db-%s", var.name, var.availability_zones[count.index])
37+
})
38+
}
39+
40+
resource "aws_db_subnet_group" "database" {
41+
count = length(var.database_subnets) > 0 ? 1 : 0
42+
43+
name = var.name
44+
description = "Database subnet group for ${var.name}"
45+
subnet_ids = aws_subnet.database.*.id
46+
47+
tags = merge(var.tags, {
48+
"Name" = format("%s", var.name)
49+
})
50+
}
51+
52+
# public route table
53+
resource "aws_route_table" "public" {
54+
vpc_id = aws_vpc.this.id
55+
56+
route {
57+
cidr_block = "0.0.0.0/0"
58+
gateway_id = aws_internet_gateway.this.id
59+
}
60+
61+
tags = merge(var.tags, {
62+
"Name" = format("%s-public", var.name)
63+
})
64+
}
65+
66+
# private route table
67+
resource "aws_route_table" "private" {
68+
count = var.nat_gateway_enable ? 0 : length(var.availability_zones)
69+
70+
vpc_id = aws_vpc.this.id
71+
72+
route {
73+
cidr_block = "0.0.0.0/0"
74+
instance_id = var.bastion_id
75+
}
76+
77+
tags = merge(var.tags, {
78+
"Name" = format("%s-private-%s", var.name, var.availability_zones[count.index])
79+
})
80+
}
81+
82+
# route table association
83+
resource "aws_route_table_association" "public" {
84+
count = length(var.public_subnets)
85+
86+
subnet_id = aws_subnet.public[count.index].id
87+
route_table_id = aws_route_table.public.id
88+
}
89+
90+
resource "aws_route_table_association" "private" {
91+
count = length(var.private_subnets)
92+
93+
subnet_id = aws_subnet.private[count.index].id
94+
route_table_id = var.nat_gateway_enable ? aws_route_table.nat_gateway_private[count.index].id :aws_route_table.private[count.index].id
95+
}
96+
97+
resource "aws_route_table_association" "database" {
98+
count = length(var.database_subnets)
99+
100+
subnet_id = aws_subnet.database[count.index].id
101+
route_table_id = var.nat_gateway_enable ? aws_route_table.nat_gateway_private[count.index].id :aws_route_table.private[count.index].id
102+
}
103+

terraform/aws/modules/vpc/vpc.tf

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# VPC
2+
resource "aws_vpc" "this" {
3+
cidr_block = var.cidr
4+
enable_dns_hostnames = true
5+
enable_dns_support = true
6+
instance_tenancy = "default"
7+
8+
tags = merge(var.tags, {
9+
"Name" = format("%s", var.name)
10+
})
11+
}
12+
13+
# internet gateway
14+
resource "aws_internet_gateway" "this" {
15+
vpc_id = aws_vpc.this.id
16+
17+
tags = merge(var.tags, {
18+
"Name" = format("%s", var.name)
19+
})
20+
}
21+
22+
# default network ACL
23+
resource "aws_default_network_acl" "dev_default" {
24+
default_network_acl_id = aws_vpc.this.default_network_acl_id
25+
26+
ingress {
27+
protocol = -1
28+
rule_no = 100
29+
action = "allow"
30+
cidr_block = "0.0.0.0/0"
31+
from_port = 0
32+
to_port = 0
33+
}
34+
35+
egress {
36+
protocol = -1
37+
rule_no = 100
38+
action = "allow"
39+
cidr_block = "0.0.0.0/0"
40+
from_port = 0
41+
to_port = 0
42+
}
43+
44+
subnet_ids = concat(
45+
aws_subnet.public.*.id,
46+
aws_subnet.private.*.id,
47+
aws_subnet.database.*.id)
48+
49+
tags = merge(var.tags, {
50+
"Name" = format("%s-default", var.name)
51+
})
52+
}
53+
54+
# default security group
55+
resource "aws_default_security_group" "dev_default" {
56+
vpc_id = aws_vpc.this.id
57+
58+
ingress {
59+
protocol = -1
60+
self = true
61+
from_port = 0
62+
to_port = 0
63+
}
64+
65+
egress {
66+
from_port = 0
67+
to_port = 0
68+
protocol = "-1"
69+
cidr_blocks = [
70+
"0.0.0.0/0"
71+
]
72+
}
73+
74+
tags = merge(var.tags, {
75+
"Name" = format("%s-default", var.name)
76+
})
77+
}

0 commit comments

Comments
 (0)