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.5k stars 3.85k forks source link

aws-stepfunctions: CDK doesn't realize that it has to destroy the `StateMachine` if `StateMachineType` changes #30709

Open garysassano opened 2 months ago

garysassano commented 2 months ago

Describe the bug

1:51:23 AM | UPDATE_FAILED        | AWS::StepFunctions::StateMachine | SfnStateMachineCF6AB1C9
CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename arn:aws:states:eu-central-1:XXXXXXXXXXXX:stateMachine:sfn-state-machine and update the stack again.

CDK attempts to update the state machine when you switch from StateMachineType.STANDARD to StateMachineType.EXPRESS, which is not allowed. As per the official docs:

image

CDK should instead destroy the state machine and recreate it.

Expected Behavior

StateMachine to be destroyed and recreated when StateMachineType changes.

Current Behavior

CDK attempts to update in place the existing StateMachine.

Reproduction Steps

Deploy a StateMachine, change the StateMachineType and try to deploy it again.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.145.0

Framework Version

No response

Node.js Version

20.15.0

OS

Ubuntu 22.04.3 LTS

Language

TypeScript

Language Version

No response

Other information

No response

khushail commented 2 months ago

Hi @garysassano , thanks for reaching out.

IMO, as its clearly mentioned in the docs that StateMachineType can not be changed, I assume that's an acceptable behavior.

AFAIK, Since the StateMachineArn is not mutable, changing the StateMachineType is causing a replacement and hence the error. I am not really sure why you would want to do that.

It would be advisable to create a new StateMachine if type is changed. I am not clear on the usecase of changing the type when new one can be created. Could you please clarify?

See this as well -

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-stepfunctions-statemachine.html#cfn-stepfunctions-statemachine-statemachinename

Screenshot 2024-07-01 at 3 27 22 PM
pahud commented 2 months ago

StateMachineType is a prop of AWS::StepFunctions::StateMachine so it's definitely allowed to change from CFN's perspective.

But we need to note:

Update requires: Replacement

This means a new StateMachine would be created in replacement of the previous one.

One common error is that when you have specified a custom name of the original one, creating a new one with exactly the same name as a replacement of the original one would cause duplication if the resource does not allow you to have duplicate name as the error indicates:

CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename arn:aws:states:eu-central-1:XXXXXXXXXXXX:stateMachine:sfn-state-machine and update the stack again.

Solution:

  1. Do not specify stateMachineName, instead, let CFN auto generate for you.
  2. If it doesn't work, you need to specify a different custom name when you update the StateMachineType.

Let me know if it works for you.

pahud commented 2 months ago

PoC

    // create a dummy statemachine
    new sfn.StateMachine(this, 'StateMachine', {
      definition: new sfn.Pass(this, 'Pass'),
      stateMachineType: StateMachineType.EXPRESS, // update this and re-deploy
    });

cdk diff

image

and cdk deploy works with no error.

image