Skip to content

michaelaw320/terraform-aws-autoscaling

 
 

Repository files navigation

AWS Auto Scaling Group (ASG) Terraform module

Terraform module which creates Auto Scaling resources on AWS.

These types of resources are supported:

Terraform versions

Terraform 0.12. Pin module version to ~> v3.0. Submit pull-requests to master branch.

Terraform 0.11. Pin module version to ~> v2.0. Submit pull-requests to terraform011 branch.

Usage

module "asg" {
  source  = "terraform-aws-modules/autoscaling/aws"
  version = "~> 3.0"
  
  name = "service"

  # Launch template
  lt_name = "example-lt"

  image_id        = "ami-ebd02392"
  instance_type   = "t2.micro"
  security_groups = ["sg-12345678"]

  ebs_block_device = [
    {
      device_name           = "/dev/xvdz"
      volume_type           = "gp2"
      volume_size           = "50"
      delete_on_termination = true
    },
  ]

  root_block_device = [
    {
      volume_size = "50"
      volume_type = "gp2"
    },
  ]

  # Auto scaling group
  asg_name                  = "example-asg"
  vpc_zone_identifier       = ["subnet-1235678", "subnet-87654321"]
  health_check_type         = "EC2"
  min_size                  = 0
  max_size                  = 1
  desired_capacity          = 1
  wait_for_capacity_timeout = 0

  tags = [
    {
      key                 = "Environment"
      value               = "dev"
      propagate_at_launch = true
    },
    {
      key                 = "Project"
      value               = "megasecret"
      propagate_at_launch = true
    },
  ]

  tags_as_map = {
    extra_tag1 = "extra_value1"
    extra_tag2 = "extra_value2"
  }
}

Conditional creation

Normally this module creates both Auto Scaling Group (ASG) and Launch Template (LT), and connect them together. It is possible to customize this behaviour passing different parameters to this module:

  1. To create ASG, but not LT. Associate ASG with an existing LT:
create_lt = false
launch_template = "existing-launch-template"
  1. To create LT, but not ASG. Outputs may produce errors.
create_asg = false
  1. To create ASG with initial lifecycle hook
create_asg_with_initial_lifecycle_hook = true

initial_lifecycle_hook_name                  = "NameOfLifeCycleHook"
initial_lifecycle_hook_lifecycle_transition  = "autoscaling:EC2_INSTANCE_TERMINATING"
initial_lifecycle_hook_notification_metadata =<<EOF
{
  "foo": "bar"
}
EOF
  1. To disable creation of both resources (LT and ASG) you can specify both arguments create_lt = false and create_asg = false. Sometimes you need to use this way to create resources in modules conditionally but Terraform does not allow to use count inside module block.

Tags

There are two ways to specify tags for auto-scaling group in this module - tags and tags_as_map. See examples/asg_ec2/main.tf for example.

Examples

Inputs

