-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
226 lines (185 loc) · 5.57 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
#terraform {
# backend "remote" {
# hostname = "my.scalr.com"
# organization = "org-sfgari365m7sck0"
# workspaces {
# name = "iacp-ha-install"
# }
# }
#}
locals {
ssh_private_key_file = "./ssh/id_rsa"
license_file = "./license/license.json"
# Currently forces server_count = 1. When multiple servers allowed will limit to the number of subnets
server_count = min(length(data.aws_subnet_ids.scalr_ids),var.server_count,1)
}
locals {
linux_types = [
"ubuntu-16.04",
"centos-7",
"centos-8",
"rhel-7",
"rhel-8",
"amazon-2"
]
names = [
"ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*",
"CentOS Linux 7 x86_64*",
"SupportedImages CentOS Linux 8 x86_64*",
"SupportedImages RHEL-7.*",
"SupportedImages RHEL-8.*",
"amzn2-ami-hvm-2.0*"
]
owners = [
"099720109477", #CANONICAL
"679593333241",
"679593333241",
"679593333241",
"679593333241",
"amazon"
]
users = [
"ubuntu",
"centos",
"centos",
"ec2-user",
"ec2-user",
"ec2-user"
]
}
provider "aws" {
region = var.region
}
# Obtain the AMI for the region
data "aws_ami" "the_ami" {
most_recent = true
filter {
name = var.ami != "" ? "image-id" : "name"
values = [var.ami != "" ? var.ami : element(local.names,index(local.linux_types,var.linux_type))]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
owners = [var.ami != "" ? var.ami_owner : element(local.owners,index(local.linux_types,var.linux_type))]
}
data "aws_subnet_ids" "scalr_ids" {
vpc_id = var.vpc
}
###############################
#
# Scalr Server
#
resource "aws_instance" "iacp_server" {
count = local.server_count
ami = data.aws_ami.the_ami.id
instance_type = var.instance_type
key_name = var.ssh_key_name
vpc_security_group_ids = [ data.aws_security_group.default_sg.id, aws_security_group.scalr_sg.id ]
subnet_id = var.subnet != "" ? var.subnet : element(tolist(data.aws_subnet_ids.scalr_ids.ids),count.index)
tags = merge(
map( "Name", "${var.name_prefix}-iacp-server-${tostring(count.index)}"),
var.tags )
connection {
host = self.public_ip
type = "ssh"
user = element(local.users,index(local.linux_types,var.linux_type))
private_key = file(local.ssh_private_key_file)
timeout = "20m"
}
provisioner "file" {
source = local.license_file
destination = "/var/tmp/license.json"
}
provisioner "file" {
source = "./SCRIPTS/scalr_install.sh"
destination = "/var/tmp/scalr_install.sh"
}
}
resource "aws_ebs_volume" "iacp_vol" {
count = local.server_count
availability_zone = aws_instance.iacp_server[count.index].availability_zone
type = "gp2"
size = 50
tags = var.tags
}
resource "aws_volume_attachment" "iacp_attach" {
count = local.server_count
device_name = "/dev/sds"
instance_id = aws_instance.iacp_server[count.index].id
volume_id = aws_ebs_volume.iacp_vol[count.index].id
}
## Certificate
resource "tls_private_key" "scalr_pk" {
algorithm = "RSA"
}
resource "tls_self_signed_cert" "scalr_cert" {
key_algorithm = "RSA"
private_key_pem = tls_private_key.scalr_pk.private_key_pem
subject {
common_name = var.public == true ? aws_instance.iacp_server.0.public_dns : aws_instance.iacp_server.0.private_dns
organization = "Scalr"
}
validity_period_hours = 336
allowed_uses = [
"key_encipherment",
"digital_signature",
"server_auth",
]
}
resource "null_resource" "null_1" {
depends_on = [aws_instance.iacp_server]
count = local.server_count
connection {
host = aws_instance.iacp_server[count.index].public_ip
type = "ssh"
user = element(local.users,index(local.linux_types,var.linux_type))
private_key = file(local.ssh_private_key_file)
timeout = "20m"
}
provisioner "file" {
content = tls_self_signed_cert.scalr_cert.cert_pem
destination = "/var/tmp/my.crt"
}
provisioner "file" {
content = tls_private_key.scalr_pk.private_key_pem
destination = "/var/tmp/my.key"
}
provisioner "remote-exec" {
inline = [
"chmod +x /var/tmp/scalr_install.sh",
"sudo /var/tmp/scalr_install.sh '${var.token}' ${aws_volume_attachment.iacp_attach[count.index].volume_id} ${var.public == true ? aws_instance.iacp_server[count.index].public_dns : aws_instance.iacp_server[count.index].private_dns}"
]
}
}
resource "null_resource" "get_info" {
depends_on = [null_resource.null_1 ]
count = local.server_count
connection {
host = aws_instance.iacp_server[count.index].public_ip
type = "ssh"
user = element(local.users,index(local.linux_types,var.linux_type))
private_key = file(local.ssh_private_key_file)
timeout = "20m"
}
provisioner "file" {
source = "./SCRIPTS/get_pass.sh"
destination = "/var/tmp/get_pass.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /var/tmp/get_pass.sh",
"sudo /var/tmp/get_pass.sh",
]
}
}
output "dns_name" {
value = "https://${var.public == true ? aws_instance.iacp_server.0.public_dns : aws_instance.iacp_server.0.private_dns}"
}
output "scalr_iacp_server_public_ip" {
value = aws_instance.iacp_server.0.public_ip
}