Skip to content

Commit

Permalink
ecs_service supports load balancer update (#1625) (#1672)
Browse files Browse the repository at this point in the history
[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 <[email protected]>
  • Loading branch information
patchback[bot] authored Jan 23, 2023
1 parent 1bdb9b0 commit d34fe8e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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).
11 changes: 9 additions & 2 deletions plugins/modules/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'])

Expand Down Expand Up @@ -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':
Expand All @@ -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'],
Expand All @@ -1054,6 +1060,7 @@ def main():
module.params['health_check_grace_period_seconds'],
module.params['force_new_deployment'],
capacityProviders,
updatedLoadBalancers,
)

else:
Expand Down
47 changes: 47 additions & 0 deletions tests/integration/targets/ecs_cluster/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d34fe8e

Please sign in to comment.