/startup-snack-step-functions-fargate-tasks

Step Functions with Fargate Tasks

Primary LanguageTypeScriptApache License 2.0Apache-2.0

Step Functions with Fargate Tasks

This project deploys a State Machine using AWS Step Functions, AWS Lambda and AWS Fargate backed Amazon Elastic Container Service (ECS) via AWS Cloud Development Kit (CDK).

Setup

  1. Install CDK globally: npm install -g aws-cdk
  2. Install local Node.js dependencies: npm install
  3. Build the project: npm run build
  4. Bootstrap the CDK Toolkit into your AWS account: cdk bootstrap
  5. Deploy the stack: cdk deploy

Useful Commands

  • npm run build compile project to dist
  • npm run clean delete everything in cdk.out and dist
  • npm run watch watch for changes and compile
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template

State Machine Description:

(see image below for reference):

  1. You can optionally supply an input of {"processor": "pi|euler|both|none"}.
  2. The LambdaTask will take that input, and invoke a Lambda task synchronously that will output {"processor": VALUE, "digits": N} with randomly selected digits and processor (unless you supplied one in step 1).
  3. The DecisionStep is a Choice and based on the value of processor, it will move the state machine to the corresponding step.
    1. If both is selected, it will run two pass tasks called staticPiTask and staticEulerTask in parallel.
    2. If euler is selected, it will run an ECS task synchronously using Fargate and wait until the task completes.
    3. If pi is selected, it will run an ECS task using Fargate as well, but this time it will wait until the task responds to the Step Functions API with the corresponding TaskToken.
    4. Otherwise, it will just continue onto the next step.
  4. The FinishingStep is another pass step just to show how to bring all of the different pathways back together.

Notes:

  • The reason for running an ECS Task within a state machine with the TaskToken, instead of synchronously, is that it allows you to return data to the state machine. The synchronous process does not let you do this when running ECS Tasks.
  • When running the state machine, click on the PiFargateTask and compare the step output to the EulerFargateTask to see the difference in outputs.
    • Euler
    • Pi