Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stepfunctions-tasks: Cannot use fromJsonPathAt when passing arg array values to ScriptBootstrapActionConfigProperty #29299

Open
anthonysgro opened this issue Feb 28, 2024 · 7 comments
Labels
@aws-cdk/aws-stepfunctions-tasks bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@anthonysgro
Copy link

anthonysgro commented Feb 28, 2024

Describe the bug

When writing step functions, often I want to pass a value from the payload to an API that expects an array value like this:

bootstrapActions: [
    {
        name: 'MyBootstrapAction',
        scriptBootstrapAction: {
            path: 'my_path',
            "Args.$": '$.bootstrapArgs' // $.bootstrapArgs is an array value
        },
     },
],

or

bootstrapActions: [
    {
        name: 'MyBootstrapAction',
        scriptBootstrapAction: {
            path: 'my_path',
            args: sfn.TaskInput.fromJsonPathAt('$.bootstrapArgs').value
        },
     },
],

This works for EmrAddStepsProps args property, but not for the args property within ScriptBootstrapActionConfigProperty. This means I cannot pass custom arguments based on my payload to bootstrap arguments when creating clusters, and this behavior seems contradictory to other tasks I have used in the library.

Expected Behavior

I expected that I could dynamically pass a payload value to the args property within ScriptBootstrapActionConfigProperty inside of EmrCreateClusterProps. This should correspond to the task in the Step Functions console having an "Args.$" key instead of "Args". However, I cannot do this via CDK. I can do this via manual cloudformation and editing in the console, so this seems to be a CDK limitation.

Current Behavior

Instead, it fails:

TypeError: x.map is not a function

    at /Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/core/lib/runtime.js:1:1549

    at BootstrapActionConfigToJson (/Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/aws-stepfunctions-tasks/lib/emr/private/cluster-utils.js:1:10250)

    at Array.map (<anonymous>)

    at /Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/core/lib/runtime.js:1:1549

    at EmrCreateCluster._renderTask (/Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/aws-stepfunctions-tasks/lib/emr/emr-create-cluster.js:1:3931)

    at EmrCreateCluster.toStateJson (/Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/aws-stepfunctions/lib/states/task-base.js:1:2233)

    at StateGraph.toGraphJson (/Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/aws-stepfunctions/lib/state-graph.js:1:1984)

    at ChainDefinitionBody.bind (/Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/aws-stepfunctions/lib/state-machine.js:1:11644)

    at new StateMachine (/Volumes/workplace/MyWorkplace/src/MyService/node_modules/aws-cdk-lib/aws-stepfunctions/lib/state-machine.js:1:6100)

    at PersistentClusterStateMachine.createPersistentClusterStateMachine (/Volumes/workplace/MyWorkplace/src/MyService/dist/lib/constructs/stepfunctions/persistentClusterStateMachine.js:47:16)

Reproduction Steps

In CDK, I have done this before with the following design pattern successfully:

