aws / aws/containers-roadmap

[ECS] [request]: Container Lifecycle Hooks

Open
#952 5 comments 231 reactions 1 assignee Assigned to @radsn View on GitHub
ECS Fargate Under consideration
Dominant language
Shell
Stars
5.4k
Forks
334
PR merge metrics
No merged PRs in 30d

Description

### Community Note

* Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) 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](https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/) and [here](https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/).

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 ' Amazon ECS Sample App body {margin-top: 40px; background-color: #333;}

Amazon ECS Sample App

Congratulations!

Your application is now running on a container in Amazon ECS.

' > /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.)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.