Skip to content

Commit 80d05cd

Browse files
authored
chore: Easier way to delete snapshots for old AMIs (#728)
1 parent 21165ab commit 80d05cd

File tree

3 files changed

+8
-21
lines changed

3 files changed

+8
-21
lines changed

src/image-builders/aws-image-builder/delete-resources.lambda.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DeleteSnapshotCommand, DeregisterImageCommand, DescribeImagesCommand, EC2Client } from '@aws-sdk/client-ec2';
1+
import { DeregisterImageCommand, DescribeImagesCommand, EC2Client } from '@aws-sdk/client-ec2';
22
import { BatchDeleteImageCommand, ECRClient } from '@aws-sdk/client-ecr';
33
import { DeleteImageCommand, ImagebuilderClient, ListImageBuildVersionsCommand, ListImageBuildVersionsResponse } from '@aws-sdk/client-imagebuilder';
44
import * as AWSLambda from 'aws-lambda';
@@ -69,21 +69,8 @@ async function deleteResources(props: DeleteResourcesProps) {
6969

7070
await ec2.send(new DeregisterImageCommand({
7171
ImageId: imageId,
72+
DeleteAssociatedSnapshots: true,
7273
}));
73-
74-
for (const blockMapping of imageDesc.Images[0].BlockDeviceMappings ?? []) {
75-
if (blockMapping.Ebs?.SnapshotId) {
76-
console.log({
77-
notice: 'Deleting EBS snapshot',
78-
image: imageId,
79-
snapshot: blockMapping.Ebs.SnapshotId,
80-
});
81-
82-
await ec2.send(new DeleteSnapshotCommand({
83-
SnapshotId: blockMapping.Ebs.SnapshotId,
84-
}));
85-
}
86-
}
8774
} catch (e) {
8875
console.warn({
8976
notice: 'Failed to delete AMI',

test/default.integ.snapshot/github-runners-test.assets.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@
4040
}
4141
}
4242
},
43-
"bfba719fe2a2764d7716a472198f4f8cbe46f6ea4616cd6ba0ebf26e75954814": {
43+
"b97c8701d090be54cf447e094fd55ece7c99b860476949ee5db7ceca6eeb2f6a": {
4444
"source": {
45-
"path": "asset.bfba719fe2a2764d7716a472198f4f8cbe46f6ea4616cd6ba0ebf26e75954814.lambda",
45+
"path": "asset.b97c8701d090be54cf447e094fd55ece7c99b860476949ee5db7ceca6eeb2f6a.lambda",
4646
"packaging": "zip"
4747
},
4848
"destinations": {
4949
"current_account-current_region": {
5050
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
51-
"objectKey": "bfba719fe2a2764d7716a472198f4f8cbe46f6ea4616cd6ba0ebf26e75954814.zip",
51+
"objectKey": "b97c8701d090be54cf447e094fd55ece7c99b860476949ee5db7ceca6eeb2f6a.zip",
5252
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
5353
}
5454
}
@@ -222,15 +222,15 @@
222222
}
223223
}
224224
},
225-
"e0e02d98917bef18a73321c2a05d5f726f5ce7d09a00013fd496365b722592e8": {
225+
"3b80f113bf1080dd8be08e53a2fe41d076fb71707d73d9bec289feb062840add": {
226226
"source": {
227227
"path": "github-runners-test.template.json",
228228
"packaging": "file"
229229
},
230230
"destinations": {
231231
"current_account-current_region": {
232232
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
233-
"objectKey": "e0e02d98917bef18a73321c2a05d5f726f5ce7d09a00013fd496365b722592e8.json",
233+
"objectKey": "3b80f113bf1080dd8be08e53a2fe41d076fb71707d73d9bec289feb062840add.json",
234234
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
235235
}
236236
}

test/default.integ.snapshot/github-runners-test.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10358,7 +10358,7 @@
1035810358
"S3Bucket": {
1035910359
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
1036010360
},
10361-
"S3Key": "bfba719fe2a2764d7716a472198f4f8cbe46f6ea4616cd6ba0ebf26e75954814.zip"
10361+
"S3Key": "b97c8701d090be54cf447e094fd55ece7c99b860476949ee5db7ceca6eeb2f6a.zip"
1036210362
},
1036310363
"Description": "Custom resource handler that deletes resources of old versions of EC2 Image Builder images",
1036410364
"Environment": {

0 commit comments

Comments
 (0)