-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
94 lines (71 loc) · 1.64 KB
/
main.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
terraform {
provider "aws" {
profile = "default"
region = "us-west-2"
}
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
locals {
public_cidr = ["10.0.0.0/24", "10.0.1.0/24"]
private_cidr = ["10.0.2.0/24", "10.0.3.0/24"]
}
resource "aws_subnet" "public" {
count = length(local.public_cidr)
vpc_id = aws_vpc.main.id
cidr_block = local.public_cidr[count.index]
tags = {
Name = "public${count.index}"
}
}
resource "aws_subnet" "private" {
count = length(local.private_cidr)
vpc_id = aws_vpc.main.id
cidr_block = local.private_cidr[count.index]
tags = {
Name = "private${count.index}"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main"
}
}
resource "aws_eip" "nat" {
count = length(local.public_cidr)
vpc = true
}
resource "aws_nat_gateway" "main" {
count = length(local.public_cidr)
allocation_id = aws_eip.nat[count.index].id
subnet_id = aws_subnet.public[count.index].id
tags = {
Name = "Main gw NAT"
}
# To ensure proper ordering, it is recommended to add an explicit dependency
# on the Internet Gateway for the VPC.
depends_on = [aws_internet_gateway.main]
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "public"
}
}
resource "aws_route_table" "private" {
count = length(local.private_cidr)
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main[count.index].id
}
tags = {
Name = "private ${count.index}"
}
}