ec2_asg - Create or delete AWS Autoscaling Groups

New in version 1.6.

Synopsis

  • Can create or delete AWS Autoscaling Groups
  • Works with the ec2_lc module to manage Launch Configurations

Requirements (on host that executes module)

  • python >= 2.6
  • boto

Options

parameter required default choices comments
availability_zones
no
List of availability zone names in which to create the group. Defaults to all the availability zones in the region if vpc_zone_identifier is not set.
aws_access_key
no
AWS access key. If not set then the value of the AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY or EC2_ACCESS_KEY environment variable is used.
aliases: ec2_access_key, access_key
aws_secret_key
no
AWS secret key. If not set then the value of the AWS_SECRET_ACCESS_KEY, AWS_SECRET_KEY, or EC2_SECRET_KEY environment variable is used.
aliases: ec2_secret_key, secret_key
default_cooldown
(added in 2.0)
no 300 seconds
The number of seconds after a scaling activity completes before another can begin.
desired_capacity
no
Desired number of instances in group, if unspecified then the current group value will be used.
ec2_url
no
Url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints). Ignored for modules where region is required. Must be specified for all other modules if region is not used. If not set then the value of the EC2_URL environment variable, if any, is used.
health_check_period
(added in 1.7)
no 500 seconds
Length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health.
health_check_type
(added in 1.7)
no EC2
  • EC2
  • ELB
The service you want the health status from, Amazon EC2 or Elastic Load Balancer.
launch_config_name
yes
Name of the Launch configuration to use for the group. See the ec2_lc module for managing these. If unspecified then the current group value will be used.
lc_check
(added in 1.8)
no True
Check to make sure instances that are being replaced with replace_instances do not already have the current launch_config.
load_balancers
no
List of ELB names to use for the group
max_size
no
Maximum number of instances in group, if unspecified then the current group value will be used.
min_size
no
Minimum number of instances in group, if unspecified then the current group value will be used.
name
yes
Unique name for group to be created or deleted
notification_topic
(added in 2.2)
no None
A SNS topic ARN to send auto scaling notifications to.
notification_types
(added in 2.2)
no [u'autoscaling:EC2_INSTANCE_LAUNCH', u'autoscaling:EC2_INSTANCE_LAUNCH_ERROR', u'autoscaling:EC2_INSTANCE_TERMINATE', u'autoscaling:EC2_INSTANCE_TERMINATE_ERROR']
A list of auto scaling events to trigger notifications on.
placement_group
(added in 2.3)
no None
Physical location of your cluster placement group created in Amazon EC2.
profile
(added in 1.6)
no
Uses a boto profile. Only works with boto >= 2.24.0.
region
no
The AWS region to use. If not specified then the value of the AWS_REGION or EC2_REGION environment variable, if any, is used. See http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region
aliases: aws_region, ec2_region
replace_all_instances
(added in 1.8)
no
In a rolling fashion, replace all instances with an old launch configuration with one from the current launch configuration.
replace_batch_size
(added in 1.8)
no 1
Number of instances you'd like to replace at a time. Used with replace_all_instances.
replace_instances
(added in 1.8)
no None
List of instance_ids belonging to the named ASG that you would like to terminate and be replaced with instances matching the current launch configuration.
security_token
(added in 1.6)
no
AWS STS security token. If not set then the value of the AWS_SECURITY_TOKEN or EC2_SECURITY_TOKEN environment variable is used.
aliases: access_token
state
no present
  • present
  • absent
register or deregister the instance
suspend_processes
(added in 2.3)
no
  • Launch
  • Terminate
  • HealthCheck
  • ReplaceUnhealthy
  • AZRebalance
  • AlarmNotification
  • ScheduledActions
  • AddToLoadBalancer
A list of scaling processes to suspend.
tags
(added in 1.7)
no None
A list of tags to add to the Auto Scale Group. Optional key is 'propagate_at_launch', which defaults to true.
target_group_arns
(added in 2.4)
no
List of target group ARNs to use for the group
termination_policies
(added in 2.0)
no Default
  • OldestInstance
  • NewestInstance
  • OldestLaunchConfiguration
  • ClosestToNextInstanceHour
  • Default
An ordered list of criteria used for selecting instances to be removed from the Auto Scaling group when reducing capacity.
For 'Default', when used to create a new autoscaling group, the "Default"i value is used. When used to change an existent autoscaling group, the current termination policies are maintained.
validate_certs
(added in 1.5)
no yes
  • yes
  • no
When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.
vpc_zone_identifier
no None
List of VPC subnets to use
wait_for_instances
(added in 1.9)
no True
Wait for the ASG instances to be in a ready state before exiting. If instances are behind an ELB, it will wait until the ELB determines all instances have a lifecycle_state of "InService" and a health_status of "Healthy".
wait_timeout
(added in 1.8)
no 300
how long before wait instances to become viable when replaced. Used in conjunction with instance_ids option.

