How to call a dynamic activity from my step functions workflow?

Viewed 889

I'd like to call an "activity" task from my workflow but have the activity ARN be defined by an external resource (for example, the activity ARN could come from the input, or it could come as the output of a lambda execution in other parts of the workflow.

However, the Resource property on tasks doesn't seem to accept the JsonPath substitution syntax. For example, for this task definition:

    "DynamicActivity":{
      "Type": "Task",
      "Resource.$": "$.activityArn",
      "Next":"Continue"
    },

I get errors immediately from the states parser in the console:

enter image description here

I'm aware that having a dynamic name can be done for lambdas, since with lambdas you are allowed to split the function name from the resource ARN, something like:

    "DynamicLambda": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": {
        "FunctionName.$": "$.functionName"
      },
      "Next": "Continue"
    }

Is there an equivalent syntax for specifying an activity name dynamically in Step Functions?

    "Resource": "arn:aws:states:::activity:???"
    "Parameters": {
      "ActivityName.$": "$.activityName"
    } 

If not, is there any other way I could start an activity dynamically without knowing its ARN or name statically? Can activities be started from a process other than the workflow itself (i.e. can a lambda function start an activity)?

1 Answers

[Edit] Assumption was not true, so the original answer is not applicable. The plausible solution now is:

  • While an activity is being created dynamically, a single node state machine with that activity task and arn will also be created.
  • The subsequent task will run that newly created state machine and use wait state with polling mechanism or async callback tasks that continue execution after the activity state machine is completed.
  • For both patterns we may read the output of activity state machine via SDK in the current state machine and continue with the flow.
  • When activity is being cleaned at some point, we may cleanup the activity state machine as well.

[Original Answer] Assumption: As the number and name of activities will always be finite and known. You can solve this problem by redesigning your workflow, such that, first state determines which activity is to be executed.

It would be accomplished by a choice state.

"ChoiceStateX": {
  "Type": "Choice",
  "Choices": [
    {
      "Variable": "$.activityName",
      "StringEquals": "myActivity",
      "Next": "myActivityState"
    }
  ],
  "Default": "DefaultState"
},

"myActivityState": {
  "Type" : "Task",
  "Resource": "arn:aws:states:us-east-1:123456789012:activity:activity-1",
  "End": true
},

"DefaultState": {
  "Type": "Fail",
  "Cause": "No Matches!"
}

It can be extended for any number of activities, and while running state machine via SDK or CLI, you will have to pass activity name. You may make it a part of current state machine, or have a different state machine as per the coupling requirements.

Related