-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autoscaling_mnist_api.tf
112 lines (95 loc) · 2.97 KB
/
Autoscaling_mnist_api.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
# Create access to our web cluster
resource "aws_security_group" "mnist_allow_http" {
name = "mnist_allow_http"
description = "Allow inbound traffic"
vpc_id = "${module.vpc.vpc_id}"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_groups = ["${aws_security_group.mnist_model.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
##################################################################
# MNIST API Autoscaling group with external launch configuration #
##################################################################
resource "aws_launch_configuration" "api-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}", "${aws_security_group.mnist_allow_http.id}"]
lifecycle {
create_before_destroy = true
}
connection {
user = "ubuntu"
type = "ssh"
private_key = "${file(var.pvt_key)}"
}
# Copy the entire frontend folder into the home directory
# of the user that is logged in: home/ubuntu/ in this case
provisioner "file" {
source = "~/api_execution_scripts"
destination = "~/"
}
provisioner "remote-exec" {
inline = [
"chmod +x ~/api_execution_scripts/setup_docker_api.sh",
"sudo ~/api_execution_scripts/setup_docker_api.sh || ~/api_execution_scripts/setup_docker_api.sh",
]
}
}
# Use the templating module for autoscaling
module "api-asg" {
source = "terraform-aws-modules/autoscaling/aws"
name = "MNIST-api-autoscaling"
# Launch configuration
launch_configuration = "${aws_launch_configuration.model-lc.name}"
create_lc = false
# when new instances are created it's going to get added to that load balancer
load_balancers = ["${module.elb.this_elb_id}"]
# 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
},
]
root_block_device = [
{
# 200 GB root partition, general purpose SSD
volume_size = "200"
volume_type = "gp2"
},
]
# Auto scaling group
asg_name = "MNIST-api-asg"
# Pull the list of public subnets from the VPC module
vpc_zone_identifier = ["${module.vpc.public_subnets}"]
health_check_type = "EC2"
min_size = 1
max_size = 5
desired_capacity = 2
wait_for_capacity_timeout = 0
tags = [
{
key = "Environment"
value = "${terraform.workspace}"
propagate_at_launch = true
},
{
key = "Project"
value = "megasecret"
propagate_at_launch = true
},
]
}