-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasg.tf
More file actions
65 lines (58 loc) · 2.21 KB
/
asg.tf
File metadata and controls
65 lines (58 loc) · 2.21 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
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-optimized_AMI.html#ecs-optimized-ami-linux
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/retrieve-ecs-optimized_AMI.html
# https://github.com/terraform-aws-modules/terraform-aws-autoscaling/blob/master/examples/complete/main.tf
data "aws_ssm_parameter" "ecs_optimized_ami" {
name = "/aws/service/ecs/optimized-ami/amazon-linux-2023/arm64/recommended"
}
module "autoscaling" {
source = "terraform-aws-modules/autoscaling/aws"
name = "${local.name}-asg"
launch_template_name = "${local.name}-ecs-ec2-asg-template"
image_id = jsondecode(data.aws_ssm_parameter.ecs_optimized_ami.value)["image_id"]
instance_type = var.ec2_instance_type
update_default_version = true
# block_device_mappings = [
# {
# # Root volume
# device_name = "/dev/xvda"
# no_device = 0
# ebs = {
# delete_on_termination = true
# encrypted = true
# volume_size = 10
# volume_type = "gp3"
# }
# }
# ]
security_groups = [module.autoscaling_sg.security_group_id]
user_data = base64encode(
<<-EOT
#!/bin/bash
cat <<'EOF' >> /etc/ecs/ecs.config
ECS_CLUSTER=${local.name}
ECS_LOGLEVEL=debug
ECS_CONTAINER_INSTANCE_TAGS=${jsonencode(local.tags)}
ECS_ENABLE_TASK_IAM_ROLE=true
EOF
EOT
)
create_iam_instance_profile = true
iam_role_name = local.name
iam_role_description = "ECS role for ${local.name}"
iam_role_policies = {
AmazonEC2ContainerServiceforEC2Role = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
ignore_desired_capacity_changes = true
vpc_zone_identifier = module.vpc.private_subnets
health_check_type = "EC2"
min_size = 1
max_size = var.as_max_size
desired_capacity = var.as_desired_capacity
autoscaling_group_tags = {
AmazonECSManaged = true
}
use_mixed_instances_policy = false
mixed_instances_policy = {}
tags = local.tags
}