-
Notifications
You must be signed in to change notification settings - Fork 4
/
swarm-node.tf
141 lines (112 loc) · 3.99 KB
/
swarm-node.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
data "template_file" "swarm-node" {
template = "${file("${path.module}/user_data/swarm-node.sh.tpl")}"
vars {
BASE_SIZE = "${var.docker_volume_size}"
EXTERNAL_LB = "${var.elb_id}"
AWS_REGION = "${var.region}"
ACCOUNT_ID = "${var.aws_account}"
VPC_ID = "${var.vpc_id}"
MANAGER_SECURITY_GROUP_ID = "${aws_security_group.swarm-manager.id}"
WORKER_SECURITY_GROUP_ID = "${aws_security_group.swarm-node.id}"
DYNAMODB_TABLE = "${aws_dynamodb_table.dyndb.id}"
SWARM_QUEUE = "${aws_sqs_queue.swarm-sqs.id}"
CLEANUP_QUEUE = "${aws_sqs_queue.swarm-cleanup.id}"
RUN_VACUUM = "yes"
ENABLE_CLOUDWATCH_LOGS = "no"
LOG_GROUP_NAME = "${var.name}-tf-${var.environment}-lg"
}
}
resource "aws_launch_configuration" "swarm-node" {
name_prefix = "${var.name}-${var.environment}-swarm-node-"
image_id = "${lookup(var.image_id, var.region)}"
instance_type = "${var.instance_type}"
ebs_optimized = "${var.instance_ebs_optimized}"
iam_instance_profile = "${aws_iam_instance_profile.ProxyInstanceProfile.name}"
key_name = "${var.ssh_key_name}"
security_groups = [
"${aws_security_group.swarm-node.id}",
]
user_data = "${data.template_file.swarm-node.rendered}"
associate_public_ip_address = "${var.associate_public_ip_address}"
# root
root_block_device {
volume_type = "gp2"
volume_size = "${var.root_volume_size}"
}
# docker
ebs_block_device {
device_name = "/dev/xvdcz"
volume_type = "gp2"
volume_size = "${var.docker_volume_size}"
}
lifecycle {
create_before_destroy = true
}
depends_on = [
"aws_autoscaling_group.swarm-manager",
]
}
resource "aws_autoscaling_group" "swarm-node" {
name = "${var.name}-tf-${var.environment}-swarm-node"
launch_configuration = "${aws_launch_configuration.swarm-node.name}"
vpc_zone_identifier = ["${var.external_subnets}"]
min_size = "${var.autoscale_min}"
max_size = "${var.autoscale_max}"
desired_capacity = "${var.autoscale_desired}"
termination_policies = ["OldestLaunchConfiguration", "Default"]
load_balancers = ["${var.elb_id}"]
health_check_type = "ELB"
health_check_grace_period = 300
tag {
key = "Name"
value = "${var.name}-tf-${var.environment}-swarm-node"
propagate_at_launch = true
}
tag {
key = "Cluster"
value = "${var.name}-tf-${var.environment}-cluster"
propagate_at_launch = true
}
tag {
key = "Environment"
value = "${var.environment}"
propagate_at_launch = true
}
tag {
key = "swarm-node-type"
value = "worker"
propagate_at_launch = true
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_policy" "swarm-node-scale-up" {
name = "${var.name}-tf-${var.environment}-ecs-up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.swarm-node.name}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_policy" "swarm-node-scale-down" {
name = "${var.name}-tf-${var.environment}-ecs-down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.swarm-node.name}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_lifecycle_hook" "swarm-node-upgrade" {
name = "${var.name}-tf-${var.environment}-swarm-node-upgrade-hook"
autoscaling_group_name = "${aws_autoscaling_group.swarm-node.name}"
default_result = "CONTINUE"
heartbeat_timeout = 2000
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
notification_target_arn = "${aws_sqs_queue.swarm-sqs.arn}"
role_arn = "${aws_iam_role.ProxyRole.arn}"
}