I have lambda's and ecs
lambda
controls start/stop the ecs.
However when stopping the ecs with stop_task
, it is revoked immediately by LoadaBalancer
I should stop the LoadBalancer
at the same time with stop_task
Or is there any way to stop the only LoadBalancer
's re-invoke function?
For example stop is like this,
import json
import boto3
ecs = boto3.client('ecs')
def lambda_handler(event, context):
temp = ecs.list_tasks(
cluster='ss-dev-cluster'
)
for t in temp['taskArns']:
response = ecs.stop_task(
cluster='ss-dev-cluster',
task=t
)
print(response)
'''
failures = response['failures']
if len(failures) != 0:
print(failures)
return {
'statusCode': 500,
'body': json.dumps('NG!')
}
'''
return {
'statusCode': 200,
'body': json.dumps('OK!')
}
Solved
Thanks to @MarkB
I use update_service
to change the desiredCount
of Fargate
instead of run_task
and stop_task
These are my lambda code
import json
import boto3
import os
ecs = boto3.client('ecs')
def lambda_handler(event, context):
response = ecs.update_service(
cluster=os.environ['cluster'],
desiredCount=1,
service=os.environ['service'],
)
CodePudding user response:
Since you are running a load balanced application, you are not just running an ECS task, you are running an ECS service. When running an ECS service you should never be calling run_task
or stop_task
directly. You should be setting the desired count of the ECS service and letting the ECS service control the stopping/starting of tasks. It is the ECS service that keeps recreating the task after you stop it, not the load balancer. The ECS service also controls the registration/deregistration of the tasks with the load balancer.