Skip to content

Instantly share code, notes, and snippets.

@ben-x
Last active August 25, 2024 19:38
Show Gist options
  • Select an option

  • Save ben-x/0999d8c126ec9f66d7b55f9887d03f2f to your computer and use it in GitHub Desktop.

Select an option

Save ben-x/0999d8c126ec9f66d7b55f9887d03f2f to your computer and use it in GitHub Desktop.
Fleet of ec2 instance with lifecycle hooks. A part of multi-region-with-single-workload project
module "app_fleet" {
source = "../../../common-modules/aws-ec2-fleet"
name = "app-fleet"
tags = var.tags
ingress_rules = {
allow_app_port = {
cidr_blocks = [aws_vpc.main.cidr_block]
source_port = var.app_port
destination_port = var.app_port
protocol = "tcp"
}
}
scaling_config = {
max_size = 5
min_size = 0
}
user_data = base64encode(
templatefile("${path.module}/files/app-fleet-user-data.sh", {
APP_PORT = var.app_port
})
)
vpc = {
id = aws_vpc.main.id
subnet_ids = local.private_subnet_ids
}
}
resource "aws_autoscaling_lifecycle_hook" "instance_launch_lifecycle_hook" {
name = "launch-hook"
autoscaling_group_name = module.app_fleet.autoscaling_group.name
default_result = "CONTINUE"
heartbeat_timeout = 60 * 10 # 10 minutes
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
}
resource "aws_autoscaling_lifecycle_hook" "instance_termination_lifecycle_hook" {
name = "termination-hook"
autoscaling_group_name = module.app_fleet.autoscaling_group.name
default_result = "CONTINUE"
heartbeat_timeout = 60 * 10 # 10 minute
lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment