aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.59k stars 3.89k forks source link

CodePipeline: Bucket at 'CacheBucket' should be created in the scope of a Stack, but no Stack found #21693

Closed jglesner closed 2 years ago

jglesner commented 2 years ago

Describe the bug

Trying to use the 'cache' property of CodeBuildOptions with 'codeBuildDefaults' property of 'CodePipelineProps'. In the linked documentation, specifically for Cache, this looks like a valid implementation.


  const myCachingBucket = new s3.Bucket(scope, "CacheBucket", {
      blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
      encryption: s3.BucketEncryption.S3_MANAGED,
      enforceSSL: true,
      versioned: true,
      removalPolicy: cdk.RemovalPolicy.RETAIN,
  });

  const pipeline = new CodePipeline(this, "Pipeline", {
      pipelineName: "ServerlessPipeline",
      crossAccountKeys: true,
      dockerEnabledForSelfMutation: true,
      dockerEnabledForSynth: true,
      synth: new ShellStep("Synth", {
        input: CodePipelineSource.codeCommit(pipelineRepo, "master"),
        commands: ["npm ci", "npm run build", "npx cdk synth"],
      }),
      codeBuildDefaults: {
        cache: codebuild.Cache.bucket(myCachingBucket),
        buildEnvironment: {
          privileged: true,
          computeType: codebuild.ComputeType.MEDIUM,
      },
    },
  });

Receiving the below CDK error, running AWS CDK 2.38.1 when trying to create the bucket for caching

Bucket at 'CacheBucket' should be created in the scope of a Stack, but no Stack found 

Assume this mean I can't setup 'cache' when initially instantiating the CodePipeline, and I have to add it after setting up the bucket in a stack? But that seems odd / kludgy in some respects. Is this intended behavior?

Expected Behavior

Given that 'cache' is an option for CodeBuildOptions, I would expect cache to be configurable with an S3 bucket when first defining the CodePipeline.

Current Behavior

It fails as shown in the included error message.

Reproduction Steps

As shown in the included 'CodePipeline' setup.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.38.1

Framework Version

2.38.1

Node.js Version

14.17.0

OS

OS X Monterey 12.1

Language

Typescript

Language Version

3.9.7

Other information

No response

jglesner commented 2 years ago

Appears as though if you change the context to 'this' when creating the bucket -e.g. new s3.Bucket(this, "CacheBucket") instead of using 'scope', it works as expected. Full example like so:

const myCachingBucket = new s3.Bucket(this, "CacheBucket", {
      blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
      encryption: s3.BucketEncryption.S3_MANAGED,
      enforceSSL: true,
      versioned: true,
      removalPolicy: cdk.RemovalPolicy.RETAIN,
  });

const pipeline = new CodePipeline(this, "Pipeline", {
      pipelineName: "ServerlessPipeline",
      crossAccountKeys: true,
      dockerEnabledForSelfMutation: true,
      dockerEnabledForSynth: true,
      synth: new ShellStep("Synth", {
        input: CodePipelineSource.codeCommit(pipelineRepo, "master"),
        commands: ["npm ci", "npm run build", "npx cdk synth"],
      }),
      codeBuildDefaults: {
        cache: codebuild.Cache.bucket(myCachingBucket),
        buildEnvironment: {
          privileged: true,
          computeType: codebuild.ComputeType.MEDIUM,
      },
    },
  });
github-actions[bot] commented 2 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.