Examples

# Basic configuration

- ec2_asg:
    name: special
    load_balancers: [ 'lb1', 'lb2' ]
    availability_zones: [ 'eu-west-1a', 'eu-west-1b' ]
    launch_config_name: 'lc-1'
    min_size: 1
    max_size: 10
    desired_capacity: 5
    vpc_zone_identifier: [ 'subnet-abcd1234', 'subnet-1a2b3c4d' ]
    tags:
      - environment: production
        propagate_at_launch: no

# Rolling ASG Updates

# Below is an example of how to assign a new launch config to an ASG and terminate old instances.
#
# All instances in "myasg" that do not have the launch configuration named "my_new_lc" will be terminated in
# a rolling fashion with instances using the current launch configuration, "my_new_lc".
#
# This could also be considered a rolling deploy of a pre-baked AMI.
#
# If this is a newly created group, the instances will not be replaced since all instances
# will have the current launch configuration.

- name: create launch config
  ec2_lc:
    name: my_new_lc
    image_id: ami-lkajsf
    key_name: mykey
    region: us-east-1
    security_groups: sg-23423
    instance_type: m1.small
    assign_public_ip: yes

- ec2_asg:
    name: myasg
    launch_config_name: my_new_lc
    health_check_period: 60
    health_check_type: ELB
    replace_all_instances: yes
    min_size: 5
    max_size: 5
    desired_capacity: 5
    region: us-east-1

# To only replace a couple of instances instead of all of them, supply a list
# to "replace_instances":

- ec2_asg:
    name: myasg
    launch_config_name: my_new_lc
    health_check_period: 60
    health_check_type: ELB
    replace_instances:
    - i-b345231
    - i-24c2931
    min_size: 5
    max_size: 5
    desired_capacity: 5
    region: us-east-1

Return Values

Common return values are documented here Return Values, the following are the fields unique to this module:

name description returned type sample
default_cooldown
The default cooldown time in seconds.
success int 300
desired_capacity
The number of EC2 instances that should be running in this group.
success int 3
healthcheck_period
Length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health.
success int 30
healthcheck_type
The service you want the health status from, one of "EC2" or "ELB".
success str ELB
healthy_instances
Number of instances in a healthy state
success int 5
in_service_instances
Number of instances in service
success int 3
instance_facts
Dictionary of EC2 instances and their status as it relates to the ASG.
success dict {'i-0123456789012': {'lifecycle_state': 'InService', 'health_status': 'Healthy', 'launch_config_name': 'public-webapp-production-1'}}
instances
list of instance IDs in the ASG
success list ['i-0123456789012']
launch_config_name
Name of launch configuration associated with the ASG. Same as launch_configuration_name, provided for compatibility with ec2_asg module.
success str public-webapp-production-1
load_balancers
List of load balancers names attached to the ASG.
success list ['elb-webapp-prod']
max_size
Maximum size of group
success int 3
min_size
Minimum size of group
success int 1
pending_instances
Number of instances in pending state
success int 1
tags
List of tags for the ASG, and whether or not each tag propagates to instances at launch.
success list [{'propagate_at_launch': 'true', 'key': 'Name', 'value': 'public-webapp-production-1', 'resource_type': 'auto-scaling-group', 'resource_id': 'public-webapp-production-1'}, {'propagate_at_launch': 'true', 'key': 'env', 'value': 'production', 'resource_type': 'auto-scaling-group', 'resource_id': 'public-webapp-production-1'}]
target_group_arns
List of ARNs of the target groups that the ASG populates
success list ['arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:targetgroup/target-group-host-hello/1a2b3c4d5e6f1a2b', 'arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:targetgroup/target-group-path-world/abcd1234abcd1234']
target_group_names
List of names of the target groups that the ASG populates
success list ['target-group-host-hello', 'target-group-path-world']
termination_policies
A list of termination policies for the group.
success str ['Default']
unhealthy_instances
Number of instances in an unhealthy state
success int 0
viable_instances
Number of instances in a viable state
success int 1

Notes

Note

  • If parameters are not set within the module, the following environment variables can be used in decreasing order of precedence AWS_URL or EC2_URL, AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY or EC2_ACCESS_KEY, AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY or EC2_SECRET_KEY, AWS_SECURITY_TOKEN or EC2_SECURITY_TOKEN, AWS_REGION or EC2_REGION
  • Ansible uses the boto configuration file (typically ~/.boto) if no credentials are provided. See http://boto.readthedocs.org/en/latest/boto_config_tut.html
  • AWS_REGION or EC2_REGION can be typically be used to specify the AWS region, when required, but this can also be configured in the boto config file

Status

This module is flagged as stableinterface which means that the maintainers for this module guarantee that no backward incompatible interface changes will be made.

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Testing Ansible and Developing Modules.

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.4/ec2_asg_module.html