cdk deployment error: RuntimeError: Cannot read properties of undefined (reading 'bindToGraph')

0

I am trying to do a cdk deployment, where I am just copying the example from the documentation. See example below (https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_stepfunctions/StateMachineFragment.html#aws_cdk.aws_stepfunctions.StateMachineFragment)

 from aws_cdk import (
    # Duration,
    Stack,
    aws_stepfunctions as sfn,
    aws_stepfunctions_tasks as tasks,
 )
 from constructs import Construct

 class MyJob(sfn.StateMachineFragment):

    def __init__(self, parent, id, *, job_flavor):
        super().__init__(parent, id)

        choice = sfn.Choice(self, "Choice").when(sfn.Condition.string_equals("$.branch", "left"), sfn.Pass(self, "Left Branch")).when(sfn.Condition.string_equals("$.branch", "right"), sfn.Pass(self, "Right Branch"))

        # ...

        self.start_state = choice
        self.end_states = choice.afterwards().end_states
      
    def start_state(self):
        return self._start_state

    def end_states(self):
        return self._end_states


class CdkStateMachineFragmentStack(Stack):
    def __init__(self, scope, id):
        super().__init__(scope, id)
        # Do 3 different variants of MyJob in parallel
        parallel = sfn.Parallel(self, "All jobs").branch(MyJob(self, "Quick", job_flavor="quick").prefix_states()).branch(MyJob(self, "Medium", job_flavor="medium").prefix_states()).branch(MyJob(self, "Slow", job_flavor="slow").prefix_states())

        sfn.StateMachine(self, "MyStateMachineFragmentTest",
            definition=parallel
        )

When deploying, I get the following error:

Traceback (most recent call last): File "app.py", line 10, in <module> CdkStateMachineFragmentStack(app, "CdkStateMachineFragmentStack", File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call inst = super().call(*args, **kwargs) File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/cdk_state_machine_fragment/cdk_state_machine_fragment_stack.py", line 34, in init sfn.StateMachine(self, "MyStateMachineFragmentTest", File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call inst = super().call(*args, **kwargs) File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/aws_cdk/aws_stepfunctions/init.py", line 6871, in init jsii.create(self.class, self, [scope, id, props]) File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_kernel/init.py", line 336, in create response = self.provider.create( File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_kernel/providers/process.py", line 363, in create return self._process.send(request, CreateResponse) File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_kernel/providers/process.py", line 340, in send raise RuntimeError(resp.error) from JavaScriptError(resp.stack) RuntimeError: Cannot read properties of undefined (reading 'bindToGraph')

I tried already what is suggested in the following stack overflow post [https://stackoverflow.com/questions/70553737/cannot-read-properties-of-undefined-reading-bindtograph] getting a different error:

Traceback (most recent call last): File "app.py", line 10, in <module> CdkStateMachineFragmentStack(app, "CdkStateMachineFragmentStack", File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call inst = super().call(*args, **kwargs) File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/cdk_state_machine_fragment/cdk_state_machine_fragment_stack.py", line 34, in init parallel = sfn.Parallel(self, "All jobs").branch(MyJob(self, "Quick", job_flavor="quick").prefix_states()).branch(MyJob(self, "Medium", job_flavor="medium").prefix_states()).branch(MyJob(self, "Slow", job_flavor="slow").prefix_states()) File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/.venv/lib/python3.8/site-packages/jsii/_runtime.py", line 111, in call inst = super().call(*args, **kwargs) File "/home/workspace/development/cdk/cdk_test/cdk_state_machine_fragment/cdk_state_machine_fragment/cdk_state_machine_fragment_stack.py", line 18, in init self.start_state = choice AttributeError: can't set attribute

Could you support me?

Thanks, Pablo

1 Answer
0

The error message "RuntimeError: Cannot read properties of undefined" is typically encountered when trying to access a property or method of a variable that has not been defined. In order to resolve this issue, you can add the @property decorator above the start_state and end_states methods, this will ensure that the methods are treated as properties and can be accessed without encountering the undefined error.

The second error message "AttributeError: can't set attribute" occurred because you had defined methods and attributes with the same name, namely "start_state" and "end_states". In Python, methods are attributes too, and having an attribute and method with the same name can cause naming conflicts. To fix this issue, you can simply rename the attributes to something else, you could use "_start_state" and "_end_states".

You can find both fixes below, these changes will ensure that your code runs smoothly:

from aws_cdk import (
    # Duration,
    Stack,
    aws_stepfunctions as sfn,
    aws_stepfunctions_tasks as tasks,
 )
from constructs import Construct

class MyJob(sfn.StateMachineFragment):

    def __init__(self, parent, id, *, job_flavor):
        super().__init__(parent, id)

        choice = sfn.Choice(self, "Choice").when(sfn.Condition.string_equals("$.branch", "left"), sfn.Pass(self, "Left Branch")).when(sfn.Condition.string_equals("$.branch", "right"), sfn.Pass(self, "Right Branch"))

        # ...

        self._start_state = choice
        self._end_states = choice.afterwards().end_states

    @property
    def start_state(self):
        return self._start_state
    @property
    def end_states(self):
        return self._end_states


class CdkStateMachineFragmentStack(Stack):
    def __init__(self, scope, id):
        super().__init__(scope, id)
        # Do 3 different variants of MyJob in parallel
        parallel = sfn.Parallel(self, "All jobs").branch(MyJob(self, "Quick", job_flavor="quick").prefix_states()).branch(MyJob(self, "Medium", job_flavor="medium").prefix_states()).branch(MyJob(self, "Slow", job_flavor="slow").prefix_states())

        sfn.StateMachine(self, "MyStateMachineFragmentTest",
            definition=parallel
        )
AWS
Maha_E
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions