Skip to content

Commit 5301485

Browse files
authored
Empty us east 1 deployment bucket upon stack delete (#243)
* Delete objects in deployment bucket upon stack delete * Version bump for release
1 parent 352e8a1 commit 5301485

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

example-serverless-app-reuse/reuse-auth-only.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Parameters:
3232
SemanticVersion:
3333
Type: String
3434
Description: Semantic version of the back end
35-
Default: 2.1.6
35+
Default: 2.1.7
3636

3737
HttpHeaders:
3838
Type: String

example-serverless-app-reuse/reuse-complete-cdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const authAtEdge = new sam.CfnApplication(stack, "AuthorizationAtEdge", {
1919
location: {
2020
applicationId:
2121
"arn:aws:serverlessrepo:us-east-1:520945424137:applications/cloudfront-authorization-at-edge",
22-
semanticVersion: "2.1.6",
22+
semanticVersion: "2.1.7",
2323
},
2424
parameters: {
2525
EmailAddress: "[email protected]",

example-serverless-app-reuse/reuse-complete.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Resources:
1212
Properties:
1313
Location:
1414
ApplicationId: arn:aws:serverlessrepo:us-east-1:520945424137:applications/cloudfront-authorization-at-edge
15-
SemanticVersion: 2.1.6
15+
SemanticVersion: 2.1.7
1616
AlanTuring:
1717
Type: AWS::Cognito::UserPoolUser
1818
Properties:

example-serverless-app-reuse/reuse-with-existing-user-pool.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Resources:
7575
Properties:
7676
Location:
7777
ApplicationId: arn:aws:serverlessrepo:us-east-1:520945424137:applications/cloudfront-authorization-at-edge
78-
SemanticVersion: 2.1.6
78+
SemanticVersion: 2.1.7
7979
Parameters:
8080
UserPoolArn: !GetAtt UserPool.Arn
8181
UserPoolClientId: !Ref UserPoolClient

src/cfn-custom-resources/us-east-1-lambda-stack/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ async function ensureUsEast1LambdaStack(props: {
128128
.catch(() => ({ Stacks: undefined }));
129129
if (stacks?.length) {
130130
console.log("Deleting us-east-1 stack ...");
131+
const deploymentBucket = stacks[0].Outputs?.find(
132+
(output) => output.OutputKey === "DeploymentBucket"
133+
)?.OutputValue;
134+
if (deploymentBucket) {
135+
await emptyBucket({ bucket: deploymentBucket });
136+
}
131137
await CFN_CLIENT_US_EAST_1.deleteStack({
132138
StackName: props.stackName,
133139
}).promise();
@@ -412,6 +418,33 @@ async function copyLambdaCodeToUsEast1(props: {
412418
return props;
413419
}
414420

421+
async function emptyBucket(props: { bucket: string }) {
422+
const params: S3.ListObjectsV2Request = {
423+
Bucket: props.bucket,
424+
};
425+
do {
426+
console.log(`Listing objects in bucket ${props.bucket} ...`);
427+
const { Contents: s3objects, NextContinuationToken } =
428+
await S3_CLIENT_US_EAST_1.listObjectsV2(params).promise();
429+
430+
if (!s3objects?.length) break;
431+
console.log(`Deleting ${s3objects.length} S3 objects ...`);
432+
433+
const { Errors: errors } = await S3_CLIENT_US_EAST_1.deleteObjects({
434+
Bucket: props.bucket,
435+
Delete: {
436+
Objects: s3objects.filter((o) => !!o.Key).map((o) => ({ Key: o.Key! })),
437+
},
438+
}).promise();
439+
440+
if (errors?.length) {
441+
console.log("Failed to delete objects:", JSON.stringify(errors));
442+
}
443+
444+
params.ContinuationToken = NextContinuationToken;
445+
} while (params.ContinuationToken);
446+
}
447+
415448
export const handler: CloudFormationCustomResourceHandler = async (event) => {
416449
console.log(JSON.stringify(event, undefined, 4));
417450
const { StackId: stackId, RequestType: requestType } = event;

template.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Metadata:
2727
"amplify",
2828
]
2929
HomePageUrl: https://github.com/aws-samples/cloudfront-authorization-at-edge
30-
SemanticVersion: 2.1.6
30+
SemanticVersion: 2.1.7
3131
SourceCodeUrl: https://github.com/aws-samples/cloudfront-authorization-at-edge
3232

3333
Parameters:
@@ -150,7 +150,7 @@ Parameters:
150150
Version:
151151
Type: String
152152
Description: "Changing this parameter after initial deployment forces redeployment of Lambda@Edge functions"
153-
Default: "2.1.6"
153+
Default: "2.1.7"
154154
LogLevel:
155155
Type: String
156156
Description: "Use for development: setting to a value other than none turns on logging at that level. Warning! This will log sensitive data, use for development only"
@@ -423,6 +423,8 @@ Resources:
423423
- s3:PutObject
424424
- s3:CreateBucket
425425
- s3:DeleteBucket
426+
- s3:DeleteObject
427+
- s3:ListBucket
426428
Resource: !Sub "arn:${AWS::Partition}:s3:::*-authedgedeploymentbucket-*"
427429
- Effect: Allow
428430
Action: lambda:GetFunction

0 commit comments

Comments
 (0)