Home > Blockchain >  CDK's CodePipeline or CodeBuildStep are leaving an S3 bucket behind, is there a way of automati
CDK's CodePipeline or CodeBuildStep are leaving an S3 bucket behind, is there a way of automati

Time:02-04

I'm creating a CloudFormation stack using CDK with code roughly like this:

export class PipelineStack extends cdk.Stack {
    constructor(scope: Construct, id: string, props: PipelineStackProps) {
        super(scope, id, props)
        new pipelines.CodePipeline(this, id, {
            pipelineName: id,
            synth: new pipelines.CodeBuildStep("Synth", {
                    input: pipelines.CodePipelineSource.connection(props.githubRepo, props.repoBranch, {connectionArn: props.repoConnectionArn}),
                    installCommands: ["npm install -g aws-cdk"],
                    commands: ["npm ci", "npx cdk synth"],
                },
            ),
        })
    }
}

and that stack creates an S3 bucket that when I delete the stack isn't deleted with it. I searched for options and I couldn't find any to control that behaviour. Is it possible to mark that S3 bucket as cdk.RemovalPolicy.DESTROY or something like that?

CodePudding user response:

I'm assuming you're referring to the artifacts bucket. It can be accessed via the CodePipeline.pipeline.artifactBucket

const artifactBucket = myPipeline.pipeline.artifactBucket;
artifactBucket.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);

Documentation:

Although this wouldn't solve the issue of the bucket being non-empty and failing to delete. To solve this properly, you need to create your own Pipeline with a bucket you create yourself and pass it to CodePipeline's constructor in the codePipeline property:

const artifactBucket = new s3.Bucket(this, 'artifactBucket', {autoDeleteObjects: true});
artifactBucket.applyRemovalPolicy(cdk.RemovalPolicy.DESTROY);

const underlyingPipeline = aws_codepipeline.Pipeline(this, 'underlyingPipeline', {artifactBucket: artifactBucket});

const pipeline = pipelines.Codepipeline(this, 'myPipeline', {codePipeline:underlyingPipeline, ...});
  •  Tags:  
  • Related