-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
256 lines (208 loc) · 5.99 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
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
provider "aws" {
region = "us-east-1"
}
variable "namespace" {
type = string
default = "Ubuntu 18.04 & 20.04 Pipeline testing Environment"
}
# Defining Private Key
variable "private_key" {
default = "Ubuntu-key.pem"
}
// Generate the SSH keypair that we’ll use to configure the EC2 instance.
// After that, write the private key to a local file and upload the public key to AWS
resource "tls_private_key" "key" {
algorithm = "RSA"
}
resource "local_file" "private_key" {
filename = "${path.module}/Ubuntu-key.pem"
sensitive_content = tls_private_key.key.private_key_pem
file_permission = "0400"
}
resource "aws_key_pair" "key_pair" {
key_name = local_file.private_key.filename
public_key = tls_private_key.key.public_key_openssh
}
// Create a security group with access to port 22 and port 80 open to serve HTTP traffic
data "aws_vpc" "default" {
default = true
}
resource "aws_security_group" "allow_ssh" {
vpc_id = data.aws_vpc.default.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
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 = var.namespace
}
}
resource "aws_instance" "Ubuntu18" {
ami = "ami-0747bdcabd34c712a"
associate_public_ip_address = true
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
key_name = aws_key_pair.key_pair.key_name
tags = {
Name = "${var.namespace}"
Name = "UBUNTU18"
}
# SSH into instance
connection {
# Host name
host = self.public_ip
# The default username for our AMI
user = "ubuntu"
# Private key for connection
private_key = file(pathexpand(var.private_key))
# Type of connection
type = "ssh"
}
provisioner "file" {
source = "ansible_setup.sh"
destination = "/home/ubuntu/ansible_setup.sh"
}
provisioner "file" {
source = "s3copy.sh"
destination = "/home/ubuntu/s3copy.sh"
}
provisioner "file" {
source = "host-local-Ubuntu"
destination = "/home/ubuntu/host-local-Ubuntu"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo chmod 755 ~/ansible_setup.sh",
"sudo ~/ansible_setup.sh -i",
]
}
}
resource "aws_instance" "Ubuntu20" {
ami = "ami-09e67e426f25ce0d7"
associate_public_ip_address = true
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
key_name = aws_key_pair.key_pair.key_name
tags = {
Name = "${var.namespace}"
Name = "UBUNTU20"
}
# SSH into instance
connection {
# Host name
host = self.public_ip
# The default username for our AMI
user = "ubuntu"
# Private key for connection
private_key = file(pathexpand(var.private_key))
# Type of connection
type = "ssh"
}
provisioner "file" {
source = "ansible_setup.sh"
destination = "/home/ubuntu/ansible_setup.sh"
}
# If you place the CIS or STIG repo in the root directory it will be placed on remote ec2 during creation time.
# provisioner "file" {
# source = "UBUNTU20-CIS"
# destination = "/home/ubuntu/UBUNTU20-CIS"
# }
provisioner "file" {
source = "s3copy.sh"
destination = "/home/ubuntu/s3copy.sh"
}
provisioner "file" {
source = "host-local-Ubuntu"
destination = "/home/ubuntu/host-local-Ubuntu"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo chmod 755 ~/ansible_setup.sh",
"sudo ~/ansible_setup.sh -i",
]
}
}
// generate inventory file
resource "local_file" "inventory" {
filename = "./hosts-ubuntu"
content = <<EOF
# hosts-dev
[SERVERS]
Ubuntu18 ansible_host=${aws_instance.Ubuntu18.public_ip}
Ubuntu20 anisble_hosts=${aws_instance.Ubuntu20.public_ip}
debain10 anisble_hosts=${aws_instance.debian10.public_ip}
[SERVERS.vars]
setup_audit=true
run_audit=true
[local]
control ansible_connection=local
EOF
}
resource "aws_instance" "debian10" {
ami = "ami-07d02ee1eeb0c996c"
associate_public_ip_address = true
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
key_name = aws_key_pair.key_pair.key_name
tags = {
Name = "${var.namespace}"
Name = "debian10"
}
# SSH into instance
connection {
# Host name
host = self.public_ip
# The default username for our AMI
user = "admin"
# Private key for connection
private_key = file(pathexpand(var.private_key))
# Type of connection
type = "ssh"
}
provisioner "file" {
source = "host-local-debian"
destination = "/home/admin/host-local-debian"
}
}
// Output the public_ip and the Ansible command to connect to ec2 instance
output "ec2_instance_ip_Ubuntu18" {
description = "IP address of the EC2 instance"
value = aws_instance.Ubuntu18.public_ip
}
output "Ubuntu18" {
description = "Copy/Paste/Enter - You are in the IRD Testing Pipeline"
value = "ssh -i Ubuntu-key.pem ubuntu@${aws_instance.Ubuntu18.public_dns}"
}
output "ec2_instance_ip_Ubuntu20" {
description = "IP address of the EC2 instance"
value = aws_instance.Ubuntu20.public_ip
}
output "Ubuntu20" {
description = "Copy/Paste/Enter - You are in the IRD Testing Pipeline"
value = "ssh -i Ubuntu-key.pem ubuntu@${aws_instance.Ubuntu20.public_dns}"
}
output "ec2_instance_ip_debian10" {
description = "IP address of the EC2 instance"
value = aws_instance.debian10.public_ip
}
output "debian10" {
description = "Copy/Paste/Enter - You are in the IRD Testing Pipeline"
value = "ssh -i Ubuntu-key.pem admin@${aws_instance.debian10.public_dns}"
}
#Testing.......