Skip to content

cdk migrate cannot deal with CDK Stacks #458

Open
@mrgrain

Description

@mrgrain

cdk migrate --from-stack --stack-name AppStack

AppStack is the default stack as created by cdk init

First failure:

 ❌  Migrate failed for `AppStack`: AppStackStack could not be generated because Analytics is not a valid property for resource CDKMetadata of type AWS::CDK::Metadata

Deployed the stack again with --no-version-reporting. Then the migrate command works but generated code contains this (including syntax errors):

import * as cdk from 'aws-cdk-lib';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as sqs from 'aws-cdk-lib/aws-sqs';

export interface AppStackStackProps extends cdk.StackProps {
  /**
   * Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
   * @default '/cdk-bootstrap/hnb659fds/version'
   */
  readonly bootstrapVersion?: string;
}

export class AppStackStack extends cdk.Stack {
  public constructor(scope: cdk.App, id: string, props: AppStackStackProps = {}) {
    super(scope, id, props);

    // Applying default props
    props = {
      ...props,
      bootstrapVersion: new cdk.CfnParameter(this, 'BootstrapVersion', {
        type: 'AWS::SSM::Parameter::Value<String>',
        default: props.bootstrapVersion?.toString() ?? '/cdk-bootstrap/hnb659fds/version',
        description: 'Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]',
      }).valueAsString,
    };

    // Resources
    const appQueueFd3f4958 = new sqs.CfnQueue(this, 'AppQueueFD3F4958', {
      visibilityTimeout: 300,
    });
    appQueueFd3f4958.cfnOptions.metadata = {
      aws:cdk:path: 'AppStack/AppQueue/Resource',
    };
    appQueueFd3f4958.cfnOptions.deletionPolicy = cdk.CfnDeletionPolicy.DELETE;

    const appTopic115Ea044 = new sns.CfnTopic(this, 'AppTopic115EA044', {
    });
    appTopic115Ea044.cfnOptions.metadata = {
      aws:cdk:path: 'AppStack/AppTopic/Resource',
    };

    const appQueuePolicyBd4f9387 = new sqs.CfnQueuePolicy(this, 'AppQueuePolicyBD4F9387', {
      policyDocument: {
        Statement: [
          {
            Action: 'sqs:SendMessage',
            Condition: {
              ArnEquals: {
                'aws:SourceArn': appTopic115Ea044.ref,
              },
            },
            Effect: 'Allow',
            Principal: {
              Service: 'sns.amazonaws.com',
            },
            Resource: appQueueFd3f4958.attrArn,
          },
        ],
        Version: '2012-10-17',
      },
      queues: [
        appQueueFd3f4958.ref,
      ],
    });
    appQueuePolicyBd4f9387.cfnOptions.metadata = {
      aws:cdk:path: 'AppStack/AppQueue/Policy/Resource',
    };

    const appQueueAppStackAppTopic352A2f0bba0ff351 = new sns.CfnSubscription(this, 'AppQueueAppStackAppTopic352A2F0BBA0FF351', {
      endpoint: appQueueFd3f4958.attrArn,
      protocol: 'sqs',
      topicArn: appTopic115Ea044.ref,
    });
    appQueueAppStackAppTopic352A2f0bba0ff351.cfnOptions.metadata = {
      aws:cdk:path: 'AppStack/AppQueue/AppStackAppTopic352A2F0B/Resource',
    };
    appQueueAppStackAppTopic352A2f0bba0ff351.addDependency(appQueuePolicyBd4f9387);
  }
}

Few things to note here:

A/ the generated code should not have syntax errors
B/ we should skip the CDK path metadata anyway
C/ we should probably not include the BootstrapVersion parameter

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions