cdk-from-cfn icon indicating copy to clipboard operation
cdk-from-cfn copied to clipboard

Feature Request: Typescript - CFN conditions are created as regular variables in CDK and fails cfn stack refactor

Open Amplifiyer opened this issue 7 months ago • 1 comments

If I have the following CFN condition in my Stack A

Conditions:
  ShouldNotCreateEnvResources:
    Fn::Equals:
      - Ref: amplify_env
      - NONE

and using this condition in a resource

  api4ad86e06:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Body:
        ...
        basePath:
          Fn::If:
            - ShouldNotCreateEnvResources
            - /Prod
            - Fn::Join:
                - ""
                - - /
                  - Ref: amplify_env

this tool generates the following CDK code

// Conditions
const shouldNotCreateEnvResources = props.amplify_env! === 'NONE';
...
const api4ad86e06 = new apigateway.CfnRestApi(this, 'api4ad86e06', {
  body: { ... },
    ....
    basePath: shouldNotCreateEnvResources ? '/Prod' : [
      '/',
      props.amplify_env!,
    ].join(''),

However when I now execute stack refactor to move this resource from Stack A to a Stack B represented by this CDK code, it fails with the error Template error: unresolved condition dependency ShouldNotCreateEnvResources in Fn::If

I was able to unblock myself by changing the generated code to

-    const shouldNotCreateEnvResources = props.amplify_env! === 'NONE';
+    new cdk.CfnCondition(this, 'ShouldNotCreateEnvResources', {
+      expression: cdk.Fn.conditionEquals(props.amplify_env, 'NONE'),
+    });
-        basePath: shouldNotCreateEnvResources ? '/Prod' : [
-          '/',
-          props.amplify_env!,
-        ].join(''),
+        basePath: cdk.Fn.conditionIf('ShouldNotCreateEnvResources', 'Prod', ['/', props.amplify_env].join('')),

Amplifiyer avatar Jul 21 '25 21:07 Amplifiyer

Replacing CFN Conditions with code conditionals was an intentional design decision, since the former is not best practice in CDK.

The error is a limitation of the CFN refactor API.

mrgrain avatar Jul 22 '25 10:07 mrgrain