ECS API currently does not allow users restart running services. Workaround is update service desired task to 0 and later update back with original value. Another solutions, used in this Lambda, stops underlying tasks associated with service. This will force scheduler to run new tasks.
-
$ git clone https://github.com/s7anley/aws-ecs-service-stop-lambda.git
or download zip. -
Deploy to lambda function with your favorite method e.g. copy content of main.py to editor or upload zip to S3 bucket
-
Create custom Cloudwatch Event Rule with event defined as Constant (JSON text) and attach it to Lambda
{ "cluster": "mycluster", "service_name": "myservice" }
-
Configure lambda
- Runtime: Python
- Handler:
lambda_function.restart_service
- Set & attach
role
to lambda function - Minimum role policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:ListTasks",
"ecs:StopTask",
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
Maybe this Lambda will fits you out-of-box, but it's just generic template which shows, how you can restart ECS service. You should adjust the code according your expected event e.g. S3 bucket name.