How do you set a lifecycle policy on CDK Pipeline input?

1

We have a CDK (v2) Pipeline that listens for changes to our repo. Everytime someone merges a change, it kicks off the Pipeline. The Pipeline copies the entire repo to S3 everytime this happens. I don't see an option from the CDK to set a life cycle policy for the Pipeline synth input. Is it possible to set up a lifecycle policy to delete old inputs?

asked a year ago130 views
2 Answers
1

I assume you are attempting to modify the lifecycle policy of the underlying S3 Artifacts bucket of your CDK pipeline. At the time of writing, there is no direct way to do this.

The workaround is to pass in an existing codePipeline as an input to your CDK pipeline. In turn, you can pass in an artifactBucket to this CodePipeline. Since you would be creating your own S3 bucket in this case, you would have full control over every aspect of it, including its lifecycle policy.

In Python for example:

artifact_bucket = Bucket(
    self,
    "code-pipeline-bucket",
) # Since you would be managing the bucket, you can set its properties like the lifecycle policy

code_pipeline = Pipeline(
    self,
    "code-pipeline",
    artifact_bucket=artifact_bucket,  # Pass in the Bucket
    ...
)

cdk_pipeline = CodePipeline(
     self,
     "cdk-pipeline",
     code_pipeline=code_pipeline,  # Pass in the CodePipeline
     ...
)
answered a year ago
0

Thank you! This worked.

For anyone reading this in the future, this is what I did:

class Pipeline extends cdk.Stack {
constructor(scope: Construct){
    super(scope, id, props)
    const pipeline = new pipelines.CodePipeline(this, 'App', {
      codePipeline: this.buildPipeline(),
      ...Other config    
  })
}
  private buildPipeline(): codepipeline.Pipeline {
    const artifactBucket = new s3.Bucket(this, 'ArtifactBucket')

    /**
     * https://github.com/aws/aws-cdk/blob/0ed661df0b060d0ec4c502b92511b3e777144854/packages/%40aws-cdk/pipelines/lib/codepipeline/codepipeline.ts#L348
     */
    const pipeline = new codepipeline.Pipeline(this, 'Pipeline', {
      /**
       * This is necessary to make self-mutation work
       * (deployments are guaranteed to happen only
       * after the builds of the latest pipeline
       * definition).
       */
      restartExecutionOnUpdate: true,
      artifactBucket,
    })

    return pipeline
  }
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions