- Newest
- Most votes
- Most comments
You can use this approach:
-
Use Amazon CloudWatch Events: Create a CloudWatch Event rule that triggers a Lambda function at regular intervals, such as once a day.
-
Create a Lambda function: The Lambda function will perform the following steps:
- Get a list of container instances in the ECS cluster using the
describeContainerInstances
API. - Iterate through the container instances and check their launch time.
- If the launch time of a container instance exceeds your desired maximum instance lifetime, call the
updateContainerInstancesState
API to set the container instance to DRAINING state, preventing new tasks from being scheduled on that instance. - Optionally, call the
terminateInstances
API to terminate instances that have been in the DRAINING state for a certain period of time.
- Get a list of container instances in the ECS cluster using the
-
Schedule the Lambda function: Use CloudWatch Events to trigger the Lambda function at regular intervals, for example, once a day. This ensures that container instances exceeding the maximum instance lifetime are drained and eventually replaced with instances running the latest versions of the ECS-Optimized AMIs.
By following this approach, you can enforce a maximum instance lifetime for your container instances in the ECS cluster and ensure that they are eventually replaced with instances running the latest versions of the ECS-Optimized AMIs.
Relevant content
- asked 5 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
I was hoping there was some feature built in to ECS so we wouldn't have to go through all this effort, but it looks like this is the best option for now. Thanks!