-
Notifications
You must be signed in to change notification settings - Fork 6
/
instance.tf
196 lines (163 loc) · 5.12 KB
/
instance.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# -----------------------------------------------------------
# set up a VPC, subnet and EC2 instance so we have something to log
# -----------------------------------------------------------
resource "aws_vpc" "example" {
cidr_block = "${var.example_vpc_cidr}"
enable_dns_support = true
enable_dns_hostnames = true
tags = "${merge(map("Name", "log-example"), var.tags)}"
}
resource "aws_internet_gateway" "example" {
vpc_id = "${aws_vpc.example.id}"
tags = "${merge(map("Name", "log-example"), var.tags)}"
}
resource "aws_route_table" "example" {
vpc_id = "${aws_vpc.example.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.example.id}"
}
tags = "${merge(map("Name", "log-example"), var.tags)}"
}
resource "aws_route_table_association" "example" {
subnet_id = "${aws_subnet.example.id}"
route_table_id = "${aws_route_table.example.id}"
}
resource "aws_subnet" "example" {
vpc_id = "${aws_vpc.example.id}"
cidr_block = "${var.example_subnet_cidr}"
map_public_ip_on_launch = true
tags = "${merge(map("Name", "log-example"), var.tags)}"
}
# -----------------------------------------------------------
# lock off default NACL, then add a new set
# -----------------------------------------------------------
resource "aws_default_network_acl" "example_default" {
default_network_acl_id = "${aws_vpc.example.default_network_acl_id}"
tags = "${merge(map("Name", "log-example"), var.tags)}"
}
resource "aws_network_acl" "example" {
vpc_id = "${aws_vpc.example.id}"
subnet_ids = ["${aws_subnet.example.id}"]
tags = "${merge(map("Name", "log-example"), var.tags)}"
}
resource "aws_network_acl_rule" "example_ssh_in" {
count = "${length(var.ssh_inbound)}"
network_acl_id = "${aws_network_acl.example.id}"
rule_number = "${100 + count.index}"
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "${var.ssh_inbound[count.index]}"
from_port = 22
to_port = 22
}
resource "aws_network_acl_rule" "example_ephemeral_in" {
network_acl_id = "${aws_network_acl.example.id}"
rule_number = 200
egress = false
protocol = "tcp"
rule_action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 1024
to_port = 65535
}
resource "aws_network_acl_rule" "example_ephemeral_out" {
count = "${length(var.ssh_inbound)}"
network_acl_id = "${aws_network_acl.example.id}"
rule_number = "${100 + count.index}"
egress = true
protocol = "tcp"
rule_action = "allow"
cidr_block = "${var.ssh_inbound[count.index]}"
from_port = 1024
to_port = 65535
}
resource "aws_network_acl_rule" "example_http_out" {
network_acl_id = "${aws_network_acl.example.id}"
rule_number = 200
egress = true
protocol = "tcp"
rule_action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 80
to_port = 80
}
# -----------------------------------------------------------
# lock off default Security Group, then add a new set
# -----------------------------------------------------------
resource "aws_default_security_group" "example_default" {
vpc_id = "${aws_vpc.example.id}"
tags = "${merge(map("Name", "log-example"), var.tags)}"
}
resource "aws_security_group" "example_ssh_access" {
name = "example-ssh"
description = "allows ssh access to the example host"
vpc_id = "${aws_vpc.example.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.ssh_inbound}"]
}
egress {
from_port = 1024
to_port = 65535
protocol = "tcp"
cidr_blocks = ["${var.ssh_inbound}"]
}
}
resource "aws_security_group" "http_out_access" {
name = "example-http-out"
description = "allows instance to reach out on port 80"
vpc_id = "${aws_vpc.example.id}"
ingress {
from_port = 1024
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# -----------------------------------------------------------
# finally, create an EC2 instance
# -----------------------------------------------------------
data "aws_ami" "target_ami" {
most_recent = true
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["${var.ami_name}"]
}
}
resource "aws_instance" "example" {
ami = "${data.aws_ami.target_ami.id}"
instance_type = "${var.instance_type}"
key_name = "${var.example_key}"
subnet_id = "${aws_subnet.example.id}"
vpc_security_group_ids = [
"${aws_security_group.example_ssh_access.id}",
"${aws_security_group.http_out_access.id}",
]
root_block_device = {
volume_type = "gp2"
volume_size = "${var.root_vol_size}"
}
tags = "${merge(map("Name", "log-example"), var.tags)}"
volume_tags = "${var.tags}"
user_data = <<EOF
#!/bin/bash
yum update -y -q
yum erase -y -q ntp*
yum -y -q install chrony git
service chronyd start
EOF
}