-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoscaling.tf
More file actions
87 lines (72 loc) · 2.52 KB
/
autoscaling.tf
File metadata and controls
87 lines (72 loc) · 2.52 KB
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
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/asg-capacity-providers-create-auto-scaling-group.html#using-warm-pool
resource "aws_autoscaling_group" "this" {
name = local.resource_name
vpc_zone_identifier = local.private_subnet_ids
protect_from_scale_in = true
// NOTE: The auto-scaling is managed by the ecs capacity provider
// max_size puts a hard cap on the ecs capacity provider
min_size = var.min_node_count
max_size = var.max_node_count
health_check_grace_period = var.warmup_period
launch_template {
id = aws_launch_template.this.id
version = "$Latest"
}
dynamic "tag" {
for_each = merge(local.tags, { "AmazonECSManaged" = "true" })
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-linux.html
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html
locals {
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/bootstrap_container_instance.html
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html
user_data = <<EOF
#!/bin/bash
# Format and mount extra disk for Docker usage
mkfs -t xfs /dev/xvdcz
mkdir -p /var/lib/docker
echo '/dev/xvdcz /var/lib/docker xfs defaults,noatime 0 0' >> /etc/fstab
mount -a
# Configure ECS agent
echo ECS_CLUSTER=${aws_ecs_cluster.this.name} >> /etc/ecs/ecs.config
EOF
}
resource "aws_launch_template" "this" {
name_prefix = local.block_name
image_id = local.ami
instance_type = var.node_instance_type
vpc_security_group_ids = [aws_security_group.this.id]
user_data = base64encode(local.user_data)
tags = local.tags
tag_specifications {
resource_type = "instance"
tags = merge(local.tags, { "Name" = "${local.block_name}/node" })
}
iam_instance_profile {
name = aws_iam_instance_profile.this.name
}
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_type = "gp3"
volume_size = var.node_volume_size
}
}
// Additional storage volume dedicated to Docker
block_device_mappings {
device_name = "/dev/xvdcz"
ebs {
volume_type = "gp3"
volume_size = var.docker_volume_size
}
}
lifecycle {
create_before_destroy = true
}
}