Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Latest commit

 

History

History
311 lines (254 loc) · 25.5 KB

File metadata and controls

311 lines (254 loc) · 25.5 KB

AWS Auto Scaling Group (ASG) Terraform module

Terraform module which creates Auto Scaling resources on AWS.

These types of resources are supported:

This module will require more than 1 instance type. Suitable to back ECS Cluster.

Terraform versions

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

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

Versioning

This module uses Semver.

x.y.z

x shall change when there's major language or breaking feature change (e.g. 0.11 to 0.12 which drastically change the language)

y shall change when there's feature addition which is not breaking existing API (e.g. addition of some parameters with default value)

z shall change when there's documentation updates, minor fixes, etc.

Version 3.0.0

Version 3.0.0 introduces a breaking change, which is in the way the module internally handles initial lifecycle hook flag. Auto Scaling group with initial lifecycle hook and without initial lifecycle hook are defined as different resources on version 2.0.0 and below. Therefore, the resource identifier will be different between ASG with and without initial lifecycle hook. In version 3.0.0 dynamic block is used and therefore the resource identifier are the same.

Other than that, version 3.0.0 allows you to set whether to ignore desired capacity changes or not. This is useful, for example, when using target-tracking autoscaling on your ASG. If desired capacity is not ignored, it will be reset whenever you do terraform apply. Refer to usage and input on how to set the flag.

Amazon Documentations

Usage

module "asg" {
  source = "HENNGE/autoscaling-mixed-instances/aws"
  version = "3.0.0"

  name = "service"

  # Launch template
  lt_name = "example-lt"

  image_id        = "ami-ebd02392"
  security_groups = ["sg-12345678"]

  block_device_mappings = [
    {
      # Root block device
      device_name = "/dev/xvda"

      ebs = [
        {
          volume_type = "gp2"
          volume_size = 50
        },
      ]
    },
    {
      # EBS Block Device
      device_name = "/dev/xvdz"

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

  # 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

  ignore_desired_capacity_changes = true

  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"
  }

  instance_types = [
    {
      instance_type = "t2.micro",
      weighted_capacity = 1,
    },
    {
      instance_type = "t3.micro",
      weighted_capacity = 1,
    },
    {
      instance_type = "t2.small",
      weighted_capacity = 2,
    },
    {
      instance_type = "t3.small",
      weighted_capacity = 2,
    },
    {
      instance_type = "t2.medium",
      weighted_capacity = 4,
    },
  ]

  on_demand_base_capacity                  = 0
  on_demand_percentage_above_base_capacity = 100
}

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

Requirements

Name Version
terraform >= 0.12.26
aws >= 3.30
null >= 2.0
random >= 3.0

Providers

Name Version
aws >= 3.30
null >= 2.0
random >= 3.0

Modules

No modules.

Resources

Name Type
aws_autoscaling_group.this resource
aws_autoscaling_group.this_ignore_desired_capacity_changes resource
aws_launch_template.this resource
null_resource.tags_as_list_of_maps resource
random_pet.asg_name resource

Inputs

Name Description Type Default Required
asg_instance_refresh_additional_triggers Set of additional property names that will trigger an Instance Refresh. A refresh will always be triggered by a change in any of launch_configuration, launch_template, or mixed_instances_policy. list(string) null no
asg_instance_refresh_healthy_percentage The amount of capacity in the Auto Scaling group that must remain healthy during an instance refresh to allow the operation to continue, as a percentage of the desired capacity of the Auto Scaling group. number null no
asg_instance_refresh_strategy The strategy to use for instance refresh. The only allowed value is Rolling. string null no
asg_instance_refresh_warmup The number of seconds until a newly launched instance is configured and ready to use. number null no
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
capacity_rebalance Indicates whether capacity rebalance is enabled. bool null 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 number 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
ignore_desired_capacity_changes Ignores any changes to desired_capacity parameter after apply. Note updating this value will destroy the existing service and recreate it. bool false 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_types Instance types to launch, minimum 2 types must be specified. List of Map of 'instance_type'(required) and 'weighted_capacity'(optional). list(map(any))
[
{}
]
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
lt_version Launch Template version to use (if it is created outside of this module). string null no
max_instance_lifetime The maximum amount of time, in seconds, that an instance can be in service, values must be either equal to 0 or between 604800 and 31536000 seconds number null no
max_size The maximum size of the auto scale group number 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 number n/a yes
name Creates a unique name beginning with the specified prefix string n/a yes
on_demand_base_capacity Absolute minimum amount of desired capacity that must be fulfilled by on-demand instances number 0 no
on_demand_percentage_above_base_capacity Percentage split between on-demand and Spot instances above the base on-demand capacity. number 100 no
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_allocation_strategy How to allocate capacity across the Spot pools. Valid values: 'lowest-price', 'capacity-optimized'. string "capacity-optimized" no
spot_instance_pools Number of Spot pools per availability zone to allocate capacity. EC2 Auto Scaling selects the cheapest Spot pools and evenly allocates Spot capacity across the number of Spot pools that you specify. Diversifies your Spot capacity across multiple instance types to find the best pricing. number 2 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(any)) [] 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(any) {} 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, OldestLaunchConfiguration, ClosestToNextInstanceHour, Default list(string)
[
"Default"
]
no
use_created_lt_latest_version Use version number generated by Launch Template instead of $Latest. bool false 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 Michael Wangsa.

Thanks to Jamie-BitFlight who added possibility to specify unlimited numbers of tags. Thanks to Anton Babenko for maintaining the upstream terraform-aws-autoscaling module.

License

Apache 2 Licensed. See LICENSE for full details.