-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.tf
80 lines (69 loc) · 1.82 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
variable "name" {
default = "public"
}
variable "cidrs" {
default = []
}
variable "azs" {
description = "A list of availability zones"
default = []
}
variable "vpc_id" {
}
variable "igw_id" {
}
variable "map_public_ip_on_launch" {
default = true
}
variable "tags" {
description = "A map of tags to add to all resources"
default = {}
}
# Subnet
resource "aws_subnet" "public" {
vpc_id = "${var.vpc_id}"
cidr_block = "${element(var.cidrs, count.index)}"
availability_zone = "${element(var.azs, count.index)}"
count = "${length(var.cidrs)}"
map_public_ip_on_launch = "${var.map_public_ip_on_launch}"
lifecycle {
create_before_destroy = true
}
tags = "${merge(var.tags, map("Name", format("%s.%s", var.name, element(var.azs, count.index))))}"
}
# Routes
resource "aws_route_table" "public" {
vpc_id = "${var.vpc_id}"
count = "${length(var.cidrs)}"
tags = "${merge(var.tags, map("Name", format("%s.%s", var.name, element(var.azs, count.index))))}"
}
resource "aws_route_table_association" "public" {
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${element(aws_route_table.public.*.id, count.index)}"
count = "${length(var.cidrs)}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "igw" {
route_table_id = "${element(aws_route_table.public.*.id, count.index)}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${var.igw_id}"
count = "${length(var.cidrs)}"
depends_on = [
"aws_route_table.public"
]
lifecycle {
create_before_destroy = true
}
}
output "subnet_ids" {
value = [
"${aws_subnet.public.*.id}"
]
}
output "public_route_table_ids" {
value = [
"${aws_route_table.public.*.id}"
]
}