Bootstrap FreeKB - Ansible - Create an Amazon Web Services (AWS) Elastic Load Balancer (ELB)
Ansible - Create an Amazon Web Services (AWS) Elastic Load Balancer (ELB)

Updated:   |  Ansible articles

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

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).

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 Buy Me A Coffee



Comments


Add a Comment


Please enter 80a138 in the box below so that we can be sure you are a human.