# Creating an aws target group with ansible

**URL:** <https://forum.ansible.com/t/creating-an-aws-target-group-with-ansible/29147>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [November 29, 2018, 4:16am UTC](https://forum.ansible.com/t/creating-an-aws-target-group-with-ansible/29147 "2018-11-29T04:16:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeldamata](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@joeldamata](https://forum.ansible.com/u/joeldamata)\
**Post date:** [November 29, 2018, 4:16am UTC](https://forum.ansible.com/t/creating-an-aws-target-group-with-ansible/29147/1 "2018-11-29T04:16:58Z")

</div>

What Im trying to accomplish is to create a network load balancer target group from dynamically generated list of instances.

brokerInstancesList is a list of instance ids. I need to iterate over this list and add them as targets to this target group.

```auto
- name: "Create 9092 target group"
  elb_target_group:
    name: "tg-{{ ClusterName }}"
    protocol: tcp
    port: 9092
    vpc_id: "{{ VPCID }}"
    targets:
      - Id: "{{ item }}"
        Port: 9092
    state: present
  loop: "{{ brokerInstancesList }}"

```

The issue with my attempt above is that only the last entry in the brokerInstancesList is kept. Something like the below is what I need.

```auto
- name: "Create 9092 target group"
  elb_target_group:
    name: "tg-{{ ClusterName }}"
    protocol: tcp
    port: 9092
    vpc_id: "{{ VPCID }}"
    targets:
      {% for item in {{ brokerInstancesList }} -%}
      - Id: "{{ apple }}"
        Port: 9092
      {%- endfor %}
    state: present

```

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [November 29, 2018, 5:36am UTC](https://forum.ansible.com/t/creating-an-aws-target-group-with-ansible/29147/2 "2018-11-29T05:36:31Z")

</div>

The elb\_target\_group interface maps very easily to the AWS API, but it’s a bit clunky to use.

`

targets: “{{ brokerInstancesList | map(‘json\_query’, ‘{Id: @}’) | map(‘combine’, { ‘Port’: 9092 }) | list }}”

`

(It might be possible to do this in fewer operations with a more complex JMESPath expression, but this way works and is almost readable.)

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [November 29, 2018, 5:40am UTC](https://forum.ansible.com/t/creating-an-aws-target-group-with-ansible/29147/3 "2018-11-29T05:40:37Z")

</div>

I suggest for readability that you build a list using set\_fact:, then pass the list to the targets: parameter.

Regards, K.
