[ECS] [request]: Container Lifecycle Hooks
Community Note
- Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
- Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
- If you are interested in working on this issue or have submitted a pull request, please leave a comment
Tell us about your request
ECS needs to be able to define in the task definition file (and maybe on the CLI and API layer) actions to be performed before SIGTERM is sent to a container, so that some housekeeping can be done before the container is terminated, or before a container is started up.
k8s contains similar Lifecycle hooks here and here.
I will expect something similar to this as is observable in k8s
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: nginx
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:
exec:
command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]
A sample task definition file in ECS should look somewhat like this:
{
"containerDefinitions": [
{
"command": [
"/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""
],
"entryPoint": [
"sh",
"-c"
],
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:
exec:
command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]
"essential": true,
"image": "httpd:2.4",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "/ecs/fargate-task-definition",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
},
"name": "sample-fargate-app",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
]
}
],
"cpu": "256",
"executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole",
"family": "fargate-task-definition",
"memory": "512",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
]
}
Which service(s) is this request for? This request is for ECS
Tell us about the problem you're trying to solve. What are you trying to do, and why is it hard?
I want to call a shutdown() method within a container that's running without a loadbalancer in ECS before it is terminated. I want the ecs-agent to look through the config for this container and see if there's a Lifecycle hook after startup and before shutdown and then execute it. It's tedious to try to run this in several containers while doing a rolling update and scheduling it appropriately
Are you currently working around this issue?
I am currently trapping SIGTERM and extending the time before SIGKILL is sent by the agent.
Additional context Nothing else
Attachments If you think you might have additional information that you'd like to include via an attachment, please do - we'll take a look. (Remember to remove any personally-identifiable information.)
I recommend covering all of the states currently covered by systemd service files. This will make for easy transition from services made for systemd to ECS. ExecStartPre, ExecStartPost, ExecStopPost
https://www.freedesktop.org/software/systemd/man/systemd.service.html
Just for future viewers:
- The current work around (trap SIGTERM and extending the time before SIGKILL is sent), has a relevant downside: the maximum time you can set before the SIGKILL is sent is 120 seconds. Plus, since this approach involves file and network I/O you have no guarantee that the operation can be completed successfully;
- Another alternative would be to attach a EFS as persistent volumes to your tasks and store the required logs there;
any plans to incorporate that ability?
Any updates on this? Have a use case around long running web sockets that it would be good to hook into the SIGTERM BEFORE the ALB terminates connections.
Hey team any update on this? Is there any way we can help and/or contribute?