-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
205 lines (166 loc) · 4.83 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
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
197
198
199
200
201
202
203
204
205
## VPC:
resource "aws_vpc" "new-vpc" {
count = "${var.create_vpc == 1 ? 1 : 0}"
cidr_block = "${var.vpc_cidr}"
enable_dns_support = true
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = "${var.use_ipv6 == 0 ? true : false}"
tags = {
Project = "kali"
ManagedBy = "terraform"
}
}
data "aws_vpc" "vpc" {
id = "${var.create_vpc == 0 ? var.vpc_id : "${join("", aws_vpc.new-vpc.*.id)}"}"
}
## Subnets
resource "aws_subnet" "public-subnet" {
count = "${var.create_vpc == 1 ? 1 : 0}"
vpc_id = "${data.aws_vpc.vpc.id}"
cidr_block = "${var.subnet_cidr_block}"
assign_ipv6_address_on_creation = "${var.use_ipv6 == true ? true : false}"
tags = {
Project = "kali"
ManagedBy = "terraform"
}
depends_on = ["aws_vpc.new-vpc"]
}
data "aws_subnet" "subnet" {
id = "${var.subnet_id != "" ? var.subnet_id : "${join("", aws_subnet.public-subnet.*.id)}"}"
}
## Internet Gateway
resource "aws_internet_gateway" "igw" {
count = "${var.create_vpc == 1 ? 1 : 0}"
vpc_id = "${data.aws_vpc.vpc.id}"
tags = {
Project = "kali"
ManagedBy = "terraform"
}
}
## Route Table
resource "aws_route_table" "rt-ipv6" {
count = "${var.create_vpc * var.use_ipv6 == 1 ? 1 : 0}"
vpc_id = "${aws_vpc.new-vpc[count.index].id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.igw[count.index].id}"
}
route {
ipv6_cidr_block = "::/0"
gateway_id = "${aws_internet_gateway.igw[count.index].id}"
}
tags = {
Project = "kali"
ManagedBy = "terraform"
}
}
resource "aws_route_table" "rt-ipv4only" {
count = "${var.create_vpc * var.use_ipv4only == 1 ? 1 : 0}"
vpc_id = "${aws_vpc.new-vpc[count.index].id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.igw[count.index].id}"
}
tags = {
Project = "kali"
ManagedBy = "terraform"
}
}
resource "aws_route_table_association" "rtassoc" {
count = "${var.create_vpc == 1 ? 1 : 0}"
subnet_id = "${aws_subnet.public-subnet[count.index].id}"
route_table_id = "${var.use_ipv6 == 1 ? "${join("", aws_route_table.rt-ipv6.*.id)}" : "${join("", aws_route_table.rt-ipv4only.*.id)}"}"
}
## Security Group
resource "aws_security_group" "sg-ipv6" {
count = "${var.use_ipv6 == 1 ? 1 : 0}"
name = "kali-ipv6"
description = "Allow all IPv4 and IPv6 for kali"
vpc_id = "${data.aws_vpc.vpc.id}"
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_blocks = ["::/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Project = "kali"
ManagedBy = "terraform"
}
}
resource "aws_security_group" "sg-ipv4-only" {
count = "${var.use_ipv4only == 1 ? 1 : 0}"
name = "kali-ipv4only"
description = "Allow all IPv4 for kali"
vpc_id = "${data.aws_vpc.vpc.id}"
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Project = "kali"
ManagedBy = "terraform"
}
}
# data "aws_security_group" "sg" {
# id = "${var.create_vpc * var.use_ipv6 == 0 ? aws_security_group.sg-ipv6.id : aws_security_group.sg-ipv4-only.id}"
# }
## SSH Key pair
resource "aws_key_pair" "ssh_key_pair" {
count = "${var.ssh_key_pair_name == "" ? 1 : 0}"
key_name = "kali-ssh-key"
public_key = "${file("${var.public_key_path}")}"
}
locals {
key_pair_name = "${var.ssh_key_pair_name == "" ? aws_key_pair.ssh_key_pair.0.key_name : var.ssh_key_pair_name}"
}
## EC2 instance
## TODO: check why the AMI doesnt use this userdata:
locals {
kali-userdata = <<USERDATA
#!/bin/bash
apt-get update
apt-get dist-upgrade -y
USERDATA
}
resource "aws_instance" "kali_machine" {
ami = "${var.packer_ami}"
instance_type = "${var.ec2_instance_type}"
monitoring = false
vpc_security_group_ids = ["${var.use_ipv6 == 1 ? "${join("", aws_security_group.sg-ipv6.*.id)}" : "${join("", aws_security_group.sg-ipv4-only.*.id)}"}"]
associate_public_ip_address = true
subnet_id = "${data.aws_subnet.subnet.id}"
key_name = "${local.key_pair_name}"
source_dest_check = false
user_data_base64 = "${base64encode(local.kali-userdata)}"
tags = {
Project = "kali"
ManagedBy = "terraform"
}
}