In our previous article, What's New: Cloud Automation with amazon.aws 9.0.0, we introduced the latest release of the Red Hat Ansible Certified Content Collection for Amazon Web Services (AWS). We covered key updates, new features, and recently supported modules designed to simplify cloud automation.
This article kicks off a three-part series exploring real-world use cases that showcase the power of these new capabilities. In this installment, we’ll focus on automating infrastructure scaling, leveraging Red Hat Ansible Automation Platform to efficiently manage Auto Scaling Groups (ASGs), Elastic Compute Cloud (EC2) launch templates, and instance refresh processes for seamless scaling and configuration consistency.
Stay tuned for the next two posts, where we’ll cover secure network segmentation and multi-virtual private cloud (VPC) connectivity—two critical aspects of modern cloud architectures. For now, let’s dive into automation strategies that help you scale smarter with Ansible Automation Platform and AWS.
Automated infrastructure scaling and refreshAutomated infrastructure scaling and refresh
In dynamic cloud environments, applications must be able to efficiently scale in response to fluctuating workloads. Auto Scaling Groups provide this flexibility by automatically adjusting the number of EC2 instances based on demand. However, ensuring that all instances in the ASG are consistently updated with the latest configurations can be a challenge, especially when changes need to be applied without causing service disruption.
This use case highlights the power of automation in managing a scalable, resilient, and cost-optimized infrastructure. By minimizing manual intervention and downtime, it ensures the infrastructure can handle fluctuating workloads while staying up to date with the latest configurations and maintaining high availability.
This use case focuses on leveraging the new modules shown in the following table.
Module | Description |
ec2_launch_template | Manage EC2 launch templates. |
ec2_launch_template_info | Retrieve information about EC2 launch templates. |
autoscaling_group | Manage AWS ASGs. |
autoscaling_instance | Manage instances associated with AWS Auto Scaling Groups (ASGs). |
autoscaling_instance_info | Retrieve information about instances associated with ASGs. |
ec2_placement_group | Manage EC2 Placement Groups. |
ec2_placement_group_info | List EC2 Placement Group(s) details. |
autoscaling_instance_refresh | Start or cancel an EC2 ASG instance refresh in AWS. |
autoscaling_instance_refresh_info | Retrieve information about EC2 ASG instance refreshes. |
5-step guide for implementation
Now that we've outlined the key automation modules, let's walk through the step-by-step implementation of an automated, scalable, and resilient infrastructure using Ansible Automation Platform.
This guide will demonstrate how to:
- Create an EC2 launch template for consistent instance deployment.
- Set up an ASG to dynamically adjust capacity.
- Optimize instance distribution with an EC2 Placement Group.
- Attach an existing EC2 Instance to the ASG.
- Perform a rolling instance refresh to update configurations seamlessly.
By following these steps, we ensure that our infrastructure remains highly available, cost-optimized, and always up to date without manual intervention.
Let’s break down an Ansible Playbook into several steps, and learn how each one assists in automating and streamlining the use case scenario I described.
Step 1: Create an EC2 launch template
An EC2 launch template serves as a blueprint for provisioning EC2 instances, ensuring that all instances within an ASG share consistent configurations. It encapsulates key settings such as AMI IDs, instance types, security groups, and key pairs. The amazon.aws.ec2_launch_template
module creates a launch template defining instance configurations.
Using amazon.aws.ec2_launch_template_info
, you can verify that the launch template is correctly configured as follows:
---
- name: Automate Autoscaling and Placement
hosts: localhost
tasks:
- name: Create an EC2 launch template
amazon.aws.ec2_launch_template:
state: present
name: "my-launch-template"
image_id: "ami-1234567890abcdef0"
instance_type: t3.micro
key_name: "my-key-pair"
tags:
Environment: Production
register: _result_launch_template
- name: Retrieve details about the launch template
amazon.aws.ec2_launch_template_info:
filters:
launch-template-name: "my-launch-template"
register: _result_launch_template_info
You can see the logs of the amazon.aws.ec2_launch_template_info
module that confirm successful creation and retrieval of the launch template:
[Partial log]
…
"launch_templates": [
{
"create_time": "2025-01-20T11:22:45+00:00",
"created_by": "arn:aws:iam::123456789101:user/abuzachis",
"default_version_number": 1,
"latest_version_number": 1,
"launch_template_id": "lt-06454f169fc012457",
"launch_template_name": "my-launch-template",
"tags": {
"Environment": "Production"
},
"versions": [
{
"create_time": "2025-01-20T11:22:45+00:00",
"created_by": "arn:aws:iam::123456789101:user/abuzachis",
"default_version": true,
"launch_template_data": {
"image_id": "ami-1234567890abcdef0",
"instance_type": "c4.large"
},
"launch_template_id": "lt-06454f169fc012457",
"launch_template_name": "my-launch-template",
"version_number": 1
}
]
}
]
Step 2: Create an Auto Scaling Group
The amazon.aws.autoscaling_group
module creates an ASG that ensures dynamic scalability by adjusting the number of instances based on demand. The ASG uses the launch template to manage the lifecycle of EC2 instances, maintaining availability and optimizing costs. It ensures the infrastructure can handle variable workloads without manual intervention. You can define the minimum, maximum, and desired capacity of the autoscaling group, ensuring it can adjust based on load. Tags are applied for organizational purposes, and the ASG spans across multiple availability zones to ensure high availability.
- name: Create an ASG
amazon.aws.autoscaling_group:
state: present
name: "my-asg"
launch_template:
launch_template_name: "{{ launch_template_info.name }}"
version: "{{ launch_template_info.latest_version_number }}"
min_size: 1
max_size: 3
desired_capacity: 2
availability_zones:
- "us-east-1a"
- "us-east-1b"
tags:
- environment: production
propagate_at_launch: true
register: _result_autoscaling_group_info
Step 3: Create an EC2 placement group
The amazon.aws.ec2_placement_group
module helps optimize the physical placement of your EC2 instances within the AWS infrastructure. Placement groups can improve network performance by grouping instances closer together, or enhance resilience by spreading instances across hardware.
In this example, a spread strategy is used, which places instances across different hardware. This is essential for ensuring high resilience and availability. In the following output, the amazon.aws.ec2_placement_group_info
module fetches details about the placement group to verify that the group is created and configured as intended:
- name: Create an EC2 placement group
amazon.aws.ec2_placement_group:
name: "my-placement-group"
strategy: spread
state: present
- name: Retrieve information about the placement group
amazon.aws.ec2_placement_group_info:
names:
- "my-placement-group"
The following logs of the amazon.aws.ec2_placement_group_info
module verify the group's configuration:
[Partial log]
…
"placement_groups": [
{
"name": "my-placement-group",
"state": "available",
"strategy": "spread",
"tags": {}
}
]
Step 4: Attach an instance to the ASG
The amazon.aws.autoscaling_instance
module attaches an existing EC2 instance to the ASG, making it part of the scaling group. This ensures that manually launched or external instances can become part of the ASG, optimizing resource utilization and enabling them to benefit from ASG management features like health checks and scaling.
Here, the amazon.aws.autoscaling_instance_info
module retrieves information about the instances currently associated with the ASG:
- name: Attach an external EC2 instance to the ASG
amazon.aws.autoscaling_instance:
group_name: "my-asg"
state: present
instance_ids:
- "i-0abc12345d6789xyz"
wait: true
register: _result_attach_instance
- name: Describe instances associated with ASG
amazon.aws.autoscaling_instance_info:
group_name: "my-asg"
The following is a sample partial log output from the amazon.aws.austocaling_info
module:
[Partial log]
…
"auto_scaling_instances": [
{
"auto_scaling_group_name": "my-asg",
"availability_zone": "us-east-1a",
"health_status": "HEALTHY",
"instance_id": "i-0762ce203680af6b2",
"instance_type": "t3.micro",
"launch_template": {
"launch_template_id": "lt-06454f169fc012457",
"launch_template_name": "my-launch-template",
"version": "1"
},
"lifecycle_state": "InService",
"protected_from_scale_in": false
}
]
Step 5: Perform an instance refresh
The amazon.aws.autoscaling_instance_refresh
initiates a rolling refresh of instances in the ASG, updating them based on the latest launch template. Instance refresh updates the infrastructure without manual intervention, ensuring all instances meet the latest configurations. It ensures at least 50% of instances are healthy during the refresh and waits 300 seconds for new instances to initialize before proceeding.
The amazon.aws.autoscaling_instance_refresh_info
module retrieves the status of the instance refresh process:
- name: Perform an instance refresh to apply new launch template configuration
amazon.aws.autoscaling_instance_refresh:
name: "my-asg"
state: started
preferences:
min_healthy_percentage: 50
instance_warmup: 300
register: _result_instance_refresh
- name: Check the status of the instance refresh
amazon.aws.autoscaling_instance_refresh_info:
name: "my-asg"
Here is a sample partial log from the amazon.aws.austocaling_instance_info
module:
[Partial log]
…
"instance_refreshes": [
{
"auto_scaling_group_name": "my-asg",
"instance_refresh_id": "18462ba8-bfc7-443d-84fa-5821627f714d",
"instances_to_update": 1,
"percentage_complete": 0,
"preferences": {
"alarm_specification": {},
"auto_rollback": false,
"instance_warmup": 300,
"min_healthy_percentage": 50,
"skip_matching": false
},
"start_time": "2025-01-20T09:45:39+00:00",
"status": "Cancelling",
"status_reason": "Replacing instances before cancelling."
},
]
What's next?What's next?
In this article, we showcased how the latest features in the amazon.aws 9.0.0 collection enable you to automate intricate scaling operations within dynamic cloud environments. We walked through a practical use case, from creating an EC2 launch template to performing a rolling instance refresh. We also highlighted how these new modules streamline the management of ASGs, placement groups, and instance configurations. This automation not only reduces the need for manual intervention but also ensures that your infrastructure remains resilient, cost-optimized, and consistently up to date.
Stay tuned for the next two articles in our series, where we’ll explore secure network segmentation and multi-VPC connectivity, the essential components for modern, secure cloud architectures.
Looking to get started with Red Hat Ansible Automation Platform for Amazon Web Services?
- Check out the Amazon Web Services Collection.
- Try out our hands-on Interactive Labs.
- Read the e-book Using automation to get the most from your public cloud.
Learn more:
- For further reading and information, visit the other articles related to Ansible Automation Platform.
- Check out Red Hat Summit 2025.
- Watch the YouTube playlist for everything about Ansible Collections.
- If you're new to Ansible automation, check out our getting started guide on developers.redhat.com.