-
Notifications
You must be signed in to change notification settings - Fork 68
/
vpc.tf
92 lines (82 loc) · 2.08 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
provider "aws" {
region = "${var.aws_region}"
}
# VPC
resource "aws_vpc" "terra_vpc" {
cidr_block = "${var.vpc_cidr}"
enable_dns_hostnames = true
tags = {
Name = "Terraform_VPC"
}
}
# Internet Gateway
resource "aws_internet_gateway" "terra_igw" {
vpc_id = "${aws_vpc.terra_vpc.id}"
tags = {
Name = "main"
}
}
# Subnets : public
resource "aws_subnet" "public" {
count = "${length(var.subnets_cidr)}"
vpc_id = "${aws_vpc.terra_vpc.id}"
cidr_block = "${element(var.subnets_cidr,count.index)}"
availability_zone = "${element(var.azs,count.index)}"
map_public_ip_on_launch = true
tags = {
Name = "Subnet-${count.index+1}"
}
}
# Route table: attach Internet Gateway
resource "aws_route_table" "public_rt" {
vpc_id = "${aws_vpc.terra_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.terra_igw.id}"
}
tags = {
Name = "publicRouteTable"
}
}
# Route table association with public subnets
resource "aws_route_table_association" "a" {
count = "${length(var.subnets_cidr)}"
subnet_id = "${element(aws_subnet.public.*.id,count.index)}"
route_table_id = "${aws_route_table.public_rt.id}"
}
resource "aws_security_group" "webservers" {
name = "allow_http"
description = "Allow http inbound traffic"
vpc_id = "${aws_vpc.terra_vpc.id}"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "ssh from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "webservers" {
count = var.number_instances
ami = var.amiid
instance_type = var.instance_type
security_groups = ["${aws_security_group.webservers.id}"]
subnet_id = "${element(aws_subnet.public.*.id,count.index)}"
user_data = "${file("install_httpd.sh")}"
key_name = var.key
tags = {
Name = "Server-${count.index}"
}
}