Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CfnLoadBalancer: CfnInclude broken for conditional tags #30740

Closed
KyleBS opened this issue Jul 3, 2024 · 3 comments · Fixed by #31578
Closed

CfnLoadBalancer: CfnInclude broken for conditional tags #30740

KyleBS opened this issue Jul 3, 2024 · 3 comments · Fixed by #31578
Assignees
Labels
@aws-cdk/cloudformation-include Issues related to the "CFN include v.20" package @aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort p1

Comments

@KyleBS
Copy link

KyleBS commented Jul 3, 2024

Describe the bug

When you attempt to CfnInclude a AWS::ElasticLoadBalancingV2::LoadBalancer resource with valid conditional tags CDK errors out

Expected Behavior

CfnInclude should succeed

Current Behavior

An error is thrown:

CfnSynthesisError: Resolution error: Supplied properties not correct for "CfnLoadBalancerProps"                                                                                                                                                                                              
  tags: element 1: {} should have a 'key' and a 'value' property. 

Reproduction Steps

Create a stack and include the following:

const template = `{                                                                                                                         
  "AWSTemplateFormatVersion": "2010-09-09",                             
  "Parameters": {                                                      
    "IsExtraTag": {                                                       
      "Type": "String",            
      "AllowedValues": [                                                  
        "true",                                                           
        "false"                             
      ],                                                                                                                                            
      "Default": "false"                                                                                                                                                                                                                                                                                 
    }                                                                                                                                                                                                                                                                                                    
  },                                                                                                                                                
  "Conditions": {                        
    "AddExtraTag": {                                                 
      "Fn::Equals": [                                                                                                                               
        {                                                                                                                                           
          "data": "IsExtraTag",                                  
          "type": "Ref",                                 
          "isCfnFunction": true                                                                                                                     
        },                                                             
        "true"                                                                                                                                      
      ]                                                        
    }                                                                                                                                               
  },                                                                                                                                                
  "Resources": {                                                                                                                                    
    "MyLoadBalancer": {                                                
      "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",                                                                                          
      "Properties": {                                        
        "Name": "my-load-balancer",                                                                                                                 
        "Subnets": [                                                                                                                                
          "subnet-0123456789abcdef0"                                                                                                                                                                                                                                                                     
        ],                                                                                                                                          
        "SecurityGroups": [                                               
          "sg-0123456789abcdef0"                                                                                                                    
        ],                                                                
        "Scheme": "internet-facing",                                                                                                                
        "LoadBalancerAttributes": [                                       
          {                                                                                                                                         
            "Key": "idle_timeout.timeout_seconds",                      
            "Value": "60"                                                                                                                           
          }                                                               
        ],                         
        "IpAddressType": "ipv4",                                          
        "Tags": [                                                                                                                                   
          {                                 
            "Key": "Name",                                                                                                                          
            "Value": "MyLoadBalancer"                                                                                                                                                                                                                                                                    
          },                                                                                                                                                                                                                                                                                             
          {                                                                                                                                         
            "data": [                          
              "IsExtraTag",                                          
              {                                                                                                                                     
                "Key": "Name2",                                                                                                                     
                "Value": "MyLoadBalancer2"                       
              },                                         
              {                                                                                                                                     
                "data": "AWS::NoValue",                                
                "type": "Ref",                                                                                                                      
                "isCfnFunction": true                          
              }                                                                                                                                     
            ],                                                                                                                                      
            "type": "Fn::If",                                                                                                                       
            "isCfnFunction": true                                      
          }                                                                                                                                         
        ]                                                    
      }                                                                                                                                             
    }                                                                                                                                               
  }
}`;

const tmpobj = tmp.fileSync();                                                                                                                    
                                                                          
fs.writeFileSync(tmpobj.fd, template);                                                                                                            
                                                                          
new CfnInclude(this, 'MyStack', {                                                                                                                 
    templateFile: tmpobj.name                                         
});                                                                                                                                               
                                                                          
tmpobj.removeCallback();  

You will get the exception

CfnSynthesisError: Resolution error: Supplied properties not correct for "CfnLoadBalancerProps"                                                     
  tags: element 1: {} should have a 'key' and a 'value' property.    

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.87.0

Framework Version

No response

Node.js Version

v18.18.2

OS

AL2

Language

TypeScript

Language Version

No response

Other information

No response

@KyleBS KyleBS added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 3, 2024
@github-actions github-actions bot added the @aws-cdk/cloudformation-include Issues related to the "CFN include v.20" package label Jul 3, 2024
@ashishdhingra ashishdhingra self-assigned this Jul 3, 2024
@ashishdhingra ashishdhingra added needs-reproduction This issue needs reproduction. and removed needs-triage This issue or PR still needs to be triaged. labels Jul 3, 2024
@ashishdhingra
Copy link
Contributor

@KyleBS Good afternoon. Thanks for reporting the issue. I'm unsure of your template is correct, but the issue is reproducible using below (modified) CDK code (based on guidance from Conditions and Conditionally tagging resources in CloudFormation):

