From d34fe8e6d47233ab32d1868420cf4243bfad7bf6 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 23 Jan 2023 12:28:20 +0000 Subject: [PATCH] ecs_service supports load balancer update (#1625) (#1672) [PR #1625/5bcc8fbf backport][stable-5] ecs_service supports load balancer update This is a backport of PR #1625 as merged into main (5bcc8fb). SUMMARY ISSUE TYPE Feature Pull Request COMPONENT NAME ecs_service ADDITIONAL INFORMATION As aws docs says, aws ecs service now supports load balancer update with UpdateService API if deploymentController type is ECS. This pull request supports load balancer update. Reviewed-by: Markus Bergholz --- ...s_service-support-load-balancer-update.yml | 2 + plugins/modules/ecs_service.py | 11 ++++- .../targets/ecs_cluster/tasks/main.yml | 47 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/1625-ecs_service-support-load-balancer-update.yml diff --git a/changelogs/fragments/1625-ecs_service-support-load-balancer-update.yml b/changelogs/fragments/1625-ecs_service-support-load-balancer-update.yml new file mode 100644 index 00000000000..0a4e71e8109 --- /dev/null +++ b/changelogs/fragments/1625-ecs_service-support-load-balancer-update.yml @@ -0,0 +1,2 @@ +minor_changes: + - ecs_service - support load balancer update for existing ecs services(https://github.com/ansible-collections/community.aws/pull/1625). \ No newline at end of file diff --git a/plugins/modules/ecs_service.py b/plugins/modules/ecs_service.py index e7d9192e3e3..a5af0df79fd 100644 --- a/plugins/modules/ecs_service.py +++ b/plugins/modules/ecs_service.py @@ -822,7 +822,7 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan def update_service(self, service_name, cluster_name, task_definition, desired_count, deployment_configuration, placement_constraints, placement_strategy, network_configuration, health_check_grace_period_seconds, - force_new_deployment, capacity_provider_strategy): + force_new_deployment, capacity_provider_strategy, load_balancers): params = dict( cluster=cluster_name, service=service_name, @@ -849,6 +849,9 @@ def update_service(self, service_name, cluster_name, task_definition, desired_co if desired_count is not None: params['desiredCount'] = desired_count + if load_balancers: + params['loadBalancers'] = load_balancers + response = self.ecs.update_service(**params) return self.jsonize(response['service']) @@ -1027,7 +1030,8 @@ def main(): if 'capacityProviderStrategy' in existing.keys(): module.fail_json(msg="It is not possible to change an existing service from capacity_provider_strategy to launch_type.") if (existing['loadBalancers'] or []) != loadBalancers: - if existing['deploymentController']['type'] != 'CODE_DEPLOY': + # fails if deployment type is not CODE_DEPLOY or ECS + if existing['deploymentController']['type'] not in ['CODE_DEPLOY', 'ECS']: module.fail_json(msg="It is not possible to update the load balancers of an existing service") if existing.get('deploymentController', {}).get('type', None) == 'CODE_DEPLOY': @@ -1042,6 +1046,8 @@ def main(): if module.params['tags'] and boto3_tag_list_to_ansible_dict(existing['tags']) != module.params['tags']: module.fail_json(msg="It is not currently supported to change tags of an existing service") + updatedLoadBalancers = loadBalancers if existing['deploymentController']['type'] == 'ECS' else [] + # update required response = service_mgr.update_service(module.params['name'], module.params['cluster'], @@ -1054,6 +1060,7 @@ def main(): module.params['health_check_grace_period_seconds'], module.params['force_new_deployment'], capacityProviders, + updatedLoadBalancers, ) else: diff --git a/tests/integration/targets/ecs_cluster/tasks/main.yml b/tests/integration/targets/ecs_cluster/tasks/main.yml index edeaf7b452e..17e4d4c4a25 100644 --- a/tests/integration/targets/ecs_cluster/tasks/main.yml +++ b/tests/integration/targets/ecs_cluster/tasks/main.yml @@ -607,6 +607,44 @@ assert: that: - ecs_task_definition_constraints_delete is not changed + + - name: Create ecs_service without load balancer + ecs_service: + name: "{{ ecs_service_name }}-lb" + cluster: "{{ ecs_cluster_name }}" + task_definition: "{{ ecs_task_name }}:{{ ecs_task_definition.taskdefinition.revision }}" + scheduling_strategy: "REPLICA" + desired_count: 1 + state: present + register: ecs_service_create_no_load_balancer + + - name: Check ecs_service does not have load balancer + assert: + that: + - ecs_service_create_no_load_balancer.changed + - "ecs_service_create_no_load_balancer.service.loadBalancers | length == 0" + + - name: Update ecs_service load balancer + ecs_service: + name: "{{ ecs_service_name }}-lb" + cluster: "{{ ecs_cluster_name }}" + load_balancers: + - targetGroupArn: "{{ elb_target_group_ip.target_group_arn }}" + containerName: "{{ ecs_task_name }}" + containerPort: "{{ ecs_task_container_port }}" + task_definition: "{{ ecs_task_name }}:{{ ecs_task_definition.taskdefinition.revision }}" + desired_count: 1 + state: present + register: ecs_service_update_load_balancer + + - name: Check ecs_service load balancer updated + assert: + that: + - ecs_service_update_load_balancer.changed + - "ecs_service_update_load_balancer.service.loadBalancers | length == 1" + - "ecs_service_update_load_balancer.service.loadBalancers[0].containerName == {{ ecs_task_name }}" + - "ecs_service_update_load_balancer.service.loadBalancers[0].containerPort == {{ ecs_task_container_port }}" + - "ecs_service_update_load_balancer.service.loadBalancers[0].loadBalancerName == {{ ecs_load_balancer_name }}" - name: Create ecs service with placement constraints ecs_service: @@ -1158,6 +1196,15 @@ force_deletion: yes wait: yes ignore_errors: yes + + - name: remove load balancer ecs service + ecs_service: + state: absent + cluster: "{{ ecs_cluster_name }}" + name: "{{ ecs_service_name }}-lb" + force_deletion: yes + wait: yes + ignore_errors: yes - name: remove fargate ECS service ecs_service: