Repository: ansible-collections/community.aws

Issue:
ecs_service load_balancers not working with container port
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
The ecs_service module doesn't work as expected when a load balancer is defined with a container port.
##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
ecs_service.py
##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
v2.10.1
```
##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
Docker inside alpine image.

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
Just execute the following task and it should fail.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: "Criando/atualizando serviço {{ service.name }}" 
  community.aws.ecs_service:
    aws_access_key: "{{ ec2_access_key }}"
    aws_secret_key: "{{ ec2_secret_key }}"
    state: present
    force_new_deployment: yes
    name: "{{ service.name }}"
    cluster: "{{ service.cluster }}"
    region: "{{ service.region }}"
    task_definition: "{{ service.task }}"
    desired_count: 1 #service.desired_count
    launch_type: "{{ service.launch_type }}"
    load_balancers:
      - targetGroupArn: "{{ service.alb_arn_tg }}"
        containerName: "{{ service.http_container }}"
        containerPort: 80 
    network_configuration:
      subnets:
      - "{{ service.subnet  }}"
      security_groups:
      - "{{ service.security_group }}"
```
My var file is here, in case you're wondering:
```yaml
  name: "Service_Site_Plataforma"
  cluster: "openbox-plataforma"
  region: "us-east-1"
  task: "openbox-plataforma-prod:11"
  desired_count: 1
  launch_type: "FARGATE"
  alb_arn_tg: "arn:aws:elasticloadbalancing:us-east-1:880610698981:targetgroup/ECS-site/5f7722edab69f6a3"
  http_container: "nginx-server"
  http_container_port: 80
  vpc: "vpc-0813fd6181f0580a4" 
  subnet: "subnet-051274368a7c79e66"
  security_group: "sg-08e8273b3b2959ada"
```
<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
It should work.

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
It fails with the following error message:
<!--- Paste verbatim command output between quotes -->
```paste below
TASK [ecs-services : Criando/atualizando serviço Service_Site_Plataforma] ******

task path: /ansible/roles/ecs-services/tasks/ecs-service.yml:1

<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root

<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'

<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260 `" && echo ansible-tmp-1602784189.4565227-171-150065538834260="` echo /root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260 `" ) && sleep 0'

Using module file /root/.ansible/collections/ansible_collections/community/aws/plugins/modules/ecs_service.py

<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-178n3qw9g/tmpg7h1z76m TO /root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py

<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/ /root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py && sleep 0'

<127.0.0.1> EXEC /bin/sh -c '/usr/local/bin/python /root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py && sleep 0'

<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/ > /dev/null 2>&1 && sleep 0'

The full traceback is:

Traceback (most recent call last):

  File "/root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py", line 102, in <module>

    _ansiballz_main()

  File "/root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py", line 94, in _ansiballz_main

    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)

  File "/root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py", line 40, in invoke_module

    runpy.run_module(mod_name='ansible_collections.community.aws.plugins.modules.ecs_service', init_globals=None, run_name='__main__', alter_sys=True)

  File "/usr/local/lib/python3.6/runpy.py", line 205, in run_module

    return _run_module_code(code, init_globals, run_name, mod_spec)

  File "/usr/local/lib/python3.6/runpy.py", line 96, in _run_module_code

    mod_name, mod_spec, pkg_name, script_name)

  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code

    exec(code, run_globals)

  File "/tmp/ansible_community.aws.ecs_service_payload_a3wwyvdm/ansible_community.aws.ecs_service_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_service.py", line 860, in <module>

  File "/tmp/ansible_community.aws.ecs_service_payload_a3wwyvdm/ansible_community.aws.ecs_service_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_service.py", line 754, in main

TypeError: string indices must be integers

fatal: [localhost]: FAILED! => {

    "changed": false,

    "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1602784189.4565227-171-150065538834260/AnsiballZ_ecs_service.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.aws.plugins.modules.ecs_service', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/usr/local/lib/python3.6/runpy.py\", line 205, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/local/lib/python3.6/runpy.py\", line 96, in _run_module_code\n    mod_name, mod_spec, pkg_name, script_name)\n  File \"/usr/local/lib/python3.6/runpy.py\", line 85, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.aws.ecs_service_payload_a3wwyvdm/ansible_community.aws.ecs_service_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_service.py\", line 860, in <module>\n  File \"/tmp/ansible_community.aws.ecs_service_payload_a3wwyvdm/ansible_community.aws.ecs_service_payload.zip/ansible_collections/community/aws/plugins/modules/ecs_service.py\", line 754, in main\nTypeError: string indices must be integers\n",

    "module_stdout": "",

    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",

    "rc": 1

}
```



Discussion:
somewhere is a type error

> TypeError: string indices must be integers

So somehwere you're refering to a position of an array? But I cannot find it adhoc.  
Basically we do the same all day...

```yml
    - name: update or create ecs service for
      ecs_service:
        state: present
        region: "{{ region }}"
        name: "{{ SERVICE }}"
        cluster: "{{ CLUSTER }}"
        task_definition: "{{ TASK_NAME }}:{{ output.taskdefinition.revision }}"
        desired_count: "{{ DESIRED_COUNT }}"
        launch_type: EC2
        load_balancers:
           - targetGroupArn: "{{ tg.target_groups[0].target_group_arn }}"
              containerName: website
              containerPort: 80
        deployment_configuration:
          minimum_healthy_percent: 50
```
#264 
same error. `element` type is wrong for load _balancers.

Resolving patch:
diff --git a/plugins/modules/ecs_service.py b/plugins/modules/ecs_service.py
--- a/plugins/modules/ecs_service.py
+++ b/plugins/modules/ecs_service.py
@@ -52,7 +52,7 @@
           - The list of ELBs defined for this service.
         required: false
         type: list
-        elements: str
+        elements: dict
     desired_count:
         description:
           - The count of how many instances of the service.
@@ -648,7 +648,7 @@ def main():
         name=dict(required=True, type='str'),
         cluster=dict(required=False, type='str'),
         task_definition=dict(required=False, type='str'),
-        load_balancers=dict(required=False, default=[], type='list', elements='str'),
+        load_balancers=dict(required=False, default=[], type='list', elements='dict'),
         desired_count=dict(required=False, type='int'),
         client_token=dict(required=False, default='', type='str'),
         role=dict(required=False, default='', type='str'),


Test patch: