An Elastic Load Balancer (ELB) is typically used to load balance requests across two (or more) different EC2 instances.
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
Prerequisites
- Before you can use the Ansible Amazon Web Services (AWS) modules, you will need to install the AWS CLI tool on the hosts that will be using the Ansible Amazon Web Services (AWS) modules. Check out my article on Getting Started with the Ansible Amazon Web Services (AWS) modules.
- You will also need to set your Amazon Web Services (AWS) Profile Configurations. Check out my article Set Amazon Web Services (AWS) Profile Configurations.
- The aws_s3_bucket_info requires the following packages. Check out my article Resolve "boto3 required for this module".
- botocore version 1.25.0 or higher
- boto3 version 1.22.0 or higher
- Python 3.6 or higher must be used. The ansible --version command can be used to list the version of Python being used with Ansible. If your Ansible installation is used a version lower than Python 3.6, one solution would be to install Ansible in a Python virtual environment using Python 3.6 or higher.
- The community.aws collection will need to be installed. Check out my article on Install a collection using the ansible-galaxy collection install command.
amazon.aws.elb_application_lb can be used to create an Elastic Load Balancer. There are a few different types of load balancers.
- Application Load Balancers (e.g. you have a web app that you want to load balance)
- Network Load Balancers (e.g. you have SQL databases that you want to load balance)
- Gateway Load Balancers
- Classic Load Balancers (deprecated)
When creating an Application Load Balancer, you will need a Target Group. A Target Group is:
- One or more EC2 instances using the EC2 instances ID
- One or more EC2 instances via IP address
- One or more EC2 instances via Lambda
- One or more Application Load Balancers (in other words, an group of Application Load Balancers)
Before creating the target group, you will need to get the Virtual Private Cloud (VPC) ID that the Target Group will reside in. In this example, TBD can be used to list your Virtual Private Clouds (VPC).
TBD
Notice in this example that the Virtual Private Cloud (VPC) has CIDR block (e.g. 172.31.0.0/16). In this scenario, the Target Group would need to consist of EC2 instances that are also in the same Virtual Private Cloud (VPC).
- community.aws.elb_target_group can be used to create a Target Group. Check out my article Ansible Create an Amazon Web Services (AWS) Elastic Load Balancer (ELB) Target Group.
- amazon.aws.ec2_vpc_subnet_info can be used to list your Amazon Web Services (AWS) Virtual Private Cloud (VPC) Subnets. Check out my article Ansible List Amazon Web Services (AWS) VPC Subnets using the ec2_vpc_subnet_info module.
- amazon.aws.elb_application_lb can be used to create an Elastic Load Balancer.
Here is a playbook that could be used to create an Elastic Load Balancer.
---
- name: main play
hosts: localhost
tasks:
- name: list AWS VPC Subnets
amazon.aws.ec2_vpc_subnet_info:
register: aws_ec2_vpc_subnets
- debug:
var: aws_ec2_vpc_subnets
- name: Create a Target Group listening on HTTP port 80
community.aws.elb_target_group:
name: my-target-group
protocol: http
port: 80
vpc_id: vpc-014d21234335abcd
state: present
register: elb_target_group
- debug:
var: elb_target_group
- amazon.aws.elb_application_lb:
name: my-application-load-balancer
security_groups:
- "{{ aws_security_group_id }}"
subnets:
- "{{ subnet_ids[0] }}"
- "{{ subnet_ids[1] }}"
listeners:
- Protocol: HTTP
Port: 80
DefaultActions:
- Type: forward
TargetGroupName: my-target-group
state: present
register: elb_application_lb
- debug:
var: elb_application_lb
...
Did you find this article helpful?
If so, consider buying me a coffee over at