const myTask = new sfn.Pass(this, 'FormatArgs', {
    parameters: {
        'emrStepArgs.$': `States.Array(
            'spark-submit',
            '--deploy-mode', 'cluster',
            '--class','${this.customValue}',
            $.payload.customValueFromPayload
            )',
        clusterId: sfn.TaskInput.fromJsonPathAt('$.clusterId').value,
        job: sfn.TaskInput.fromJsonPathAt('$.job').value
    }
}).next(
    new tasks.EmrAddStep(this, 'RunStep', {
        name: 'TaskName',
        clusterId: sfn.TaskInput.fromJsonPathAt('$.clusterId').value,
        jar: 'command-runner.jar',
        args: sfn.TaskInput.fromJsonPathAt('$.emrStepArgs').value
    })

If you notice, in EmrAddStepProps, args is expected to be an array, but allows this as is receiving this array from the input of the payload.

However, this does not work for the EmrCreateCluster api, specifically for the args property in ScriptBootstrapActionConfigProperty. Here is an example that fails to build:

const myTask = new sfn.Pass(this, 'FormatBootstrapArgs, {
    {
        parameters: {
            'payload.$': '$.payload',
            'bootstrapArgs.$': `States.Array(
                'string_literal',
                '${this.customValue}',
                $.payload.customValueFromPayload
            )',
        }
}).next(
    new tasks.EmrCreateCluster(this, 'CreateEmrCluster', {
        name: 'ClusterName',
        releaseLabel: '7.0.0',
        instances: ...
        clusterRole: ...
        serviceRole: ...
        applications: ....
        configuration: ...
        bootstrapActions: [
            {
                name: 'MyBootstrapAction',
                scriptBootstrapAction: {
                    path: 'my_path',
                    args: sfn.TaskInput.fromJsonPathAt('$.bootstrapArgs').value
                },
             },
         ],
         ....
});

I can actually bypass this error when doing:

bootstrapActions: sfn.TaskInput.fromObject([
            {
                name: 'MyBootstrapAction',
                scriptBootstrapAction: {
                    path: 'my_path',
                    'Args.$': '$.bootstrapArgs'
                },
             },
         ]),

But then the Args parameter is dropped on synthesis and it doesn't get published to the AWS account

Possible Solution

It seems that sfn.TaskInput.fromJsonPathAt creates a resolvable token that is a string under the hood. However, CDK is still trying to map over this token which is incorrect behavior. I think that a possible fix could be to ignore this mapping behavior on resolvable tokens.

No response

Additional Information/Context

No response

CDK CLI Version

2.126.0

Framework Version

No response

Node.js Version

v18.19.1

OS

Sonoma 14.3.1

Language

TypeScript

Language Version

No response

Other information

No response

@anthonysgro anthonysgro added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 28, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@anthonysgro anthonysgro reopened this Feb 28, 2024
@anthonysgro
Copy link
Author

anthonysgro commented Feb 28, 2024

I see this token is being mapped here:

/**
* Render the BootstrapActionProperty as JSON
*
* @param property
*/
export function BootstrapActionConfigToJson(property: EmrCreateCluster.BootstrapActionConfigProperty) {
return {
Name: cdk.stringToCloudFormation(property.name),
ScriptBootstrapAction: {
Path: cdk.stringToCloudFormation(property.scriptBootstrapAction.path),
Args: cdk.listMapper(cdk.stringToCloudFormation)(property.scriptBootstrapAction.args),
},
};
}

@anthonysgro anthonysgro changed the title stepfunctions-tasks: Cannot use fromJsonPathAt when passing values to ScriptBootstrapActionConfigProperty stepfunctions-tasks: Cannot use fromJsonPathAt when passing arg array values to ScriptBootstrapActionConfigProperty Feb 28, 2024
@pahud pahud added p1 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Feb 28, 2024
@eeelnico
Copy link

eeelnico commented Mar 7, 2024

+1

I am experiencing the same issue

@paulhcsun paulhcsun self-assigned this Mar 8, 2024
@paulhcsun
Copy link
Contributor

@anthonysgro I wasn't able to reproduce this error with the following stack, it deployed successfully for me:

    const myTask = new sfn.Pass(this, 'FormatBootstrapArgs', {
        parameters: {
            'payload.$': '$.payload',
            'bootstrapArgs.$': `States.Array(
                'string_literal',
                'com.example.spark.jobs.ProcessData',
                $.payload.customValueFromPayload
            )`,
        }
    }
        ).next(
            new tasks.EmrCreateCluster(this, 'CreateEmrCluster', {
                name: 'ClusterName',
                releaseLabel: 'emr-7.0.0',
                instances: {
                    masterInstanceType: 'm5.xlarge',
                    instanceCount: 2,
                },
                bootstrapActions:[
                    {
                        name: 'MyBootstrapAction',
                        scriptBootstrapAction: {
                            path: 'my_path',
                            args: sfn.TaskInput.fromJsonPathAt('$.bootstrapArgs').value
                        },
                    },
                ]
            }))

Could you provide another example of the stack that failed for you with the missing parameters filled in or let me know if there is some major difference with mine that mightve caused me to not run into the same error?

@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Mar 15, 2024
@pahud pahud assigned pahud and unassigned paulhcsun Mar 15, 2024
Copy link

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.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 17, 2024
@eeelnico
Copy link

I will also try to reproduce in a small/minimal example.

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Mar 19, 2024
@pahud pahud added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Mar 20, 2024
@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 and removed p1 labels Apr 10, 2024
@pahud pahud removed their assignment Apr 10, 2024
@pahud pahud removed the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Apr 10, 2024
@pahud
Copy link
Contributor

pahud commented Apr 10, 2024

@anthonysgro @eeelnico

Are you able to provide a full mini example that we can deploy and reproduce this issue?

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-stepfunctions-tasks bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

4 participants