This template sets up an AWS Autoscaling Group and attaches a Lifecycle Hook to it. The hook triggers when an EC2 instance is launching.

Terraform Template

resource "aws_autoscaling_group" "foobar" {
  availability_zones = ["us-west-2a"]
  health_check_type = "EC2"
  name = "terraform-test-foobar5"

  tag {
    key = "Foo"
    propagate_at_launch = true
    value = "foo-bar"
  }
  termination_policies = ["OldestInstance"]
}

resource "aws_autoscaling_lifecycle_hook" "foobar" {
  autoscaling_group_name = aws_autoscaling_group.foobar.name
  default_result = "CONTINUE"
  heartbeat_timeout = 2000
  lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
  name = "foobar"
  notification_metadata = jsonencode({
 foo = "bar"
 })
  notification_target_arn = "arn:aws:sqs:us-east-1:444455556666:queue1*"
  role_arn = "arn:aws:iam::123456789012:role/S3Access"
}