forked from hazelops/terraform-aws-ecs-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscaling.tf
94 lines (80 loc) · 2.82 KB
/
autoscaling.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
resource "aws_eip" "autoscaling" {
# If ec2_eip_count is set, use that number for number of EIPs, otherwise use var.max_size + 1 (but that might not be the best during downscaling and deletion of EIPs
count = var.ec2_eip_enabled ? (var.ec2_eip_count > 0 ? var.ec2_eip_count : var.max_size + 1) : 0
public_ipv4_pool = "amazon"
vpc = true
tags = {
Name = "${local.name}-${count.index + 1}"
env = var.env
service = local.name
}
}
module "autoscaling" {
source = "terraform-aws-modules/autoscaling/aws"
version = "~> 4.0"
create_asg = var.ecs_launch_type == "EC2" ? true : false
create_lt = var.ecs_launch_type == "EC2" ? true : false
name = local.name
lt_name = local.name
use_lt = true
# Auto scaling group
#v4.0? asg_name = local.name
image_id = var.image_id
instance_type = var.instance_type
security_groups = var.security_groups
iam_instance_profile_name = var.iam_instance_profile # 4.0? var.iam_instance_profile - it's "ID of the IAM instance profile"
key_name = var.key_name
#4.0? recreate_asg_when_lc_changes = true
root_block_device = [
{
volume_size = var.root_block_device_size
volume_type = var.root_block_device_type
},
]
target_group_arns = var.app_type == "web" || var.app_type == "tcp-app" ? module.alb[0].target_group_arns : []
user_data_base64 = var.ecs_launch_type == "EC2" ? base64encode(data.template_file.asg_ecs_ec2_user_data.rendered) : null
vpc_zone_identifier = var.public_ecs_service ? var.public_subnets : var.private_subnets
health_check_type = var.autoscaling_health_check_type
min_size = var.min_size
max_size = var.max_size
desired_capacity = var.desired_capacity
wait_for_capacity_timeout = 0
create_schedule = var.create_schedule
schedules = var.schedules
tags = [
{
key = "env"
value = var.env
propagate_at_launch = true
},
{
key = "cluster"
value = local.ecs_cluster_name
propagate_at_launch = true
},
{
key = "service-groups"
value = var.ec2_service_group
propagate_at_launch = true
},
]
}
# IAM Role changes for ASG Auto EIP
resource "aws_iam_role_policy" "ec2_auto_eip" {
count = var.ec2_eip_enabled && var.ecs_launch_type == "EC2" ? 1 : 0
name = "EC2ChangeEIP_Policy"
role = data.aws_iam_instance_profile.this[0].role_name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ec2:Describe*",
"ec2:AssociateAddress"
]
Effect = "Allow"
Resource = "*"
},
]
})
}