mustbealennox
mustbealennox

Reputation: 147

AWS CDK asset path is incorrect

On September 6, I ran a build using CodePipeline. It generates a CloudFormation template for a project's stack using CDK. The stack has assets (a Lambda Layer), and the assets are correctly placed in the cdk.out folder. This can be seen in the CloudFormation template:

      "Metadata": {
        "aws:cdk:path": "MyStack/MyLayer/Resource",
        "aws:asset:path": "asset.ccb8fd8b4259a8f517879d7aaa083679461d02b9d60bfd12725857d23567b70f",
        "aws:asset:property": "Content"
      }

Starting yesterday, builds were failing with "Uploaded file must be a non-empty zip". When I investigated further, I noticed that the template was no longer correct. It has the asset path set to the source code of the Lambda instead:

      "Metadata": {
        "aws:cdk:path": "MyStack/MyLayer/Resource",
        "aws:asset:path": "/codebuild/output/src216693626/src/src/lambdas/layers",
        "aws:asset:property": "Content"
      }

When I build, I've added additional commands to the buildspec file which shows that the assets.abcdef folder has the layer and its dependencies, while the src folder does not. Yet the template is now different.

No code was changed in this time period, and I've tried both CDK version 1.105.0 and 1.119.0.

This code declares the Layer:

    new lambdapython.PythonLayerVersion(this.stack, 'MyLayer', {
      entry: path.join(__dirname, '../../src/lambdas/layers'),
      description: 'Common utilities for the Lambdas',
      compatibleRuntimes: [lambda.Runtime.PYTHON_3_8],
      layerVersionName: `${Aws.STACK_NAME}Utils`,
    });

Is there a known way for me to force the stack to use the assets in the cdk.out folder? Has something changed in the last couple of days with respect to how CDK generates the template's asset path?

Upvotes: 0

Views: 2166

Answers (2)

atlas_scoffed
atlas_scoffed

Reputation: 4127

This can also happen if you do not specify the --no-staging when running cdk synch command. Specifying no-staging will reference the original code.

Some details here: https://github.com/aws/aws-cdk/issues/10367

Example command:

cdk synth --no-staging --context deployment_env=dev > template.yml

Upvotes: 0

mustbealennox
mustbealennox

Reputation: 147

It turns out that I had added a cdk ls to print out additional debugging information while troubleshooting another problem. That command re-synthesized the stack, but with the incorrect asset path.

          build: {
            commands: [
              'cd ' + config.cdkDir,
              'cdk synth',
              'cdk ls --long'
            ]
          }

The solution was to delete the cdk ls --long from the buildspec definition.

Upvotes: 2

Related Questions