This repository has been archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
asg.tf
84 lines (70 loc) · 2.01 KB
/
asg.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
data "aws_ssm_parameter" "ecs_optimized_ami" {
# Get latest ecs optimized ami
# https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/ecs-optimized_AMI.html
name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
}
resource "aws_autoscaling_group" "tmp-asg" {
name = "tmp-asg"
max_size = 10
min_size = 0
vpc_zone_identifier = [aws_subnet.tmp-a.id, aws_subnet.tmp-c.id]
mixed_instances_policy {
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.tmp-template.id
version = "$Latest" # https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#version
}
override {
instance_type = "t3.large"
weighted_capacity = "1"
}
override {
instance_type = "t3.xlarge"
weighted_capacity = "2"
}
}
instances_distribution {
# Use spot instance only.
on_demand_percentage_above_base_capacity = 0
}
}
lifecycle {
ignore_changes = [desired_capacity, tag]
}
# Interval of scale in/out
default_cooldown = 60
# If you enable managedTerminationProtection on capacity provider, you have to enable this.
# protect_from_scale_in = true
}
resource "aws_launch_template" "tmp-template" {
name = "tmp-template"
image_id = data.aws_ssm_parameter.ecs_optimized_ami.value
vpc_security_group_ids = [aws_security_group.tmp.id]
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = 60
volume_type = "gp2"
}
}
ebs_optimized = true
user_data = base64encode(templatefile("./userdata.sh",
{
CLUSTER_NAME = var.ecs_cluster_name
}))
tag_specifications {
resource_type = "instance"
tags = {
Name = "tmp-ecs-instance"
}
}
tag_specifications {
resource_type = "volume"
tags = {
Name = "tmp-ecs-instance"
}
}
iam_instance_profile {
arn = aws_iam_instance_profile.ecsInstanceRole_tmp.arn
}
}