-
Notifications
You must be signed in to change notification settings - Fork 12
/
rabbit-node.tf
executable file
·122 lines (101 loc) · 3.62 KB
/
rabbit-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
# Template use at launch to install docker
# It will also lauch each docker container that are used to manage the state of our cluster
# This is use to pass required settings from terraform template directly in the ED2 instance
data "template_file" "rabbit-node" {
template = file("${path.module}/user_data/rabbitmq.sh")
vars = {
AWS_REGION = var.region
VPC_ID = var.vpc_id
ERL_SECRET_COOKIE = var.erl_secret_cookie
AWS_ACCESS_KEY = var.aws_access_key
AWS_SECRET_KEY = var.aws_secret_key
RABBITMQ_VERSION = var.rabbitmq_version
ERLANG_VERSION = var.erlang_version
CLUSTER_NAME = "${var.cluster_fqdn}-${var.name}-${var.environment}"
}
}
resource "aws_launch_configuration" "rabbit-node" {
name_prefix = "${var.name}-${var.environment}-rabbit-"
image_id = var.image_id
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.rabbit-cluster.id,
aws_security_group.rabbit-node.id,
]
# User Data is what's run at start from the template file previously rendered
user_data = data.template_file.rabbit-node.rendered
associate_public_ip_address = var.associate_public_ip_address
# root
root_block_device {
volume_type = "gp2"
volume_size = var.root_volume_size
}
# rabbit
ebs_block_device {
device_name = "/dev/xvdcz"
volume_type = "gp2"
volume_size = var.rabbit_volume_size
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "rabbit-node" {
name = "${var.name}-${var.environment}-rabbit"
launch_configuration = aws_launch_configuration.rabbit-node.name
vpc_zone_identifier = var.external_subnets
min_size = var.autoscaling_min_size
max_size = var.autoscaling_max_size
desired_capacity = var.desired_capacity
termination_policies = ["OldestLaunchConfiguration", "Default"]
health_check_type = "EC2"
health_check_grace_period = 300
tag {
key = "Name"
value = "${var.name}-${var.environment}-rabbit"
propagate_at_launch = true
}
tag {
key = "Cluster"
value = "${var.name}-${var.environment}-cluster"
propagate_at_launch = true
}
tag {
key = "Environment"
value = var.environment
propagate_at_launch = true
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_policy" "rabbit-node-scale-up" {
name = "${var.name}-${var.environment}-rabbit-node-up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.rabbit-node.name
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_policy" "rabbit-node-scale-down" {
name = "${var.name}-${var.environment}-rabbit-node-down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.rabbit-node.name
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_lifecycle_hook" "rabbit-node-upgrade" {
name = "${var.name}-${var.environment}-rabbit-node-upgrade-hook"
autoscaling_group_name = aws_autoscaling_group.rabbit-node.name
default_result = "CONTINUE"
heartbeat_timeout = 2000
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
}