import * as cdk from 'aws-cdk-lib';
import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';
import { Construct } from 'constructs';
import * as fs from 'fs';
const tmp = require('tmp');

export class Issue30740Stack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const template = `{                                                                                                                         
      "AWSTemplateFormatVersion": "2010-09-09",                             
      "Parameters": {                                                      
        "IsExtraTag": {                                                       
          "Type": "String",            
          "AllowedValues": [                                                  
            "true",                                                           
            "false"                             
          ],                                                                                                                                            
          "Default": "false"                                                                                                                                                                                                                                                                                 
        }                                                                                                                                                                                                                                                                                                    
      },                                                                                                                                                
      "Conditions": {                        
        "AddExtraTag": {                                                 
          "Fn::Equals": [                                                                                                                               
            {                                                                                                                                           
              "Ref": "IsExtraTag",                                                                                                                 
            },                                                             
            "true"                                                                                                                                      
          ]                                                        
        }                                                                                                                                               
      },                                                                                                                                                
      "Resources": {                                                                                                                                    
        "MyLoadBalancer": {                                                
          "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",                                                                                          
          "Properties": {                                        
            "Name": "my-load-balancer",                                                                                                                 
            "Subnets": [                                                                                                                                
              "subnet-0123456789abcdef0"                                                                                                                                                                                                                                                                     
            ],                                                                                                                                          
            "SecurityGroups": [                                               
              "sg-0123456789abcdef0"                                                                                                                    
            ],                                                                
            "Scheme": "internet-facing",                                                                                                                
            "LoadBalancerAttributes": [                                       
              {                                                                                                                                         
                "Key": "idle_timeout.timeout_seconds",                      
                "Value": "60"                                                                                                                           
              }                                                               
            ],                         
            "IpAddressType": "ipv4",                                          
            "Tags": [                                                                                                                                   
              {                                 
                "Key": "Name",                                                                                                                          
                "Value": "MyLoadBalancer"                                                                                                                                                                                                                                                                    
              },                                                                                                                                                                                                                                                                                             
              {                                                                                                                                         
                "Fn::If": [                          
                  "AddExtraTag",                                          
                  {                                                                                                                                     
                    "Key": "Name2",                                                                                                                     
                    "Value": "MyLoadBalancer2"                       
                  },                                         
                  {                                                                                                                                     
                    "Ref": "AWS::NoValue",
                  }                                                                                                                                     
                ]                                    
              }                                                                                                                                         
            ]                                                    
          }                                                                                                                                             
        }                                                                                                                                               
      }
    }`;
    
    const tmpobj = tmp.fileSync();                                                                                                                    
                                                                              
    fs.writeFileSync(tmpobj.fd, template);                                                                                                            
                                                                              
    new CfnInclude(this, 'MyStack', {                                                                                                                 
        templateFile: tmpobj.name                                         
    });                                                                                                                                               
                                                                              
    tmpobj.removeCallback(); 
  }
}

Got the below error during cdk synth command:

CfnSynthesisError: Resolution error: Supplied properties not correct for "CfnLoadBalancerProps"
  tags: element 1: {} should have a 'key' and a 'value' property.
    at ValidationResult.assertSuccess (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/runtime.js:1:2707)
    at convertCfnLoadBalancerPropsToCloudFormation (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/aws-elasticloadbalancingv2/lib/elasticloadbalancingv2.generated.js:1:96969)
    at CfnLoadBalancer.renderProperties (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/aws-elasticloadbalancingv2/lib/elasticloadbalancingv2.generated.js:1:90724)
    at PostResolveToken.Resources (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/cfn-resource.js:1:7803)
    at PostResolveToken.postProcess (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/util.js:1:1653)
    at Object.postProcess (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:1205)
    at DefaultTokenResolver.resolveToken (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/resolvable.js:1:1483)
    at resolve (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:2711)
    at Object.resolve (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:1079)
    at resolve (/Users/REDACTED/dev/repros/cdk/issue30740/node_modules/aws-cdk-lib/core/lib/private/resolve.js:1:2990) {
  type: 'CfnSynthesisError'
}

Subprocess exited with error 1

It is more likely related to existing open issue #27594.

Thanks,
Ashish

@ashishdhingra ashishdhingra added p1 effort/medium Medium work item – several days of effort and removed needs-reproduction This issue needs reproduction. labels Jul 3, 2024
@ashishdhingra ashishdhingra removed their assignment Jul 3, 2024
@ashishdhingra ashishdhingra added the @aws-cdk/core Related to core CDK functionality label Jul 3, 2024
@mikewrighton mikewrighton self-assigned this Sep 4, 2024
@comcalvi comcalvi self-assigned this Oct 1, 2024
@mergify mergify bot closed this as completed in #31578 Oct 3, 2024
@mergify mergify bot closed this as completed in 9410361 Oct 3, 2024
Copy link

github-actions bot commented Oct 3, 2024

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

1 similar comment
Copy link

github-actions bot commented Oct 3, 2024

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/cloudformation-include Issues related to the "CFN include v.20" package @aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants