hashicorp / cdktf-aws-cdk

Use AWS CDK constructs in CDKTF projects
Mozilla Public License 2.0
93 stars 14 forks source link

Can't invoke a state machine from a state machine (nested workflows) #82

Open smcroskey opened 2 years ago

smcroskey commented 2 years ago

I have a simple state machine using the AWS CDK adapter with cdktf where I am trying to use nested workflows via the StepFunctionsStartExecution optimized integration.

        // create child state machine
        const manualReviewStateMachine = new StateMachine(...)

       // in the parent state machine...
        return new sfnTasks.StepFunctionsStartExecution(this.scope, `Manual Review`, {
            stateMachine: manualReviewStateMachine,
            integrationPattern: IntegrationPattern.RUN_JOB,
            input: sfn.TaskInput.fromObject({...})
        })

However, the cdktf JSON output doesn't appear to be valid because the reference to the child state machine uses a Ref attribute that doesn't exist (modified the TF json output slightly to make it more readable, but the key is the "Ref" attribute in the StateMachineArn field value for the Manual Review state):

"Manual Review":{
    "Next": "...",
    "Type": "Task",
    "Resource": join("", "arn:", data.aws_partition.aws-adapter_aws-partition_68D67DE5.partition, ":states:::states:startExecution.sync:2"),
    "Parameters": {
        "Input": { ... },
        "StateMachineArn": jsondecode(aws_cloudcontrolapi_resource.aws-adapter_ParallelComplianceLegalEntityReviewManualReviewStateMachine0B6709C9_D8D91C4C.properties)["Ref"]
    }
}

If I try to deploy, it complains about a missing Ref attribute. Note that if I change the Ref above to Arn and deploy, it deploys successfully. It seems that the StateMachine mapping for the AWS CDK adapter must not be including the Ref attribute?

smcroskey commented 2 years ago

Based on the above, I was able to work around this issue by deriving from StateMachine and overriding the stateMachineArn property like this:

export class StateMachine extends sfn.StateMachine {
    constructor(scope: Construct, id: string, props: StateMachineProps) {
        super(scope, id, props);
        // @ts-ignore
        this.stateMachineArn = this.getResourceArnAttribute(
            (this.node.defaultChild as CfnStateMachine).attrArn,
            {
                service: 'states',
                resource: 'stateMachine',
                resourceName: this.physicalName,
                arnFormat: ArnFormat.COLON_RESOURCE_NAME,
            }
        )
    }
}

(basically just replacing the resource attribute here from ref to attrArn)

ansgarm commented 2 years ago

Wow, nice detective work here!

I don't know about the specifics that are at play here, but it feels like this could be a difference between CloudFormation and the AWS CloudControl API with the latter not returning a ref attribute while CloudFormation seems to support this. But that is something that should be confirmed first.

We could however, add a special case for state machines and switch from Ref to Arn in this case. The current implementation for resources that are supported by the AWS CC API just passes along, whatever was called.

github-actions[bot] commented 2 years ago

This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon.

github-actions[bot] commented 2 years ago

This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon.

github-actions[bot] commented 1 year ago

This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon.

github-actions[bot] commented 1 year ago

Closing this issue as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen.

msmith93 commented 1 month ago

Is there any workaround for this issue when you are using a construct that is out of our control has this kind of issue? We're using a construct that defines an S3 bucket and S3 bucket policy and the same kind of .ref reference:

https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-s3/lib/bucket.ts#L2007

I wonder if this can be solved by simply adding ref as an attribute to the bucket resource: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/s3_bucket#attribute-reference