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?

질문됨 2년 전716회 조회
2개 답변
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
     ...
)
AWS
답변함 2년 전
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
  }
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