-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbluedata_infra_main_worker_gpu.tf
137 lines (114 loc) · 4.16 KB
/
bluedata_infra_main_worker_gpu.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
locals {
gpu_worker_count = var.gpu_worker_count
gpu_worker_instance_type = var.gpu_worker_instance_type
gpu_worker_has_disk_for_df = var.gpu_worker_has_disk_for_df
gpu_worker_instance_type_error = join("\n", [
"gpu_worker_instance_type '${local.gpu_worker_instance_type}' is invalid for Region '${var.region}'.",
"You can check available gpu instance types with './bin/ec2_gpu_instances_types.sh ${var.region}'"
])
}
########## ASSERT GPU WORKER INSTANCE TYPE IS VALID FOR AZ ##########
data "aws_ec2_instance_type_offerings" "gpu_worker_instance_types" {
filter {
name = "instance-type"
values = [ local.gpu_worker_instance_type ]
}
filter {
name = "location"
values = [ var.region ]
}
location_type = "region"
}
resource "null_resource" "gpu_worker_instance_type_validation" {
count = (local.gpu_worker_count > 0 && length(data.aws_ec2_instance_type_offerings.gpu_worker_instance_types.instance_types) == 0) ? 1 : 0
provisioner "local-exec" {
command = "false"
interpreter = [ "bash", "-c", "(tput setaf 1 && echo \"${local.gpu_worker_instance_type_error}\" && tput sgr0 && false)"]
}
}
########## END ASSERTION ##########
resource "aws_instance" "workers_gpu" {
count = local.gpu_worker_count
ami = var.EC2_CENTOS7_AMIS[var.region]
instance_type = local.gpu_worker_instance_type
key_name = aws_key_pair.main.key_name
vpc_security_group_ids = [
module.network.security_group_allow_all_from_client_ip,
module.network.security_group_main_id
]
subnet_id = module.network.subnet_main_id
root_block_device {
volume_type = "gp2"
volume_size = 400
tags = {
Name = "${var.project_id}-worker-gpu-${count.index + 1}-root-ebs"
Project = var.project_id
user = local.user
deployment_uuid = random_uuid.deployment_uuid.result
}
}
tags = {
Name = "${var.project_id}-instance-worker-gpu-${count.index + 1}"
Project = var.project_id
user = local.user
deployment_uuid = random_uuid.deployment_uuid.result
}
depends_on = [
null_resource.gpu_worker_instance_type_validation,
]
}
resource "null_resource" "yum_update_workers_gpu" {
count = local.gpu_worker_count
connection {
type = "ssh"
user = "centos"
host = aws_instance.workers_gpu.*.public_ip[count.index]
private_key = file(var.ssh_prv_key_path)
agent = false
}
provisioner "remote-exec" {
inline = [ "sudo yum update -y -q" ]
}
}
# /dev/sdb
resource "aws_ebs_volume" "worker-gpu-ebs-volumes-sdb" {
count = local.gpu_worker_count
availability_zone = var.az
size = 500
type = "gp2"
tags = {
Name = "${var.project_id}-worker-gpu-${count.index + 1}-ebs-sdb"
Project = var.project_id
user = local.user
deployment_uuid = random_uuid.deployment_uuid.result
}
}
resource "aws_volume_attachment" "worker-gpu-volume-attachment-sdb" {
count = local.gpu_worker_count
device_name = "/dev/sdb"
volume_id = aws_ebs_volume.worker-gpu-ebs-volumes-sdb.*.id[count.index]
instance_id = aws_instance.workers_gpu.*.id[count.index]
# hack to allow `terraform destroy ...` to work: https://github.com/hashicorp/terraform/issues/2957
force_detach = true
}
# /dev/sdc
resource "aws_ebs_volume" "worker-gpu-ebs-volumes-sdc" {
count = local.gpu_worker_has_disk_for_df == true ? local.gpu_worker_count : 0
availability_zone = var.az
size = 500
type = "gp2"
tags = {
Name = "${var.project_id}-worker-gpu-${count.index + 1}-ebs-sdc"
Project = var.project_id
user = local.user
deployment_uuid = random_uuid.deployment_uuid.result
}
}
resource "aws_volume_attachment" "worker-gpu-volume-attachment-sdc" {
count = local.gpu_worker_has_disk_for_df == true ? local.gpu_worker_count : 0
device_name = "/dev/sdc"
volume_id = aws_ebs_volume.worker-gpu-ebs-volumes-sdc.*.id[count.index]
instance_id = aws_instance.workers_gpu.*.id[count.index]
# hack to allow `terraform destroy ...` to work: https://github.com/hashicorp/terraform/issues/2957
force_detach = true
}