Name Description Type Default Required
asg_name Creates a unique name for autoscaling group beginning with the specified prefix string "" no
associate_public_ip_address Associate a public ip address with an instance in a VPC bool "false" no
block_device_mappings Mappings of block devices, see https://www.terraform.io/docs/providers/aws/r/launch_template.html#block-devices list(any) [ {} ] no
create_asg Whether to create autoscaling group bool "true" no
create_asg_with_initial_lifecycle_hook Create an ASG with initial lifecycle hook bool "false" no
create_lt Whether to create launch template bool "true" no
default_cooldown The amount of time, in seconds, after a scaling activity completes before another scaling activity can start number "300" no
desired_capacity The number of Amazon EC2 instances that should be running in the group string n/a yes
ebs_optimized If true, the launched EC2 instance will be EBS-optimized bool "false" no
enable_monitoring Enables/disables detailed monitoring. This is enabled by default. bool "true" no
enabled_metrics A list of metrics to collect. The allowed values are GroupMinSize, GroupMaxSize, GroupDesiredCapacity, GroupInServiceInstances, GroupPendingInstances, GroupStandbyInstances, GroupTerminatingInstances, GroupTotalInstances list(string) [ "GroupMinSize", "GroupMaxSize", "GroupDesiredCapacity", "GroupInServiceInstances", "GroupPendingInstances", "GroupStandbyInstances", "GroupTerminatingInstances", "GroupTotalInstances" ] no
force_delete Allows deleting the autoscaling group without waiting for all instances in the pool to terminate. You can force an autoscaling group to delete even if it's in the process of scaling a resource. Normally, Terraform drains all the instances before deleting the group. This bypasses that behavior and potentially leaves resources dangling bool "false" no
health_check_grace_period Time (in seconds) after instance comes into service before checking health number "300" no
health_check_type Controls how health checking is done. Values are - EC2 and ELB string n/a yes
iam_instance_profile The IAM instance profile to associate with launched instances string "" no
image_id The EC2 image ID to launch string "" no
initial_lifecycle_hook_default_result Defines the action the Auto Scaling group should take when the lifecycle hook timeout elapses or if an unexpected failure occurs. The value for this parameter can be either CONTINUE or ABANDON string "ABANDON" no
initial_lifecycle_hook_heartbeat_timeout Defines the amount of time, in seconds, that can elapse before the lifecycle hook times out. When the lifecycle hook times out, Auto Scaling performs the action defined in the DefaultResult parameter string "60" no
initial_lifecycle_hook_lifecycle_transition The instance state to which you want to attach the initial lifecycle hook string "" no
initial_lifecycle_hook_name The name of initial lifecycle hook string "" no
initial_lifecycle_hook_notification_metadata Contains additional information that you want to include any time Auto Scaling sends a message to the notification target string "" no
initial_lifecycle_hook_notification_target_arn The ARN of the notification target that Auto Scaling will use to notify you when an instance is in the transition state for the lifecycle hook. This ARN target can be either an SQS queue or an SNS topic string "" no
initial_lifecycle_hook_role_arn The ARN of the IAM role that allows the Auto Scaling group to publish to the specified notification target string "" no
instance_type The size of instance to launch string "" no
key_name The key name that should be used for the instance string "" no
launch_template The name of the launch template to use (if it is created outside of this module) string "" no
load_balancers A list of elastic load balancer names to add to the autoscaling group names list(string) [] no
lt_name Creates a unique name for launch template beginning with the specified prefix string "" no
max_size The maximum size of the auto scale group string n/a yes
metrics_granularity The granularity to associate with the metrics to collect. The only valid value is 1Minute string "1Minute" no
min_elb_capacity Setting this causes Terraform to wait for this number of instances to show up healthy in the ELB only on creation. Updates will not wait on ELB instance number changes number "0" no
min_size The minimum size of the auto scale group string n/a yes
name Creates a unique name beginning with the specified prefix string n/a yes
placement_group The name of the placement group into which you'll launch your instances, if any string "" no
placement_tenancy The tenancy of the instance. Valid values are 'default' or 'dedicated' string "default" no
protect_from_scale_in Allows setting instance protection. The autoscaling group will not select instances with this setting for termination during scale in events. bool "false" no
recreate_asg_when_lt_changes Whether to recreate an autoscaling group when launch template changes bool "false" no
security_groups A list of security group IDs to assign to the launch template list(string) [] no
service_linked_role_arn The ARN of the service-linked role that the ASG will use to call other AWS services. string "" no
spot_price The price to use for reserving spot instances string "" no
suspended_processes A list of processes to suspend for the AutoScaling Group. The allowed values are Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance, AlarmNotification, ScheduledActions, AddToLoadBalancer. Note that if you suspend either the Launch or Terminate process types, it can prevent your autoscaling group from functioning properly. list(string) [] no
tags A list of tag blocks. Each element should have keys named key, value, and propagate_at_launch. list(map(string)) [] no
tags_as_map A map of tags and values in the same format as other resources accept. This will be converted into the non-standard format that the aws_autoscaling_group requires. map(string) {} no
target_group_arns A list of aws_alb_target_group ARNs, for use with Application Load Balancing list(string) [] no
termination_policies A list of policies to decide how the instances in the auto scale group should be terminated. The allowed values are OldestInstance, NewestInstance, OldestLaunchTemplate, ClosestToNextInstanceHour, Default list(string) [ "Default" ] no
user_data The user data to provide when launching the instance string " " no
vpc_zone_identifier A list of subnet IDs to launch resources in list(string) n/a yes
wait_for_capacity_timeout A maximum duration that Terraform should wait for ASG instances to be healthy before timing out. (See also Waiting for Capacity below.) Setting this to '0' causes Terraform to skip all Capacity Waiting behavior. string "10m" no
wait_for_elb_capacity Setting this will cause Terraform to wait for exactly this number of healthy instances in all attached load balancers on both create and update operations. Takes precedence over min_elb_capacity behavior. number "null" no

Outputs

Name Description
this_autoscaling_group_arn The ARN for this AutoScaling Group
this_autoscaling_group_availability_zones The availability zones of the autoscale group
this_autoscaling_group_default_cooldown Time between a scaling activity and the succeeding scaling activity
this_autoscaling_group_desired_capacity The number of Amazon EC2 instances that should be running in the group
this_autoscaling_group_health_check_grace_period Time after instance comes into service before checking health
this_autoscaling_group_health_check_type EC2 or ELB. Controls how health checking is done
this_autoscaling_group_id The autoscaling group id
this_autoscaling_group_load_balancers The load balancer names associated with the autoscaling group
this_autoscaling_group_max_size The maximum size of the autoscale group
this_autoscaling_group_min_size The minimum size of the autoscale group
this_autoscaling_group_name The autoscaling group name
this_autoscaling_group_target_group_arns List of Target Group ARNs that apply to this AutoScaling Group
this_autoscaling_group_vpc_zone_identifier The VPC zone identifier
this_launch_template_id The ID of the launch template
this_launch_template_name The name of the launch template

Authors

Module managed by Anton Babenko.

Thanks to Jamie-BitFlight who added possibility to specify unlimited numbers of tags.

License

Apache 2 Licensed. See LICENSE for full details.

About

Terraform module which creates Auto Scaling resources on AWS

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HCL 99.4%
  • Makefile 0.6%