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

(cli): cdk migrate fails to process Fn::Sub within Fn::ImportValue #29014

Closed
danielfen opened this issue Feb 7, 2024 · 4 comments · Fixed by cdklabs/cdk-from-cfn#581
Closed
Assignees
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p1 package/tools Related to AWS CDK Tools or CLI toolkit/migrate Related to cdk migrate

Comments

@danielfen
Copy link

danielfen commented Feb 7, 2024

Describe the bug

It is common to use the intrinsic function Fn::Sub within Fn::ImportValue, to import cross-stack values in an environment-specific way (e.g. selecting on a dev, test or production environment stack export). An example of this syntax is given on the Fn::ImportValue dcumentation: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html#w9aac34c28c46c15

The CDK Migrate CLI command fails currently when processing templates containing this syntax with the error

Migrate failed for `<stack name>`: stack generation failed due to error '<Resource Path>.Fn::ImportValue: invalid type: map, expected a string at ...'

stack generation failed due to error '<Resource Path>.Fn::ImportValue: invalid type: map, expected a string at ...'

Expected Behavior

cdk migrate to be able to generate CDK code for templates containing

"Fn::ImportValue": {
  "Fn::Sub": [
    "${Value}...",
    {
      "Value": {
        "Ref": "ParameterName"
      }
    }
  ]
}

Current Behavior

An error raised due to the Fn::Sub within Fn::ImportValue not being a string value

Reproduction Steps

Run cdk migrate --stack-name my-stack --language typescript --from-path <template> --output-path <path for output> using the following template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "EnvironmentName": {
      "Description": "Environment used for this deployment.",
      "Type": "String",
      "AllowedValues": [
        "dev",
        "stage",
        "prod"
      ]
    }
  },
  "Resources": {
    "MyApi5xxErrorsAlarm": {
      "Type": "AWS::CloudWatch::Alarm",
      "Properties": {
        "AlarmDescription": "Example alarm",
        "Namespace": "AWS/ApiGateway",
        "Dimensions": [
          {
            "Name": "ApiName",
            "Value": "MyApi"
          }
        ],
        "MetricName": "5XXError",
        "ComparisonOperator": "GreaterThanThreshold",
        "Statistic": "Average",
        "Threshold": 0,
        "Period": 900,
        "EvaluationPeriods": 1,
        "TreatMissingData": "notBreaching",
        "AlarmActions": [
          {
            "Fn::ImportValue": {
              "Fn::Sub": [
                "${Environment}AlarmsTopicArn",
                {
                  "Environment": {
                    "Ref": "EnvironmentName"
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

The error will be raised

Run the same command with the following template and the template is processed successfully:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "EnvironmentName": {
      "Description": "Environment used for this deployment.",
      "Type": "String",
      "AllowedValues": [
        "dev",
        "stage",
        "prod"
      ]
    }
  },
  "Resources": {
    "MyApi5xxErrorsAlarm": {
      "Type": "AWS::CloudWatch::Alarm",
      "Properties": {
        "AlarmDescription": "Example alarm",
        "Namespace": "AWS/ApiGateway",
        "Dimensions": [
          {
            "Name": "ApiName",
            "Value": "MyApi"
          }
        ],
        "MetricName": "5XXError",
        "ComparisonOperator": "GreaterThanThreshold",
        "Statistic": "Average",
        "Threshold": 0,
        "Period": 900,
        "EvaluationPeriods": 1,
        "TreatMissingData": "notBreaching",
        "AlarmActions": [
          {
            "Fn::ImportValue": "devAlarmsTopicArn"
          }
        ]
      }
    }
  }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.126.0 (build fb74c41)

Framework Version

No response

Node.js Version

v18.17.1

OS

20.04.6 LTS (Focal Fossa)

Language

TypeScript

Language Version

No response

Other information

No response

@danielfen danielfen added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 7, 2024
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Feb 7, 2024
@tim-finnigan
Copy link

Thanks for reporting this issue, I could reproduce the error.

@tim-finnigan tim-finnigan added p1 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Feb 7, 2024
@TheRealAmazonKendra
Copy link
Contributor

Looks like the recursion doing the translating is not going deep enough. Adding this task to cdk-from-cfn for fixing.

@TheRealAmazonKendra
Copy link
Contributor

The fix for this was released in v2.132.

Copy link

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

@colifran colifran self-assigned this Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p1 package/tools Related to AWS CDK Tools or CLI toolkit/migrate Related to cdk migrate
Projects
None yet
4 participants