-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autoscaling_mnist_model.tf
129 lines (107 loc) · 3.19 KB
/
Autoscaling_mnist_model.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
resource "aws_security_group" "mnist_model" {
name = "mnist_model"
description = "Allow inbound traffic"
vpc_id = "${module.vpc.vpc_id}"
# The model is using 8500 ports
ingress {
from_port = 8500
to_port = 8500
protocol = "tcp"
# The only people that can communicate with this are
# instances within the security group and instances of the API
security_groups = ["${aws_security_group.mnist_model.id}",
"${aws_security_group.mnist_allow_http.id}"]
}
# Allow access to anywhere
# any port, to any port and can be any protocol
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
#####################################################################
# MNIST Module Autoscaling group with external launch configuration #
#####################################################################
resource "aws_launch_configuration" "model-lc" {
name_prefix = "MNIST-model-lc-"
image_id = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
şecurity_groups = ["${aws_security_group.mnist_model.id}"]
lifecycle {
create_before_destroy = true
}
connection {
user = "ubuntu"
type = "ssh"
private_key = "${file(var.pvt_key)}"
}
provisioner "file" {
source = "C:\\Users\\Roman\\code\\Styria-Mnist-Demo\\model_execution_scripts"
destination = "~/"
}
provisioner "remote-exec" {
inline = [
"chmod +x ~/model_execution_scripts/docker_setup_model.sh",
"cd ~/model_execution_scripts",
"sudo ~/model_execution_scripts/docker_setup_model.sh || ~/model_execution_scripts/docker_setup_model.sh ",
]
}
}
module "model-asg" {
source = "terraform-aws-modules/autoscaling/aws"
name = "MNIST-model-autoscaling"
# Launch configuration
launch_configuration = "${aws_launch_configuration.model-lc.name}"
create_lc = false
# Add instances to load balancer
load_balancers = ["${module.this_elb_id}"]
root_block_device = [
{
# 200 GB root partition, general purpose SSD
volume_size = "200"
volume_type = "gp2"
},
]
# At least 200 GB for production because of I/O speed
ebs_block_device = [
{
device_name = "/dev/xvdz"
volume_type = "gp2"
volume_size = "${terraform.workspace == "production" ? 200 : 50}"
delete_on_termination = true
},
]
# Autoscaling group
asg_name = "MNIST-model-asg"
vpc_zone_identifier = ["${module.vpc.private_subnets}"]
min_size = 1
desired_capacity = 1
max_size = 3
wait_for_capacity_timeout = 0
tags = [
{
key = "Environment"
value = "${terraform.workspace}"
propagate_at_launch = true
},
{
key = "Project"
value = "megasecret"
propagate_at_launch = true
},
]
}