-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
113 lines (88 loc) · 2.48 KB
/
vpc.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-southeast-2"
}
data "aws_availability_zones" "azs" {
state = "available"
}
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "Prod"
}
}
resource "aws_subnet" "public_subnets" {
count = length(var.public_subnet_cidrs)
vpc_id = aws_vpc.vpc.id
cidr_block = element(var.public_subnet_cidrs, count.index)
availability_zone = data.aws_availability_zones.azs.names[count.index]
tags = {
Name = "Public-${count.index + 1}"
}
}
resource "aws_internet_gateway" "vpc_ig" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "Prod"
}
}
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.vpc_ig.id
}
tags = {
Name = "Prod"
}
}
resource "aws_route_table_association" "public_route_table_association" {
count = length(var.public_subnet_cidrs)
subnet_id = element(aws_subnet.public_subnets.*.id, count.index)
route_table_id = aws_route_table.public_route_table.id
}
resource "aws_subnet" "private_subnets" {
count = length(var.private_subnet_cidrs)
vpc_id = aws_vpc.vpc.id
cidr_block = element(var.private_subnet_cidrs, count.index)
availability_zone = data.aws_availability_zones.azs.names[count.index]
tags = {
Name = "Private-${count.index + 1}"
}
}
resource "aws_eip" "nat_ip" {
count = length(aws_subnet.public_subnets)
vpc = true
}
resource "aws_nat_gateway" "gw" {
count = length(aws_subnet.public_subnets)
allocation_id = aws_eip.nat_ip[count.index].id
subnet_id = element(aws_subnet.public_subnets.*.id, count.index)
#subnet_id = each.value.id
}
resource "aws_route_table" "private_route_table" {
count = length(aws_nat_gateway.gw)
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = element(aws_nat_gateway.gw.*.id, count.index)
}
tags = {
Name = "Prod"
}
}
resource "aws_route_table_association" "private_route_table_association" {
count = length(aws_subnet.private_subnets)
subnet_id = element(aws_subnet.private_subnets.*.id, count.index)
route_table_id = element(aws_route_table.private_route_table.*.id, count.index)
}