[{"data":1,"prerenderedAt":98},["ShallowReactive",2],{"content-svc-step-functions-guides-integrating-step-functions-with-containerized-workloads-60-callback":3},{"markdown":4,"frontMatterAttributes":5,"bodyRaw":16,"menu":17,"menuType":95,"isCollapseableMenu":20,"nextDocItem":74,"previousDocItem":54,"contributorPaths":96,"contentName":19,"slug":73,"readingTime":37},"\u003Ch4>Invoking Amazon ECS task using Wait for Callback integration\u003C\u002Fh4>\n\n\n\u003Cp>You can invoke ECS RunTask API using \u003Ccode>Wait for a Callback\u003C\u002Fcode> integration pattern. With this pattern, you send a task token to the ECS task, and then send the token back from ECS task to your workflow using the Step Functions API. EKS integration does not allow running an EKS job with callback token.\u003C\u002Fp>\n\u003Cp>\u003Cimg src=\"\u002Fassets\u002Fexternal\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002Fassets\u002FRuntaskwithtoken.png\" alt=\"image\">\u003C\u002Fp>\n\u003Cp>The Amazon States language (ASL) for \u003Ccode>Wait for a Callback\u003C\u002Fcode> pattern is similar to the \u003Ccode>Run a Job \u003C\u002Fcode>integration except the resource attribute has .waitForTaskToken and task token is sent to the ECS task using the \u003Cstrong>Environment\u003C\u002Fstrong> attribute. \u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-json\">&quot;Environment&quot;:[  \n  {  \n      &quot;Name&quot;:&quot;TASK_TOKEN_ENV_VARIABLE&quot;,\n      &quot;Value.$&quot;:&quot;$$.Task.Token&quot;\n  }\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>The Step Functions state invokes ECS task and pauses until the task token is returned. ECS task completes the business operation, returns the task token back to Step Functions by either sending success or failure along with any response.\u003C\u002Fp>\n\u003Cpre>\u003Ccode class=\"language-python\">client = boto3.client(&#39;stepfunctions&#39;)\ntask_token = os.environ[&#39;TASK_TOKEN_ENV_VARIABLE&#39;]\n\nif success:\n  client.send_task_success(\n    taskToken = task_token,\n    output = stringoutput\n  )\nelse:\n  client.send_task_failure(\n    taskToken = task_token,\n    cause = strCause,\n    error = strError\n  )\n\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cp>This integration pattern offers couple of advantages over \u003Ccode>Run a job\u003C\u002Fcode> integration.\u003C\u002Fp>\n\u003Cul>\n\u003Cli>You can send response from Amazon ECS task to Step Functions workflow as part of sending the token back.\u003C\u002Fli>\n\u003Cli>Wait for a Callback integration avoids DescribeTasks API throttles at high volume as there is no constant polling from Step Functions to get the status of ECS task. The status is returned to Step Functions using the Step Functions API.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>However, because task token has to be sent back to Step Functions, ECS task requires permissions to invoke the Step Functions APIs and custom code has to be written in the ECS task to send the token back. You can try out the \u003Ccode>Wait for a callback\u003C\u002Fcode> integration using the \u003Ca href=\"https:\u002F\u002Fserverlessland.com\u002Fpatterns\u002Fsfn-ecs-sam\">Serverless land pattern\u003C\u002Fa>.\u003C\u002Fp>\n",{"title":6,"order":7,"callout":8,"resources":11},"Wait for Callback integration",60,{"title":9,"description":9,"link":10},"Parallel video processing using AWS Fargate tasks","https:\u002F\u002Fcatalog.workshops.aws\u002Fstepfunctions\u002Fen-US\u002Fuse-cases\u002Fdata-processing\u002Fmap-ecs",[12],{"link":13,"text":14,"type":15},"https:\u002F\u002Fserverlessland.com\u002Fpatterns\u002Fsfn-ecs-sam","Sample pattern for Wait for Callback","pattern","\u003Ch4>Invoking Amazon ECS task using Wait for Callback integration\u003C\u002Fh4>\n\n\nYou can invoke ECS RunTask API using `Wait for a Callback` integration pattern. With this pattern, you send a task token to the ECS task, and then send the token back from ECS task to your workflow using the Step Functions API. EKS integration does not allow running an EKS job with callback token.\n\n![image](\u002Fassets\u002Fexternal\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002Fassets\u002FRuntaskwithtoken.png)\n\nThe Amazon States language (ASL) for `Wait for a Callback` pattern is similar to the `Run a Job `integration except the resource attribute has .waitForTaskToken and task token is sent to the ECS task using the **Environment** attribute. \n```json\n\"Environment\":[  \n  {  \n      \"Name\":\"TASK_TOKEN_ENV_VARIABLE\",\n      \"Value.$\":\"$$.Task.Token\"\n  }\n\n```\nThe Step Functions state invokes ECS task and pauses until the task token is returned. ECS task completes the business operation, returns the task token back to Step Functions by either sending success or failure along with any response.\n\n```python\nclient = boto3.client('stepfunctions')\ntask_token = os.environ['TASK_TOKEN_ENV_VARIABLE']\n\nif success:\n  client.send_task_success(\n    taskToken = task_token,\n    output = stringoutput\n  )\nelse:\n  client.send_task_failure(\n    taskToken = task_token,\n    cause = strCause,\n    error = strError\n  )\n\n\n```\nThis integration pattern offers couple of advantages over `Run a job` integration.\n\n* You can send response from Amazon ECS task to Step Functions workflow as part of sending the token back.\n* Wait for a Callback integration avoids DescribeTasks API throttles at high volume as there is no constant polling from Step Functions to get the status of ECS task. The status is returned to Step Functions using the Step Functions API.\n\nHowever, because task token has to be sent back to Step Functions, ECS task requires permissions to invoke the Step Functions APIs and custom code has to be written in the ECS task to send the token back. You can try out the `Wait for a callback` integration using the [Serverless land pattern](https:\u002F\u002Fserverlessland.com\u002Fpatterns\u002Fsfn-ecs-sam).\n\n",[18],{"title":19,"collapsible":20,"isCollapsed":20,"content":21},"Integrating Step Functions with containerized workloads",false,[22,29,41,54,67,74,90],{"title":23,"order":24,"time":25,"path":26,"id":27,"link":28},"Introduction",10,"1 min","10-introduction","10-introduction.md","\u002Fcontent\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002F10-introduction",{"title":30,"order":31,"resources":32,"time":37,"path":38,"id":39,"link":40},"Service Integration patterns and AWS service integrations ",20,[33],{"link":34,"text":35,"type":36},"https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fconnect-to-resource.html","Service integration patterns","documentation","2 min","20-patterns","20-patterns.md","\u002Fcontent\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002F20-patterns",{"title":42,"order":43,"resources":44,"time":25,"path":51,"id":52,"link":53},"Connecting to systems outside AWS",40,[45,48],{"link":46,"text":47,"type":36},"https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fconnect-third-party-apis.html","Third party API integration",{"link":49,"text":50,"type":36},"https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fconcepts-activities.html","Activity Integration","40-external-system-integration","40-external-system-integration.md","\u002Fcontent\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002F40-external-system-integration",{"title":55,"order":56,"resources":57,"time":37,"path":64,"id":65,"link":66},"Run a job Integration",50,[58,61],{"link":59,"text":60,"type":36},"https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fconnect-ecs.html","Manage Amazon ECS or Fargate Tasks with Step Functions",{"link":62,"text":63,"type":36},"https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fconnect-eks.html","Call Amazon EKS with Step Functions","50-run-a-job","50-run-a-job.md","\u002Fcontent\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002F50-run-a-job",{"title":6,"order":7,"callout":68,"resources":69,"time":37,"path":71,"id":72,"link":73},{"title":9,"description":9,"link":10},[70],{"link":13,"text":14,"type":15},"60-callback","60-callback.md","\u002Fcontent\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002F60-callback",{"title":50,"order":75,"resources":76,"callout":83,"time":37,"path":87,"id":88,"link":89},70,[77,79],{"link":49,"text":78,"type":36},"Activities",{"link":80,"text":81,"type":82},"https:\u002F\u002Fcatalog.us-east-1.prod.workshops.aws\u002Fworkshops\u002F2a22e604-2f2e-4d7b-85a8-33b38c999234\u002Fen-US\u002Fuse-cases\u002Fmonte-carlo-simulation","Run Monte Carlo Simulation with AWS Fargate and Step Functions Activity","workshop",{"title":84,"description":85,"link":86},"Using Activities with AWS Fargate","This blog post demostrates how you can use Activities with AWS Fargate at scale.","https:\u002F\u002Faws.amazon.com\u002Fblogs\u002Fcontainers\u002Frun-monte-carlo-simulations-at-scale-with-aws-step-functions-and-aws-fargate\u002F","70-activity","70-activity.md","\u002Fcontent\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002F70-activity",{"title":91,"order":24,"time":25,"path":92,"id":93,"link":94},"Conclusion","80-conclusion","80-conclusion.md","\u002Fcontent\u002Fservice\u002Fstep-functions\u002Fguides\u002Fintegrating-step-functions-with-containerized-workloads\u002F80-conclusion","LIST",[97],"content\u002Fcontributors\u002Fuma-ramadoss.json",1792578874827]