aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[@aws-cdk/aws-sam] contentUri for CfnLayerVersion cause error while creating changeset when using a ref value

Open atakidev opened this issue 6 years ago • 1 comments

contentUri for CfnLayerVersion cannot use a ref value to point S3 URI. If contentUri is not a literal string, then cdk deploy failed because of transform failure like the following one:

Error: Failed to create ChangeSet CDK-08d25eae-8b17-4537-8cf0-96fe8b6c5c8c on CdkIssueSamLayerStack: FAILED, Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [CDKTestLayerc332cdb064] is invalid. 'ContentUri' requires Bucket and Key properties to be specified.

Reproduction Steps

  1. Setup CDK
$ mkdir cdk-issue-sam-layer
$ cd cdk-issue-sam-layer
$ cdk init --language javascript
$ npm install @aws-cdk/aws-s3-assets @aws-cdk/aws-sam path
$ mkdir dummy
  1. Change lib/cdk-issue-sam-layer-stack.js like this:
const cdk = require('@aws-cdk/core');
const sam = require('@aws-cdk/aws-sam');
const assets = require('@aws-cdk/aws-s3-assets');
const path = require('path');

class CdkIssueSamLayerStack extends cdk.Stack {
  /**
   *
   * @param {cdk.Construct} scope
   * @param {string} id
   * @param {cdk.StackProps=} props
   */
  constructor(scope, id, props) {
    super(scope, id, props);
    const asset = new assets.Asset(this, 'LayerAsset',
	       {path: path.join(__dirname, '../dummy'),}
       );

    const layer = new sam.CfnLayerVersion(this, 'CDKTestLayer', {
         compatibleRuntimes: ['python3.8'],
         contentUri: asset.s3ObjectUrl
    });
  }
}
  1. Check generated CloudFormation template.
$ cdk synth

You will see like the following Fn::Join for ContentUri.

Resources:
  CDKTestLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      CompatibleRuntimes:
        - python3.8
      ContentUri:
        Fn::Join:
          - ""
          - - s3://
            - Ref: AssetParameters1ebc9d3ac2033816c4abb63e4afd69d350b4aba8704cc9236b82ea520b74f4b0S3Bucket5EA66AEF
            - /
            - Fn::Select:
                - 0
                - Fn::Split:
                    - "||"
                    - Ref: AssetParameters1ebc9d3ac2033816c4abb63e4afd69d350b4aba8704cc9236b82ea520b74f4b0S3VersionKeyD171B821
            - Fn::Select:
                - 1
                - Fn::Split:
                    - "||"
                    - Ref: AssetParameters1ebc9d3ac2033816c4abb63e4afd69d350b4aba8704cc9236b82ea520b74f4b0S3VersionKeyD171B821

What did you expect to happen?

I expect contentUri for CfnLayerVersion should be able to handle a ref value for S3 bucket/key.

What actually happened?

If the template generated like the above template, then it fails to cdk deploy because of the following error.

$ cdk deploy
CdkIssueSamLayerStack: deploying...
[0%] start: Publishing 1ebc9d3ac2033816c4abb63e4afd69d350b4aba8704cc9236b82ea520b74f4b0:current
[100%] success: Published 1ebc9d3ac2033816c4abb63e4afd69d350b4aba8704cc9236b82ea520b74f4b0:current
CdkIssueSamLayerStack: creating CloudFormation changeset...

 ❌  CdkIssueSamLayerStack failed: Error: Failed to create ChangeSet CDK-08d25eae-8b17-4537-8cf0-96fe8b6c5c8c on CdkIssueSamLayerStack: FAILED, Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [CDKTestLayerc332cdb064] is invalid. 'ContentUri' requires Bucket and Key properties to be specified.
    at /home/ec2-user/cdk/node_modules/aws-cdk/lib/api/util/cloudformation.ts:220:11
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at waitFor (/home/ec2-user/cdk/node_modules/aws-cdk/lib/api/util/cloudformation.ts:181:20)
    at Object.waitForChangeSet (/home/ec2-user/cdk/node_modules/aws-cdk/lib/api/util/cloudformation.ts:206:15)
    at Object.deployStack (/home/ec2-user/cdk/node_modules/aws-cdk/lib/api/deploy-stack.ts:237:32)
    at CdkToolkit.deploy (/home/ec2-user/cdk/node_modules/aws-cdk/lib/cdk-toolkit.ts:181:24)
    at main (/home/ec2-user/cdk/node_modules/aws-cdk/bin/cdk.ts:268:16)
    at initCommandLine (/home/ec2-user/cdk/node_modules/aws-cdk/bin/cdk.ts:188:9)
Failed to create ChangeSet CDK-08d25eae-8b17-4537-8cf0-96fe8b6c5c8c on CdkIssueSamLayerStack: FAILED, Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [CDKTestLayerc332cdb064] is invalid. 'ContentUri' requires Bucket and Key properties to be specified.

Environment

  • CLI Version :1.59.0
  • Framework Version:1.59.0
  • Node.js Version:v14.7.0
  • OS :Amazon Linux 2
  • Language (Version):JavaScript

Other

I think the reason of the failure is described in the following issue.

  • Serverless Transform failing when using Intrinsic function within value for "ContentUri" property for "AWS::Serverless::LayerVersion" resource https://github.com/awslabs/serverless-application-model/issues/1159

Based on the above issue, I think the generated template should use not Fn::Join but the object of Bucket and Key.

Please let me know if you have any questions.


This is :bug: Bug Report

atakidev avatar Aug 16 '20 08:08 atakidev

Unfortunately, we don't have support for assets in our SAM APIs. Please use the APIs from the @aws-cdk/aws-lambda module that has support for assets and an overall better CDK experience.

nija-at avatar Aug 17 '20 09:08 nija-at