amazon.aws.autoscaling_group module – Create or delete AWS AutoScaling Groups (ASGs)
Note
This module is part of the amazon.aws collection (version 10.0.0-dev0).
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install amazon.aws
.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: amazon.aws.autoscaling_group
.
New in amazon.aws 5.0.0
Synopsis
Can create or delete AWS AutoScaling Groups.
Can be used with the community.aws.autoscaling_launch_config module to manage Launch Configurations.
Prior to release 5.0.0 this module was called community.aws.ec2_asg. The usage did not change.
This module was originally added to
community.aws
in release 1.0.0.
Aliases: ec2_asg
Requirements
The below requirements are needed on the host that executes this module.
python >= 3.6
boto3 >= 1.28.0
botocore >= 1.31.0
Parameters
Parameter |
Comments |
---|---|
AWS access key ID. See the AWS documentation for more information about access tokens https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys. The The aws_access_key and profile options are mutually exclusive. The aws_access_key_id alias was added in release 5.1.0 for consistency with the AWS botocore SDK. The ec2_access_key alias has been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
List of availability zone names in which to create the group. Defaults to all the availability zones in the region if |
|
The location of a CA Bundle to use when validating SSL certificates. The |
|
A dictionary to modify the botocore configuration. Parameters can be found in the AWS documentation https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config. |
|
Use a The Choices:
|
|
Support for the Indicates whether the AutoScalingGroup decrements the desired capacity value by the number of instances detached. Choices:
|
|
The number of seconds after a scaling activity completes before another can begin. Default: |
|
Desired number of instances in group, if unspecified then the current group value will be used. |
|
Support for the Removes one or more instances from the specified AutoScalingGroup. If If a Classic Load Balancer is attached to the AutoScalingGroup, the instances are also deregistered from the load balancer. If there are target groups attached to the AutoScalingGroup, the instances are also deregistered from the target groups. Default: |
|
URL to connect to instead of the default AWS endpoints. While this can be used to connection to other AWS-compatible services the amazon.aws and community.aws collections are only tested against AWS. The The ec2_url and s3_url aliases have been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
Length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health. Default: |
|
The service you want the health status from, Amazon EC2 or Elastic Load Balancer. Choices:
|
|
Name of the Launch configuration to use for the group. See the community.aws.autoscaling_launch_config module for managing these. Exactly one of Note Amazon has deprecated support for AutoScaling Launch Configurations in favour of EC2 Launch Templates. See https://docs.aws.amazon.com/autoscaling/ec2/userguide/launch-configurations.html for more information |
|
Dictionary describing the Launch Template to use. Exactly one of |
|
The id of the launch template. Only one of |
|
The name of the launch template. Only one of |
|
The version number of the launch template to use. Defaults to latest version if not provided. |
|
Support for the Check to make sure instances that are being replaced with Choices:
|
|
List of ELB names to use for the group. Use for classic load balancers. |
|
Support for the Check to make sure instances that are being replaced with Choices:
|
|
The maximum amount of time, in seconds, that an instance can be in service. Maximum instance lifetime must be equal to Value of |
|
Maximum number of instances in group, if unspecified then the current group value will be used. |
|
Enable ASG metrics collection. Choices:
|
|
When Default: |
|
List of autoscaling metrics to collect when Default: |
|
Minimum number of instances in group, if unspecified then the current group value will be used. |
|
A mixed instance policy to use for the ASG. Only used when the ASG is configured to use a Launch Template ( |
|
A list of instance types. |
|
Specifies the distribution of On-Demand Instances and Spot Instances, the maximum price to pay for Spot Instances, and how the Auto Scaling group allocates instance types to fulfill On-Demand and Spot capacity. See also https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_InstancesDistribution.html |
|
Indicates how to allocate instance types to fulfill On-Demand capacity. |
|
The minimum amount of the Auto Scaling group’s capacity that must be fulfilled by On-Demand Instances. This base portion is provisioned first as your group scales. Default if not set is |
|
Controls the percentages of On-Demand Instances and Spot Instances for your additional capacity beyond Default if not set is Valid range: |
|
Indicates how to allocate instances across Spot Instance pools. |
|
The number of Spot Instance pools across which to allocate your Spot Instances. The Spot pools are determined from the different instance types in the Overrides array of LaunchTemplate. Default if not set is Used only when the Spot allocation strategy is lowest-price. Valid Range: Minimum value of |
|
The maximum price per unit hour that you are willing to pay for a Spot Instance. If you leave the value of this parameter blank (which is the default), the maximum Spot price is set at the On-Demand price. To remove a value that you previously set, include the parameter but leave the value blank. |
|
Unique name for group to be created or deleted. |
|
A SNS topic ARN to send auto scaling notifications to. |
|
A list of auto scaling events to trigger notifications on. Default: |
|
Physical location of your cluster placement group created in Amazon EC2. |
|
A named AWS profile to use for authentication. See the AWS documentation for more information about named profiles https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html. The The profile option is mutually exclusive with the aws_access_key, aws_secret_key and security_token options. |
|
The AWS region to use. For global services such as IAM, Route53 and CloudFront, region is ignored. The See the Amazon AWS documentation for more information http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region. The Support for the |
|
Support for the In a rolling fashion, replace all instances that used the old launch configuration with one from the new launch configuration. It increases the ASG size by Choices:
|
|
Support for the Number of instances you’d like to replace at a time. Used with Default: |
|
Support for the List of instance ids belonging to the named AutoScalingGroup that you would like to terminate and be replaced with instances matching the current launch configuration. Default: |
|
AWS secret access key. See the AWS documentation for more information about access tokens https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys. The The secret_key and profile options are mutually exclusive. The aws_secret_access_key alias was added in release 5.1.0 for consistency with the AWS botocore SDK. The ec2_secret_key alias has been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
AWS STS session token for use with temporary credentials. See the AWS documentation for more information about access tokens https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys. The The security_token and profile options are mutually exclusive. Aliases aws_session_token and session_token were added in release 3.2.0, with the parameter being renamed from security_token to session_token in release 6.0.0. The security_token, aws_security_token, and access_token aliases have been deprecated and will be removed in a release after 2024-12-01. Support for the |
|
Register or deregister the instance. Choices:
|
|
A list of scaling processes to suspend. Valid values include:
Full documentation of valid values can be found in the AWS documentation: https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-suspend-resume-processes.html Default: |
|
A list of tags to add to the Auto Scale Group. Optional key is When Default: |
|
List of target group ARNs to use for the group. Use for application load balancers. |
|
An ordered list of criteria used for selecting instances to be removed from the Auto Scaling group when reducing capacity. Using Valid values include: Full documentation of valid values can be found in the AWS documentation: Default: |
|
When set to Setting validate_certs=false is strongly discouraged, as an alternative, consider setting aws_ca_bundle instead. Choices:
|
|
List of VPC subnets to use |
|
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”. Choices:
|
|
How long to wait for instances to become viable when replaced. If you experience the error “Waited too long for ELB instances to be healthy”, try increasing this value. Default: |
Notes
Note
Caution: For modules, environment variables and configuration files are read from the Ansible ‘host’ context and not the ‘controller’ context. As such, files may need to be explicitly copied to the ‘host’. For lookup and connection plugins, environment variables and configuration files are read from the Ansible ‘controller’ context and not the ‘host’ context.
The AWS SDK (boto3) that Ansible uses may also read defaults for credentials and other settings, such as the region, from its configuration files in the Ansible ‘host’ context (typically
~/.aws/credentials
). See https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html for more information.
Examples
# Basic configuration with Launch Template
- name: Create an autoscaling group using launch template
amazon.aws.autoscaling_group:
name: example_asg
load_balancers: ['lb1', 'lb2']
availability_zones: ['eu-west-1a', 'eu-west-1b']
launch_template:
launch_template_name: 'template-1'
min_size: 1
max_size: 10
desired_capacity: 5
vpc_zone_identifier: ['subnet-abcd1234', 'subnet-1a2b3c4d']
tags:
- environment: production
propagate_at_launch: false
# Rolling ASG Updates
# Below is an example of how to assign a new launch template to an ASG and replace old instances.
# By setting max_healthy_percentage to a value over 100 the old rolling-replacement behaviour of
# scaling up before scaling in can be maintained.
- name: Update autoscaling group with new template - instances are not replaced
amazon.aws.autoscaling_group:
name: example_asg
launch_template:
launch_template_name: template-2
health_check_period: 60
health_check_type: ELB
min_size: 2
max_size: 13
desired_capacity: 6
region: us-east-1
- name: Replace 2 instances based on EC2 Instance ID by marking them for termination
amazon.aws.autoscaling_instance:
group_name: example_asg
state: terminated
instance_ids:
- i-b345231
- i-24c2931
decrement_desired_capacity: false
wait: true
- name: Trigger rolling replacement of all instances that do not match the current configuration.
amazon.aws.autoscaling_instance_refresh:
group_name: example_asg
state: started
strategy: Rolling
preferences:
skip_matching: true
max_healthy_percentage: 125 # scale out before terminating instances during replacement
# Basic Configuration with Launch Template
- name: Example autoscaling group creation with a launch template
amazon.aws.autoscaling_group:
name: example_with_template
load_balancers: ['lb1', 'lb2']
availability_zones: ['eu-west-1a', 'eu-west-1b']
launch_template:
version: '1'
launch_template_name: 'lt-example'
launch_template_id: 'lt-123456'
min_size: 1
max_size: 10
desired_capacity: 5
vpc_zone_identifier: ['subnet-abcd1234', 'subnet-1a2b3c4d']
tags:
- environment: production
propagate_at_launch: false
# Basic Configuration with Launch Template using mixed instance policy
- name: Example autoscaling group creation with a mixed instance policy
amazon.aws.autoscaling_group:
name: example_with_policy
load_balancers: ['lb1', 'lb2']
availability_zones: ['eu-west-1a', 'eu-west-1b']
launch_template:
version: '1'
launch_template_name: 'lt-example'
launch_template_id: 'lt-123456'
mixed_instances_policy:
instance_types:
- t3a.large
- t3.large
- t2.large
instances_distribution:
on_demand_percentage_above_base_capacity: 0
spot_allocation_strategy: capacity-optimized
min_size: 1
max_size: 10
desired_capacity: 5
vpc_zone_identifier: ['subnet-abcd1234', 'subnet-1a2b3c4d']
tags:
- environment: production
propagate_at_launch: false
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
The unique ARN of the autoscaling group Returned: success Sample: |
|
The unique name of the auto scaling group Returned: success Sample: |
|
The availability zones for the auto scaling group Returned: success Sample: |
|
Timestamp of create time of the auto scaling group Returned: success Sample: |
|
The default cooldown time in seconds. Returned: success Sample: |
|
The number of EC2 instances that should be running in this group. Returned: success Sample: |
|
Length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health. Returned: success Sample: |
|
The service you want the health status from, one of “EC2” or “ELB”. Returned: success Sample: |
|
Number of instances in a healthy state Returned: success Sample: |
|
Number of instances in service Returned: success Sample: |
|
Dictionary of EC2 instances and their status as it relates to the ASG. Returned: success Sample: |
|
list of instance IDs in the ASG Returned: success Sample: |
|
Name of launch configuration associated with the ASG. Same as launch_configuration_name, provided for compatibility with amazon.aws.autoscaling_group module. Returned: success Sample: |
|
List of load balancers names attached to the ASG. Returned: success Sample: |
|
The maximum amount of time, in seconds, that an instance can be in service. Returned: success Sample: |
|
Maximum size of group Returned: success Sample: |
|
List of enabled AutosSalingGroup metrics Returned: success Sample: |
|
Minimum size of group Returned: success Sample: |
|
Returns the list of instance types if a mixed instances policy is set. Returned: success Sample: |
|
Returns the full dictionary representation of the mixed instances policy if a mixed instances policy is set. Returned: success Sample: |
|
Number of instances in pending state Returned: success Sample: |
|
List of tags for the ASG, and whether or not each tag propagates to instances at launch. Returned: success Sample: |
|
List of ARNs of the target groups that the ASG populates Returned: success Sample: |
|
List of names of the target groups that the ASG populates Returned: success Sample: |
|
A list of termination policies for the group. Returned: success Sample: |
|
Number of instances in an unhealthy state Returned: success Sample: |
|
Number of instances in a viable state Returned: success Sample: |
|
VPC zone ID / subnet id for the auto scaling group Returned: success Sample: |