aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.7k stars 3.93k forks source link

dynamodb: Could not perform a hotswap deployment, because the CloudFormation template could not be resolved: We don't support the 'StreamArn' attribute of the 'AWS::DynamoDB::Table' resource. #32199

Open JonHolman opened 5 days ago

JonHolman commented 5 days ago

Describe the bug

When trying to reference the table stream from a a table in another stack and using cdk watch, I get: Could not perform a hotswap deployment, because the CloudFormation template could not be resolved: We don't support the 'StreamArn' attribute of the 'AWS::DynamoDB::Table' resource. This is a CDK limitation. Please report it at https://github.com/aws/aws-cdk/issues/new/choose

example code:

import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";

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

    const table = new cdk.aws_dynamodb.Table(this, "myTable", {
      partitionKey: {
        name: "pk",
        type: cdk.aws_dynamodb.AttributeType.STRING,
      },
      stream: cdk.aws_dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
    });

    new HelloStack(this, "helloStack", {
      table,
    });
  }
}

interface HelloStackProps extends cdk.NestedStackProps {
  table: cdk.aws_dynamodb.Table;
}

export class HelloStack extends cdk.NestedStack {
  constructor(scope: Construct, id: string, props: HelloStackProps) {
    super(scope, id, props);

    const myLambda = new cdk.aws_lambda.Function(this, "myLambda", {
      runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
      code: cdk.aws_lambda.Code.fromInline(`
        exports.handler = async (event) => {
          return {
            Data: {
              Data: "hello world",
            },
          };
        };
      `),
      handler: "index.handler",
      environment: {
        test: props.table.tableStreamArn!,
      },
    });

    myLambda.addEventSource(
      new cdk.aws_lambda_event_sources.DynamoEventSource(props.table, {
        startingPosition: cdk.aws_lambda.StartingPosition.TRIM_HORIZON,
      })
    );
  }
}

Regression Issue

Last Known Working CDK Version

No response

Expected Behavior

Expect for cdk watch to work successfully hot swapping with dynamodb streams being used directly and/or indirectly with DynamoEventSource.

Current Behavior

hot swap is not working

Reproduction Steps

use the sample code, run cdk watch, make a small change to the lambda and watch for the hot swap error message.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.167.1

Framework Version

No response

Node.js Version

20.11.1

OS

macOS Sequoia 15.1

Language

TypeScript

Language Version

No response

Other information

No response

ashishdhingra commented 2 days ago

Reproducible using customer's provided code:

CdktestStack: start: Building b4f191e73a6d0917814d24f41b268c44283b91c1245f7c80b518c41a4fc1dde5:<<ACCOUNT_ID>>-us-east-2
CdktestStack: success: Built b4f191e73a6d0917814d24f41b268c44283b91c1245f7c80b518c41a4fc1dde5:<<ACCOUNT_ID>>-us-east-2
CdktestStack: start: Building bcb504bb841509279c59e5a0b0b37e0fe81891560c64c7275289a1f6fdce2184:<<ACCOUNT_ID>>-us-east-2
CdktestStack: success: Built bcb504bb841509279c59e5a0b0b37e0fe81891560c64c7275289a1f6fdce2184:<<ACCOUNT_ID>>-us-east-2
CdktestStack: start: Publishing b4f191e73a6d0917814d24f41b268c44283b91c1245f7c80b518c41a4fc1dde5:<<ACCOUNT_ID>>-us-east-2
CdktestStack: start: Publishing bcb504bb841509279c59e5a0b0b37e0fe81891560c64c7275289a1f6fdce2184:<<ACCOUNT_ID>>-us-east-2
CdktestStack: success: Published b4f191e73a6d0917814d24f41b268c44283b91c1245f7c80b518c41a4fc1dde5:<<ACCOUNT_ID>>-us-east-2
CdktestStack: success: Published bcb504bb841509279c59e5a0b0b37e0fe81891560c64c7275289a1f6fdce2184:<<ACCOUNT_ID>>-us-east-2
CdktestStack: deploying... [1/1]
Could not perform a hotswap deployment, because the CloudFormation template could not be resolved: We don't support the 'StreamArn' attribute of the 'AWS::DynamoDB::Table' resource. This is a CDK limitation. Please report it at https://github.com/aws/aws-cdk/issues/new/choose

 ✅  CdktestStack (no changes)

✨  Deployment time: 3.96s

The hotswap works fine when not using NestedStack, using code below:

import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";

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

    const table = new cdk.aws_dynamodb.Table(this, "myTable", {
      partitionKey: {
        name: "pk",
        type: cdk.aws_dynamodb.AttributeType.STRING,
      },
      stream: cdk.aws_dynamodb.StreamViewType.NEW_AND_OLD_IMAGES,
    });

    const myLambda = new cdk.aws_lambda.Function(this, "myLambda", {
      runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
      code: cdk.aws_lambda.Code.fromInline(`
        exports.handler = async (event) => {
          return {
            Data: {
              Data: "hello world",
            },
          };
        };
      `),
      handler: "index.handler",
      environment: {
        test: table.tableStreamArn!,
      },
    });

    myLambda.addEventSource(
      new cdk.aws_lambda_event_sources.DynamoEventSource(table, {
        startingPosition: cdk.aws_lambda.StartingPosition.TRIM_HORIZON,
      })
    );
  }
}

@JonHolman I found existing issue with inputs from CDK team on why hotswap doesn't work for nested stacks:

In your use case, there is a below cross-stack reference:

"helloStackNestedStackhelloStackNestedStackResource61D4F3E5": {
   "Type": "AWS::CloudFormation::Stack",
   "Properties": {
    "Parameters": {
     "referencetoCdkDdbStreamExampleStackmyTableDBDB2785StreamArn": {
      "Fn::GetAtt": [
       "myTableA48C5C70",
       "StreamArn"
      ]
     }
    },

Please refer above issues for possible explanation.

Thanks, Ashish

github-actions[bot] commented 8 hours ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.