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

(cfnInclude : (unable to include custom resources with reference to conditions that are part of main template )

Open mandar-git opened this issue 2 years ago • 2 comments

Describe the bug

I am trying to create a custom Resource from a similar json as below using CDK using cloudformation-include/CfnInclude.
The condition being referenced in my custom resource json is added using below in my cdk script.

 new core.CfnCondition(this, 'IsBucketEmpty', {
            expression: core.Fn.conditionEquals(core.Fn.ref('ImageId'), '')
        })

When I use the cfnInclude, it complains saying resource

CfnInclude(this, 'customResources', { templateFile: customResourcesFile ,  preserveLogicalIds: false  })

Error: Resource 'some-bucket' uses Condition 'IsImageIdEmpty' that doesn't exist at CfnParser.handleAttributes

//template.json

{
  "Resources": {
    "Bucket": {
      "Type": "AWS::S3::Bucket",
      "Condition": "IsBucketEmpty",
      "Properties": {
        "BucketName": "some-bucket"
      }
    }
  }
}

When I include below condition in template.json, I get an error saying Error: section 'Conditions' already contains 'IsBucketEmpty' at mergeObjectsWithoutDuplicates (node_modules\aws-cdk-lib\core\lib\stack.js:2:854)

 "Conditions": {
    "IsBucketEmpty": {
     "Fn::Equals": [
      {
       "Ref": "BucketName"
      },
      ""
     ]
    }
   },

Expected Behavior

Expected for custom resource to get created similar to the way cfnInclude was working in @aws-cdk/core (earlier version)

Current Behavior

cfnInclude failed with error saying resource contains condition that does not exist.

Error: Resource 'Bucket' uses Condition 'IsBucketEmpty' that doesn't exist
    at CfnParser.handleAttributes (..\node_modules\aws-cdk-lib\core\lib\helpers-internal\cfn-parse.js:1:4828)    
    at Function._fromCloudFormation (..\node_modules\aws-cdk-lib\aws-s3\lib\s3.generated.js:1:22070)
    at CfnInclude.getOrCreateResource (..\node_modules\aws-cdk-lib\cloudformation-include\lib\cfn-include.js:1:12233)
    at new CfnInclude (C:\opt\newapp\node_modules\aws-cdk-lib\cloudformation-include\lib\cfn-include.js:1:1909)

Reproduction Steps

mkdir my-project cd my-project cdk init app --language typescript extract files from below zip in my-project directory, compile and run the sample typescript code using below

npm install tsc && ts-node .\app.ts

app.zip

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.33.0

Framework Version

No response

Node.js Version

14.16.1

OS

linux

Language

Typescript

Language Version

typescript 3.9.10

Other information

aws-cdk-lib:2.33.0 constructs: 10.1.57

mandar-git avatar Jul 27 '22 09:07 mandar-git

As an alternate solution, I was able to create the custom resources from template file using cfnResource. As this ignores the conditions in the templateFile during resource creation, I added the cfnConditions to cnfResource.cnfOptions by parsing the templateFile for referenced conditions and assigning those matching ones found in my current cdk stack/app

mandar-git avatar Jul 28 '22 13:07 mandar-git

@mandar-git could you please include the exact template file you're including in your app?

comcalvi avatar Sep 22 '22 18:09 comcalvi

Feel free to open another issue/discussion if you are still experiencing this. Thanks!

peterwoodworth avatar Dec 05 '22 23:12 peterwoodworth

⚠️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.

github-actions[bot] avatar Dec 05 '22 23:12 github-actions[bot]