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

Step Function: Unable to trigger Standard Step Function via App Sync API #28727

Closed
khlling opened this issue Jan 16, 2024 · 2 comments
Closed
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@khlling
Copy link

khlling commented Jan 16, 2024

Describe the bug

My flow is as follows:

App sync API Graphql Mutation called
App sync startExectution on Step function
Note the step function is a STANDARD workflow, not an express workflow due to limited timeout time on the express workflow.

I initially had an express workflow but I switched to a standard workflow. I'm using amplify to define the App Sync Schema and custom/cdk to define the step function.

When I call the mutation nothing happens. The step function does not start. No errors visible. No error response from App Sync

Expected Behavior

Calling the mutation triggers the start execution

Current Behavior

When I call the mutation nothing happens. The step function does not start. No errors visible. No error response from App Sync

Reproduction Steps

I followed this setup guide for the flow initially:

https://aws.amazon.com/blogs/mobile/integrate-aws-step-functions-with-aws-amplify-using-amplify-custom-resources/

Works great as is. I customised it for my purpose and it was working fine. However, I was running into an issue with the initial implementation - the express workflow timeout being only 5 mins. The App sync schema was kept the same.

As a result, I changed express to standard workflow. I can manually execute the standard step function (from the console) and this works as expected.

  • But now I can't trigger the step function from the mutation.
  • I've tried to invoke the state machine from the App Sync console manually, but the step function doesn't trigger either

Code Snippet

"""
Create a new 'Execution' type that will be returned by our call
to the Step Functions workflow.
"""
type Execution {
  name: String
  status: String
  input: String
  executionArn: String
  startDate: String
  stopDate: String
  output: String
}

"""
Mutation that triggers the synchronous execution of our Step
Functions workflow.
"""
type Mutation {
  executeMediaProcessingStateMachine(
    input: String!
    itemId: String!
  ): Execution @aws_api_key 
}
const START_EXECUTION_REQUEST_TEMPLATE = (stateMachineArn: String) => {
  return `
  {
    "version": "2018-05-29",
    "method": "POST",
    "resourcePath": "/",
    "params": {
      "headers": {
        "content-type": "application/x-amz-json-1.0",
        "x-amz-target":"AWSStepFunctions.StartSyncExecution"
      },
      "body": {
        "stateMachineArn": "${stateMachineArn}",
        "input": "{ \\\"input\\\": \\\"$context.args.input\\\", \\\"itemId\\\": \\\"$context.args.itemId\\\"}"
      }
    }
  }
`;
};

const RESPONSE_TEMPLATE = `
## Raise a GraphQL field error in case of a datasource invocation error
#if($ctx.error)
  $util.error($ctx.error.message, $ctx.error.type)
#end
## if the response status code is not 200, then return an error. Else return the body **
#if($ctx.result.statusCode == 200)
    ## If response is 200, return the body.
  $ctx.result.body
#else
    ## If response is not 200, append the response to error block.
  $ctx.result.body
#end
`;
   // Create a service role for SFN to use
    const serviceRole = new iam.Role(this, "Role", {
      assumedBy: new iam.ServicePrincipal(
        "states." + cdk.Stack.of(this).region + ".amazonaws.com"
      ),
    });

    /* 
    Defines the express SFN workflow resource using the state 
    machine definition as well as the service role defined above.
    */
    const stateMachine = new sfn.StateMachine(this, "SyncStateMachine", {
      definition: transcribeStateMachineDefinition,
      stateMachineType: sfn.StateMachineType.STANDARD,
      role: serviceRole,
    });

    // Grant AppSync HTTP data source rights to execute the SFN workflow
    stateMachine.grant(
      httpdatasource.grantPrincipal,
      "states:StartSyncExecution"
    );

    // Creates an IAM role that can be assumed by the AWS AppSync service
    const appsyncStepFunctionsRole = new iam.Role(
      this,
      "SyncStateMachineRole",
      {
        assumedBy: new iam.ServicePrincipal("appsync.amazonaws.com"),
      }
    );

    // Allows the role we defined above to execute express SFN workflows
    appsyncStepFunctionsRole.addToPolicy(
      new iam.PolicyStatement({
        resources: [stateMachine.stateMachineArn],
        actions: ["states:StartSyncExecution"],
      })
    );

    /*
    Adds a GraphQL resolver to our HTTP data source that defines how 
    GraphQL requests and fetches information from our SFN workflow.
    */
    httpdatasource.createResolver("execute-state-machine", {
      typeName: "Mutation",
      fieldName: "executeMediaProcessingStateMachine",
      requestMappingTemplate: appsync.MappingTemplate.fromString(
        START_EXECUTION_REQUEST_TEMPLATE(stateMachine.stateMachineArn)
      ),
      responseMappingTemplate:
        appsync.MappingTemplate.fromString(RESPONSE_TEMPLATE),
    });

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2

Framework Version

No response

Node.js Version

v19.8.1

OS

Mac OS

Language

TypeScript

Language Version

No response

Other information

No response

@khlling khlling added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 16, 2024
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Jan 16, 2024
@khlling khlling changed the title Step Function: Unable to trigger Standard State Function via App Sync API Step Function: Unable to trigger Standard Step Function via App Sync API Jan 17, 2024
@khlling
Copy link
Author

khlling commented Jan 18, 2024

Fixed see aws-amplify/amplify-js#12852

@khlling khlling closed this as completed Jan 18, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

No branches or pull requests

1 participant