cdklabs/aws-delivlib

`autoBuildOptions` creates bucket for build logs that cannot be deleted

webdog opened this issue · 2 comments

When creating a Pipeline, the construct uses autoBuildOptions to determine whether or not to create an S3 Bucket for CodeBuild logs via the publicLogs optional property:

export interface AutoBuildOptions {
/**
* Build environment.
* @default - see defaults in `BuildEnvironmentProps`
*/
readonly environment?: BuildEnvironmentProps;
/**
* The name of the CodeBuild project.
*
* @default - a name will be generated by CloudFormation.
*/
readonly projectName?: string;
/**
* Make build logs public and publishes a link to GitHub PR discussion.
*
* @see https://github.com/jlhood/github-codebuild-logs
*
* @default false
*/
readonly publicLogs?: boolean;
/**
* Configure the project to respond to webhooks.
*
* @default true
*/
readonly webhook?: boolean;
/**
* Whether to publish a link to build logs when build is successful.
*
* @see https://github.com/jlhood/github-codebuild-logs#app-parameters
*
* @default true
*/
readonly publicLogsOnSuccess?: boolean;
/**
* Whether to delete previously published links to build logs
* before posting a new one.
*
* @see https://github.com/jlhood/github-codebuild-logs#app-parameters
*
* @default true
*/
readonly deletePreviousPublicLogsLinks?: boolean;
/* tslint:disable:max-line-length */
/**
* Build spec file to use for AutoBuild
*
* @default @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-codebuild-project-source.html#cfn-codebuild-project-source-buildspec
*/
readonly buildSpec?: codebuild.BuildSpec;
/* tslint:enable:max-line-length */

When publicLogs is set to true, the Pipeline then uses aws-sam.serverless.CfnApplication to create a bucket, which is a SAM application managed in this repositoy: https://github.com/jlhood/github-codebuild-logs by @jlhood

if (publicLogs) {
new serverless.CfnApplication(this, 'GitHubCodeBuildLogsSAR', {
location: {
applicationId: 'arn:aws:serverlessrepo:us-east-1:277187709615:applications/github-codebuild-logs',
semanticVersion: '1.4.0',
},
parameters: {
CodeBuildProjectName: this.project.projectName,
DeletePreviousComments: (props.deletePreviousPublicLogsLinks ?? true).toString(),
CommentOnSuccess: (props.publicLogsOnSuccess ?? true).toString(),
...githubToken ? { GitHubOAuthToken: Token.asString(githubToken) } : undefined,
},
});
}
}

Once deployed by the SAM, the resource contains create logic, but not destroy logic AFAICT:

https://github.com/jlhood/github-codebuild-logs/blob/86f8fd60892c00fea1ddbefd741ae35292707e0a/src/build.py#L15

If I run cdk destroy on the Pipeline stack, and CodeBuild has run to a point where it has successfully generated logs, the stack deletion will return DELETE_FAILED because of this behavior (It is standard behavior to have buckets being unable to be deleted when there are objects present).

image

❯ cdk destroy                                                                                                                                                                                                                                                                                                             
Are you sure you want to delete: PipelineStack (y/n)? y
PipelineStack: destroying...
2:31:54 PM | DELETE_FAILED        | AWS::CloudFormation::Stack          | PipelineAutoBuildG...ildLogsSAR914A0D32
Embedded stack arn:aws:cloudformation:us-east-1::stack/PipelineStack-PipelineAutoBuildGitHubCodeBuildLogsSAR was not successfully deleted: The following resource(s) failed to delete: [BuildLogs].

        new AutoBuild (/pipeline/node_modules/aws-delivlib/lib/auto-build.ts:118:7)
        \_ Pipeline.autoBuild (/pipeline/node_modules/aws-delivlib/lib/pipeline.ts:464:12)
        \_ new Pipeline (/pipeline/node_modules/aws-delivlib/lib/pipeline.ts:277:36)
        \_ new PipelineStack (/pipeline/lib/pipeline-stack.ts:55:22)
        \_ Object.<anonymous> (/pipeline/bin/pipeline.ts:7:1)
2:31:54 PM | DELETE_FAILED        | AWS::CloudFormation::Stack          | Pipeline/AutoBuild...ubCodeBuildLogsSAR
Embedded stack arn:aws:cloudformation:us-east-1::stack/PipelineStack-PipelineAutoBuildGitHubCodeBuildLogsSAR was not successfully deleted: The following resource(s) failed to delete: [BuildLogs].
2:32:24 PM | DELETE_FAILED        | AWS::CloudFormation::Stack          | PipelineStack
The following resource(s) failed to delete: [PipelineAutoBuildGitHubCodeBuildLogsSAR].

 ❌  PipelineStack: destroy failed Error: The stack named PipelineStack is in a failed state. You may need to delete it from the AWS console : DELETE_FAILED (The following resource(s) failed to delete: [PipelineAutoBuildGitHubCodeBuildLogsSAR914A0D32]. )
    at Object.waitForStackDelete (/usr/local/lib/node_modules/aws-cdk/lib/api/util/cloudformation.ts:277:11)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Object.destroyStack (/usr/local/lib/node_modules/aws-cdk/lib/api/deploy-stack.ts:390:28)
    at CdkToolkit.destroy (/usr/local/lib/node_modules/aws-cdk/lib/cdk-toolkit.ts:253:9)
    at initCommandLine (/usr/local/lib/node_modules/aws-cdk/bin/cdk.ts:210:9)
The stack named PipelineStack is in a failed state. You may need to delete it from the AWS console : DELETE_FAILED (The following resource(s) failed to delete: [PipelineAutoBuildGitHubCodeBuildLogsSAR]. )

In the S3 Construct, autoDeleteObjects? is available in s3.Bucket which deletes objects from a bucket when the stack is deleted, as of Dec 2020 from the Pull Request merged by @jogold

Is there an opportunity to move off this external dependency and utilize the s3 Bucket construct? I am raising my if a PR were to be accepted 😺

This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon.

Closing this issue as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen.