# How to properyly set up target groups for an application load balancer (ALB)?

**URL:** <https://forum.ansible.com/t/how-to-properyly-set-up-target-groups-for-an-application-load-balancer-alb/27143>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [March 28, 2018, 10:50pm UTC](https://forum.ansible.com/t/how-to-properyly-set-up-target-groups-for-an-application-load-balancer-alb/27143 "2018-03-28T22:50:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Stewart](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@Tim\_Stewart](https://forum.ansible.com/u/Tim_Stewart)\
**Post date:** [March 28, 2018, 10:50pm UTC](https://forum.ansible.com/t/how-to-properyly-set-up-target-groups-for-an-application-load-balancer-alb/27143/1 "2018-03-28T22:50:01Z")

</div>

Hi,

**Background:**

We are developing a system using a Microservices approach and deploying it with Ansible 2.5.0.

I’m writing a site.yml playbook that will provision two Ubuntu 16.04 servers for each of eight different microservices. We’re provisioning two of each microservice for HA and we may increase that number in the future. For example, here are the microservice instances I want to create grouped by subnet (note this is not YAML):

SubNet-a:  
MicroService-1-a  
MicroService-2-a

:  
:  
MicroService-8-a

SubNet-b:

MicroService-1-b  
MicroService-2-b

:  
:  
MicroService-8-b

Now I want to create a Target Group for each kind of microservice (e.g. one for MicroService-1, one for Microservice-2, etc.). For example, here are the target groups and their targets:

MicroService-1-TargetGroup:  
MicroService-1-a  
MicroService-1-b

MicroService-2-TargetGroup:  
MicroService-2-a  
MicroService-2-b

:  
:

MicroService-8-TargetGroup:  
MicroService-8-a  
MicroService-8-b

I then want to set up my ALB (using the elb\_application\_lb module) so that it has a **listener** with eight **rules** that route requests to the various Target groups based on a **path\_pattern** condition.

Here is an example **elb\_target\_group** with hard-coded targets (based on Ansible’s documentation on elb\_target\_groups):

```auto
- elb_target_group:
    name: Microservice-1
    protocol: http
    port: 81
    vpc_id: vpc-01234567
    health_check_path: /
    successful_response_codes: "200"
    targets:
<b> - Id: i-01234567
        Port: 80
      - Id: i-98765432
        Port: 80</b>
    state: present
    wait_timeout: 200
    wait: True

```

But since my site.yml playbook creates the EC2 instances, I won’t have access to the **instance\_id** s referred to in the **Id** parameter above.

What is a good way of specifying the **targets** property of the **elb\_target\_group** (above) such that it only includes all of the EC2 instances of a particular kind of microservice?

For example:

```auto
- elb_target_group:
    name: **Microservice-1**
    protocol: http
    port: 81
    vpc_id: vpc-01234567
    health_check_path: /
    successful_response_codes: "200,250-260"
    targets:
 - Id: *<* *Microservice-1-a's instance_id >* Port: 80 - Id: __*<*__ *Microservice-2-a's instance_id >* Port: 80
    state: present
    wait_timeout: 200
    wait: True

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Tim\_Stewart](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@Tim\_Stewart](https://forum.ansible.com/u/Tim_Stewart)\
**Post date:** [March 29, 2018, 3:30pm UTC](https://forum.ansible.com/t/how-to-properyly-set-up-target-groups-for-an-application-load-balancer-alb/27143/2 "2018-03-29T15:30:45Z")

</div>

One more thing. The reason I did not provide any of my ansible files is that I would rather find the right way of doing this and have to go and change all my Ansible files than have someone try to come up with a solution that works with the potentially bad approach I’ve started from. Thanks!

---

<div class="post-metadata">

**Author:** ![Tim\_Stewart](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@Tim\_Stewart](https://forum.ansible.com/u/Tim_Stewart)\
**Post date:** [March 29, 2018, 8:28pm UTC](https://forum.ansible.com/t/how-to-properyly-set-up-target-groups-for-an-application-load-balancer-alb/27143/3 "2018-03-29T20:28:50Z")

</div>

Here is the solution that I came up with through a good deal of trial and error:

vars:

# The list of service names we will be deploying.

services:

- name: microservice-1  
port: 8080
- name: microservice-2  
port: 8080
- name: microservice-3  
port: 8080  
:  
:
- name: microservice-8  
port: 8080

tasks:

# some tasks omitted

- name: Create Target Groups

elb\_target\_group:  
region: “{{ region }}”  
name: “{{ item.name }}-tg”  
protocol: http  
port: 80  
vpc\_id: “{{ vpc\_id }}”  
health\_check\_path: /health  
health\_check\_interval: 60  
successful\_response\_codes: “200”  
targets: “{{ ec2\_instances | json\_query(query) | map(‘combine’, {‘Port’: item.port}) | list }}”  
tags:  
service: “{{ item.name }}”  
state: present  
register: target\_groups  
with\_items: “{{ services }}”  
vars:  
query: “results.tagged\_instances[?tags.service==‘{{item.name}}’].{Id: id}”

---

<div class="post-metadata">

**Author:** ![Tim\_Stewart](https://avatars.discourse-cdn.com/v4/letter/t/13edae/32.png) [@Tim\_Stewart](https://forum.ansible.com/u/Tim_Stewart)\
**Post date:** [March 30, 2018, 3:07pm UTC](https://forum.ansible.com/t/how-to-properyly-set-up-target-groups-for-an-application-load-balancer-alb/27143/4 "2018-03-30T15:07:35Z")

</div>

BTW, the ec2\_instances variable in my solution is the registered result of the ec2 task run with ‘with\_items’.
