-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Auto delete images upon ECR repo removal - CDK v2.70.0 not working #24822
Comments
I can successfully destroy it with cdk 2.70.0 and it works pretty well in my account. Are you using CDK in TypeScript or Java? Your code snippets seems to be TypeScript but you specified |
The issue i encounter is in Java and I have not tested it in ts
|
I can destroy it with CDK in Java. This is the Java code I used: package com.myorg;
import software.constructs.Construct;
import software.amazon.awscdk.Stack;
import software.amazon.awscdk.StackProps;
import software.amazon.awscdk.RemovalPolicy;
import software.amazon.awscdk.services.ecr.Repository;
public class EcrJavaStack extends Stack {
public EcrJavaStack(final Construct scope, final String id) {
this(scope, id, null);
}
public EcrJavaStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);
Repository ecrRepo = Repository.Builder
.create(this, "DemoRepo")
.repositoryName("test-app")
.imageScanOnPush(false)
.removalPolicy(RemovalPolicy.DESTROY)
.autoDeleteImages(true)
.build();
}
} |
@pahud Thank you for taking the time to test. I believe the issue arises when you put a multi-architecture image into a repo that has an An Example of building multi arch using docker buildx plugin
My Dockerfile (BusyBox multi-arch) from this file
|
@pahud Are you able to reproduce this(Unable to delete images when ECR contains multi-arch images and index)? |
@pahud You can reproduce this issue by the following CDK code. Prerequisite
CDK codeimport { Stack, StackProps, RemovalPolicy, CfnOutput } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import {
aws_ecr as ecr,
} from 'aws-cdk-lib';
export class EcrDebugStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const multiArchRepo = new ecr.Repository(this, "MultiArchitectureRepository", {
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteImages: true,
});
const stack = Stack.of(this);
const region = stack.region;
const accountId = stack.account;
new CfnOutput(this, 'DockerLoginCommand', {
value: `aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${accountId}.dkr.ecr.${region}.amazonaws.com`
});
new CfnOutput(this, 'DockerBuildxPushCommand', {
value: `docker buildx build --platform linux/arm64,linux/amd64 --tag ${multiArchRepo.repositoryUri}:latest --push .`
});
}
} Reproduction stepsOnce you deploy the stack, run Why the error occurs?If manifest list is tagged and points to a manifest then the manifest list is not deleted before the manifest list. When you delete CloudFormation stack How to fix this?Fix auto-delete-images-handler as code below. async function emptyRepository(params: ECR.ListImagesRequest) {
const listedImages = await ecr.listImages(params).promise();
const imageIds = listedImages?.imageIds ?? [];
// retrieve tagged images
const imageIdsTagged = imageIds.filter((imageId) => 'imageTag' in imageId);
const nextToken = listedImages.nextToken ?? null;
if (imageIds.length === 0) {
return;
}
// delete tagged images first
await ecr.batchDeleteImage({
repositoryName: params.repositoryName,
imageIds: imageIdsTagged,
}).promise();
await ecr.batchDeleteImage({
repositoryName: params.repositoryName,
imageIds: imageIds,
}).promise();
if (nextToken) {
await emptyRepository({
...params,
nextToken,
});
}
} |
Thank you @hkford I can reproduce this now and I saw the error message as:
Yes I believe we should fix the handler to batch delete all images. Thank you for your PR and feel free to let me know if you need any further help. |
…st (#25789) Fixes #24822 As I commented on #24822 (comment), auto delete container images in ECR repository fails when it has container manifest list. I fix custom resource Lambda function to delete tagged images first. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
|
Describe the bug
Tested this much awaited feature in CDKv2.70.0 and it is still not working as expected.
Error
ecr: add option to auto delete images upon ECR repository removal (#24572) (7de5b00), closes #15932 #12618 #15932
Expected Behavior
Delete of ECR repo with images should succeed if removalPolicy "Destroy" and autoDelete "true"
Current Behavior
Error
Reproduction Steps
Create a ECR repo similar to below using
cdk deploy
and add images to it. Afterwards, executecdk destroy
which fails with exception mentioned abovePossible Solution
No response
Additional Information/Context
No response
CDK CLI Version
CDK v2.70.0
Framework Version
No response
Node.js Version
v19.8.0
OS
mac-os M1
Language
Java
Language Version
JDK 17
Other information
No response
The text was updated successfully, but these errors were encountered: