-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(ecr): add lifecycle policy to clean up untagged images #6296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
|
+1 as workaround, following can be applied to sst.config.ts async run() {
new aws.ecr.LifecyclePolicy("sst-asset-lifecycle", {
repository: "sst-asset", //ECR name from SST bootstrap
policy: JSON.stringify({
"rules": [
{
"rulePriority": 1,
"description": "Expire untagged images pushed over 30 days ago",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 30
},
"action": {"type": "expire"}
}
]
}),
});
}, |
|
i'm wondering if this should be more explicit. something like: const vpc = new sst.aws.Vpc("MyVpc");
const cluster = new sst.aws.Cluster("MyCluster", { vpc });
new sst.aws.Service("MyService", {
cluster,
image: {
context: "./app",
dockerfile: "Dockerfile",
expiresIn: "30 days"
}
});what do you think? |
|
yes, this is much better. with enums like "x days", "on push", etc. |
|
exactly! @ekaya97 do any of you wanna give it a try? |
|
@vimtor I'll take care of it tonight! |
|
couple questions:
|
|
probably if we do it by service/task we should add a unique tag based on the component's name and create the lifecycle targeting it. maybe there's another way but that's the only one that comes to mind atm |
|
Sorry, things got busy for me so I didn't get around to it yet but wanted to contribute to the convo: My 2 cents are: SST/pulumi untags all images that are not in use by a task in a service. I opened this PR in the current setup for one 'sst-asset' ECR that is managed by SST and in this it seems that when pulumi deletes the task definition during an update it also automatically untags the image behind that definition (not 100% sure on that but to be confirmed). I am not sure if custom tags inside the service definition will persists between task definition updates. Happy to add it to the service definition instead of the bootstrap, but the tagging/targeting might become an issue with how pulumi works. SST already tags the image with the container name using the same 'tags' property a custom tag would use but it gets removed anyway. Hence I created the 'untagged' rule. If we wanted service based expiration, we may need to change how SST structures images under the hood. |
|
great analysis @mkilp i'm not sure what the best approach is here tbh i don't think it's worth changing how we tag images for a feature like this a simple fix could be adding an example to the docs using @ekaya97's snippet still, i like the PR change - everyone will need this, so it fits as a core feature. i dislike making it a default without any config, but i don't know where to put that config, which is probably telling on the other hand, most people rolling back will just what do you think? |
|
thanks @vimtor I did a more in depth trial on my AWS account and found the following:
This might actually cause huge issues if we expire all untagged images and you try to scale out Service A on Stage X due to the task definition not being able to find the image SHA that it contains. The only solution that I can currently find is changing the Let me know what you think about the |
|
i'm thinking that maybe the best approach is to provide an utility method like new aws.ecr.LifecyclePolicy("sst-asset-lifecycle", {
repository: sst.aws.getECRRepository().apply(repo => repo.name),
policy: JSON.stringify({
"rules": [
{
"rulePriority": 1,
"description": "Expire untagged images pushed over 30 days ago",
"selection": {
"tagStatus": "untagged",
"countType": "sinceImagePushed",
"countUnit": "days",
"countNumber": 30
},
"action": {"type": "expire"}
}
]
}),
}); |
Hi everyone,
I ran into a semi-big issue while developing my ECS service. My containers are pretty heavy (multiple gigs) and I did a sanity check inside ECR to make see what kind of storage I am looking at.
Low and behold I noticed there are dozens of images that never get deleted.
This PR adds a lifecycle policy to ECR that expires untagged images, which as far as I can tell are what we can safely remove.
I do forsee a problem with rollbacks since SST uses the digest to attache the image to the task.
I am happy to take input on the exact lifecycle policy to use, I understand this is a pretty big change since it adds to the bootstrap. I tested it and its working:
My change will essentially gives 30 days of rollback time. We could also change the rule to at least keep x number of untagged images. Note: We can only target untagged images with one rule.
I do believe this is very important since ECR storage is pretty expensive at 0.10$ per GB Month.
cheers,
marv