-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.tf
142 lines (122 loc) · 3.32 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# ----------------------------------------#
#
# | Terraform Main file |
#
# ----------------------------------------#
# File: main.tf
# Author: Vithursan Thangarasa (vithursant)
# ----------------------------------------#
# AWS Provider
provider "aws" {
region = "${var.my_region}"
}
locals {
avail_zone = "${var.avail_zone}"
}
resource "aws_vpc" "main_vpc" {
cidr_block = "${var.my_cidr_block}"
instance_tenancy = "default"
enable_dns_hostnames = true
tags {
Name = "main_vpc"
}
}
resource "aws_internet_gateway" "main_vpc_igw" {
vpc_id = "${aws_vpc.main_vpc.id}"
tags {
Name = "main_vpc_igw"
}
}
resource "aws_default_route_table" "main_vpc_default_route_table" {
default_route_table_id = "${aws_vpc.main_vpc.default_route_table_id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.main_vpc_igw.id}"
}
tags {
Name = "main_vpc_default_route_table"
}
}
resource "aws_subnet" "main_vpc_subnet" {
vpc_id = "${aws_vpc.main_vpc.id}"
cidr_block = "${var.my_cidr_block}"
map_public_ip_on_launch = true
availability_zone = "${local.avail_zone}"
tags {
Name = "main_vpc_subnet"
}
}
resource "aws_default_network_acl" "main_vpc_nacl" {
default_network_acl_id = "${aws_vpc.main_vpc.default_network_acl_id}"
subnet_ids = ["${aws_subnet.main_vpc_subnet.id}"]
ingress {
protocol = -1
rule_no = 1
action = "allow"
// cidr_block = "${var.my_ip}"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
egress {
protocol = -1
rule_no = 2
action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
tags {
Name = "main_vpc_nacl"
}
}
resource "aws_default_security_group" "main_vpc_security_group" {
vpc_id = "${aws_vpc.main_vpc.id}"
# SSH access from anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
# for Jupyter notebook
ingress {
from_port = 8888
to_port = 8888
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"]
}
# for git clone
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [
"0.0.0.0/0"]
}
tags {
Name = "main_vpc_security_group"
}
}
resource "aws_spot_instance_request" "aws_dl_custom_spot" {
ami = "${var.ami_id}"
spot_price = "${var.spot_price}"
instance_type = "${var.instance_type}"
key_name = "${var.my_key_pair_name}"
monitoring = true
associate_public_ip_address = true
count = "${var.num_instances}"
security_groups =
["${aws_default_security_group.main_vpc_security_group.id}"]
subnet_id = "${aws_subnet.main_vpc_subnet.id}"
ebs_block_device = [ {
device_name = "/dev/sdh"
volume_size = "${var.ebs_volume_size}"
volume_type = "gp2"
} ]
tags {
Name = "aws_dl_custom_spot"
}
}