-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-splunk.tf
293 lines (246 loc) · 6.8 KB
/
main-splunk.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Create a VPC
resource "aws_vpc" "my-vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "splunk VPC"
}
}
# Create Web Public Subnet
resource "aws_subnet" "web-subnet" {
vpc_id = aws_vpc.my-vpc.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "${var.aws_region}a"
tags = {
Name = "splunk-subnet"
}
}
# Create Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my-vpc.id
tags = {
Name = "splunk IGW"
}
}
# Create Web layber route table
resource "aws_route_table" "web-rt" {
vpc_id = aws_vpc.my-vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "WebRT"
}
}
# Create Web Subnet association with Web route table
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.web-subnet.id
route_table_id = aws_route_table.web-rt.id
}
# Create Web Security Group
resource "aws_security_group" "web-sg" {
name = "splunk security group"
description = "Allow ssh inbound traffic"
vpc_id = aws_vpc.my-vpc.id
ingress {
description = "ssh from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "ssh from VPC splunk server"
from_port = 8000
to_port = 8000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "ssh from VPC splunk forwader"
from_port = 9997
to_port = 9997
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "http port"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Open port for JFOG
ingress {
description = "ssh from VPC splunk forwader"
from_port = 8081
to_port = 8081
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"]
}
tags = {
Name = "splunk-SG"
}
}
#data for amazon linux
data "aws_ami" "amazon_linux_2" {
most_recent = true
owners = ["amazon"]
filter {
name = "owner-alias"
values = ["amazon"]
}
filter {
name = "name"
values = ["amzn2-ami-hvm*"]
}
}
#create ec2 instances
resource "aws_instance" "splunk-server" {
ami = data.aws_ami.amazon_linux_2.id
instance_type = var.aws_instance_type_server
subnet_id = aws_subnet.web-subnet.id
vpc_security_group_ids = ["${aws_security_group.web-sg.id}"]
key_name = aws_key_pair.ec2-key.key_name
#user_data = file("splunk_script.sh")
# Set the instance's root volume to 30 GB
root_block_device {
volume_size = 30
}
tags = {
Name = "splunk-server"
owner = "splunk"
Environment = "dev"
}
provisioner "file" {
source = "${path.module}/scripts_for_splunk_server"
destination = "/home/ec2-user/"
connection {
type = "ssh"
user = "ec2-user"
#private_key = file("splunkkey.pem")
private_key = file(local_file.ssh_key.filename)
host = self.public_ip
timeout = "1m"
}
}
}
resource "aws_instance" "splunk-forwarder" {
ami = data.aws_ami.amazon_linux_2.id
instance_type = var.aws_instance_type_forwader
subnet_id = aws_subnet.web-subnet.id
vpc_security_group_ids = ["${aws_security_group.web-sg.id}"]
key_name = aws_key_pair.ec2-key.key_name
#user_data = file("splunk_forwarder_script.sh")
# Set the instance's root volume to 30 GB
root_block_device {
volume_size = 30
}
tags = {
Name = "splunk-forwarder"
owner = "splunk"
Environment = "dev"
}
provisioner "file" {
source = "${path.module}/scripts_for_splunk_forwarder"
destination = "/home/ec2-user/"
connection {
type = "ssh"
user = "ec2-user"
#private_key = file("splunkkey.pem")
private_key = file(local_file.ssh_key.filename)
host = self.public_ip
timeout = "1m"
}
}
provisioner "file" {
source = "${path.module}/script_for_forwarder_config"
destination = "/home/ec2-user/"
connection {
type = "ssh"
user = "ec2-user"
#private_key = file("splunkkey.pem")
private_key = file(local_file.ssh_key.filename)
host = self.public_ip
timeout = "1m"
}
}
}
# just install the splunk forwader script
resource "null_resource" "install_splunk_forwarder" {
# ssh into the ec2 instance
connection {
type = "ssh"
user = "ec2-user"
private_key = file(local_file.ssh_key.filename)
host = aws_instance.splunk-forwarder.public_ip
}
# set permissions and run the file
provisioner "remote-exec" {
inline = [
"ls",
"pwd",
# Install httpd
"sh scripts_for_splunk_forwarder/splunk_forwarder_script.sh",
]
}
# wait for ec2 to be created
depends_on = [aws_instance.splunk-forwarder]
}
# just install the splunk server script
resource "null_resource" "install_splunk_server" {
# ssh into the ec2 instance
connection {
type = "ssh"
user = "ec2-user"
private_key = file(local_file.ssh_key.filename)
host = aws_instance.splunk-server.public_ip
}
# set permissions and run the file
provisioner "remote-exec" {
inline = [
"ls",
"pwd",
# Install httpd
"sh scripts_for_splunk_server/splunk_script.sh",
]
}
# wait for ec2 to be created
depends_on = [aws_instance.splunk-server]
}
# Wait for scripts to be installed in the first two null_resources before installing the last null_resource.
resource "null_resource" "name" {
# ssh into the ec2 instance
connection {
type = "ssh"
user = "ec2-user"
private_key = file(local_file.ssh_key.filename)
host = aws_instance.splunk-forwarder.public_ip
}
# set permissions and run the file
provisioner "remote-exec" {
inline = [
"ls",
"pwd",
# Install httpd
"sh script_for_forwarder_config/apache_installation.sh",
# Install JFROG
"sh script_for_forwarder_config/jfrog_installation_from_scratch.sh",
# Create the users
"sudo sh script_for_forwarder_config/create_users.sh script_for_forwarder_config/user_informations.txt",
# End configuration on forwader
"sudo sh script_for_forwarder_config/config_host_forwader.sh ${aws_instance.splunk-server.public_ip}",
]
}
# wait the 2 null resource that install splunk and splunk forwarder
depends_on = [null_resource.install_splunk_server, null_resource.install_splunk_forwarder]
}