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: CDK to CFN template is serializing incorrectly #26111

Open
srivastavr opened this issue Jun 26, 2023 · 4 comments
Open

stepfunctions: CDK to CFN template is serializing incorrectly #26111

srivastavr opened this issue Jun 26, 2023 · 4 comments
Labels
@aws-cdk/aws-stepfunctions Related to AWS StepFunctions bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@srivastavr
Copy link

Describe the bug

Issue Description

Hi,

We are passing array of array in payload from step function to a lambda function, below is the sample code

            lambdaFunction: serviceCallHandler,
            resultSelector: resultSelector,
            resultPath: resultPath,
            payload: TaskInput.fromObject({
                key1: [
                    [
                        {
                            key2: {
                                key3: 'value1',
                            },
                            key4: [
                                [
                                    {
                                        key5: {
                                            key6: 'value2',
                                        },
                                    },
                                ],
                            ],
                        },
                    ],
                ],
            }),
        }

but after building, the cloud formation template converts this into array of objects with 0 index at places which is incorrect.

like this

 {
        "Ref": "AWS::Partition"
       },
       ":states:::lambda:invoke\",\"Parameters\":{\"FunctionName\":\"",
       {
        "Fn::GetAtt": [
         "servicecallhandler3F6E12B2",
         "Arn"
        ]
       },
       "\",\"Payload\":{\"key1\":[{\"0\":{\"key2\":{\"key3\":\"value1\"},\"key4\":[{\"0\":{\"key5\":{\"key6\":\"value2\"}}}]}}]}}}

Expected Behavior

 {
        "Ref": "AWS::Partition"
       },
       ":states:::lambda:invoke\",\"Parameters\":{\"FunctionName\":\"",
       {
        "Fn::GetAtt": [
         "servicecallhandler3F6E12B2",
         "Arn"
        ]
       },
       "\",\"Payload\":{\"key1\":[[{\"key2\":{\"key3\":\"value1\"},\"key4\":[[{\"key5\":{\"key6\":\"value2\"}]}]}}]]}}

Observed Behavior

Test Cases

NA

Other Details

No response

Expected Behavior

 {
        "Ref": "AWS::Partition"
       },
       ":states:::lambda:invoke\",\"Parameters\":{\"FunctionName\":\"",
       {
        "Fn::GetAtt": [
         "servicecallhandler3F6E12B2",
         "Arn"
        ]
       },
       "\",\"Payload\":{\"key1\":[[{\"key2\":{\"key3\":\"value1\"},\"key4\":[[{\"key5\":{\"key6\":\"value2\"}]}]}}]]}}

Current Behavior

{
        "Ref": "AWS::Partition"
       },
       ":states:::lambda:invoke\",\"Parameters\":{\"FunctionName\":\"",
       {
        "Fn::GetAtt": [
         "servicecallhandler3F6E12B2",
         "Arn"
        ]
       },
       "\",\"Payload\":{\"key1\":[{\"0\":{\"key2\":{\"key3\":\"value1\"},\"key4\":[{\"0\":{\"key5\":{\"key6\":\"value2\"}}}]}}]}}}

Reproduction Steps

building the typescript code

Possible Solution

The serialization is implemented in CDK:

public static fromObject(obj: { [key: string]: any }) {
return new TaskInput(InputType.OBJECT, obj);
}

Additional Information/Context

No response

CDK CLI Version

"aws-cdk": "^2.72.0"

Framework Version

No response

Node.js Version

NodeJS/releases/default.331411.0

OS

Linux

Language

Typescript

Language Version

"typescript": "^4.9.4"

Other information

No response

@srivastavr srivastavr added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 26, 2023
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Jun 26, 2023
@srivastavr
Copy link
Author

reference from CFN : aws-cloudformation/cloudformation-coverage-roadmap#1722

@pahud
Copy link
Contributor

pahud commented Jun 26, 2023

This is the code I used to reproduce this issue

export class Demo2Stack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const obj = {
      key1: [
          [
              {
                  key2: {
                      key3: 'value1',
                  },
                  key4: [
                      [
                          {
                              key5: {
                                  key6: 'value2',
                              },
                          },
                      ],
                  ],
              },
          ],
      ],
    }
    const payload = sfn.TaskInput.fromObject(obj)
    const invoke = new sfntasks.LambdaInvoke(this, 'Invoke Func', {
      lambdaFunction: getLambdaFunction(this),
      payload,
    });

    new sfn.StateMachine(this, 'StateMachine', {
      definition: invoke,
    })
    
    new CfnOutput(this, 'input', { value: JSON.stringify(payload) })
  };
};

I can confirm the CfnOutput is serialized correctly:

Outputs:
  input:
    Value: '{"type":1,"value":{"key1":[[{"key2":{"key3":"value1"},"key4":[[{"key5":{"key6":"value2"}}]]}]]}}'

But the state machine DefinitionString is not serialized correctly:

 StateMachine2E01A3A5:
    Type: AWS::StepFunctions::StateMachine
    Properties:
      RoleArn:
        Fn::GetAtt:
          - StateMachineRoleB840431D
          - Arn
      DefinitionString:
        Fn::Join:
          - ""
          - - '{"StartAt":"Invoke Func","States":{"Invoke Func":{"End":true,"Retry":[{"ErrorEquals":["Lambda.ServiceException","Lambda.AWSLambdaException","Lambda.SdkClientException"],"IntervalSeconds":2,"MaxAttempts":6,"BackoffRate":2}],"Type":"Task","Resource":"arn:'
            - Ref: AWS::Partition
            - :states:::lambda:invoke","Parameters":{"FunctionName":"
            - Fn::GetAtt:
                - Func217E03A4
                - Arn
            - '","Payload":{"key1":[{"0":{"key2":{"key3":"value1"},"key4":[{"0":{"key5":{"key6":"value2"}}}]}}]}}}}}'

@pahud pahud added the p1 label Jun 26, 2023
@pahud pahud changed the title CDK to CFN template is serializing incorrectly stepfunctions: CDK to CFN template is serializing incorrectly Jun 26, 2023
@pahud pahud added effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jun 26, 2023
@peterwoodworth peterwoodworth added @aws-cdk/aws-stepfunctions Related to AWS StepFunctions and removed @aws-cdk/aws-lambda Related to AWS Lambda labels Jun 26, 2023
@srivastavr
Copy link
Author

Hi, Any updates ?

@sakurai-ryo
Copy link
Contributor

Hello @srivastavr, maybe this PR has solved the problem.
#26055

@pahud pahud added p2 and removed p1 labels Jun 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 Related to AWS StepFunctions 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