AWS SWF Simple Workflow — A Demonstration Using AWS CLI

First What is a Workflow?

What is AWS SWF?

AWS CLI Based SWF Demonstration

dom=MyGreetingSWF
aws swf register-domain --name $dom --workflow-execution-retention-period-in-days 1
wf=DailyGreetingWorkflow
tl=GreetTaskList
aws swf register-workflow-type --domain $dom --name $wf --workflow-version "v1" --default-task-list name=$tl
at=ShowGreatingsActivityType
aws swf register-activity-type --domain $dom --name $at --activity-version av1 --default-task-list name=$tl
input=9
aws swf start-workflow-execution --domain $dom --workflow-id wf-id-01 --workflow-type name=$wf,version=v1 --task-list name=$tl --input $input --execution-start-to-close-timeout 600 --task-start-to-close-timeout 600 --child-policy ABANDON
{
"runId": "23f6U6YRO+9M0wmmEpULgEUE07W7eTkF8saZF2CpWrunQ="
}
runId="23f6U6YRO+9M0wmmEpULgEUE07W7eTkF8saZF2CpWrunQ="

Events in History

aws swf get-workflow-execution-history  --domain $dom --execution workflowId=wf-id-01,runId=$runId
{
"events": [
{
"eventTimestamp": 1542552218.283,
"eventType": "WorkflowExecutionStarted",
"eventId": 1,
"workflowExecutionStartedEventAttributes": {
"input": "9",
"executionStartToCloseTimeout": "600",
"taskStartToCloseTimeout": "600",
"childPolicy": "ABANDON",
"taskList": {
"name": "GreetTaskList"
},
"workflowType": {
"name": "DailyGreetingWorkflow",
"version": "v1"
},
"parentInitiatedEventId": 0
}
},
{
"eventTimestamp": 1542552218.283,
"eventType": "DecisionTaskScheduled",
"eventId": 2,
"decisionTaskScheduledEventAttributes": {
"taskList": {
"name": "GreetTaskList"
},
"startToCloseTimeout": "600"
}
}
]
}
aws swf poll-for-decision-task --domain $dom --task-list name=$tl{
"events": [
{
"eventTimestamp": 1542363855.6,
"eventType": "WorkflowExecutionStarted",
"eventId": 1,
"workflowExecutionStartedEventAttributes": {
"input": "9",
"executionStartToCloseTimeout": "600",
"taskStartToCloseTimeout": "600",
"childPolicy": "ABANDON",
"taskList": {
"name": "GreetTaskList"
},
"workflowType": {
"name": "DailyGreetingWorkflow",
"version": "v1"
},
"parentInitiatedEventId": 0
}
},
{
"eventTimestamp": 1542363855.6,
"eventType": "DecisionTaskScheduled",
"eventId": 2,
"decisionTaskScheduledEventAttributes": {
"taskList": {
"name": "GreetTaskList"
},
"startToCloseTimeout": "600"
}
},
{
"eventTimestamp": 1542363859.063,
"eventType": "DecisionTaskStarted",
"eventId": 3,
"decisionTaskStartedEventAttributes": {
"scheduledEventId": 2
}
}
],
"taskToken": "AAAAKgAAAAIAAAAAAAAAA3P11zBUr7GW6tcACtZHcMl9WsjCRO+aYyn908OXBQwsUa4TN0xNsuZxUeM1/etilQnZEmfc8kaDhhJuAw9wQCN38EddOn7H4FCE0lS4dz9An0FrSxbFO72dezZaFr4QSFELoqkWl05mhZObDlNV2WQgWJVdtoXHgWr16v1fPygT4qxMnpwcphCuvV6bLF8r4Lb8V9tEvAOFCdewduVxqrocZCx4wpJHUq8l6Cr/AtF/8IDYjpVlpazOlnKUN7bhrp41ERXuz/5G8dKgHg18shclTLPGiyrLKPF32ryatzmj",
"startedEventId": 3,
"workflowExecution": {
"workflowId": "wf-id-01",
"runId": "23f6U6YRO+9M0wmmEpULgEUE07W7eTkF8saZF2CpWrunQ="
},
"workflowType": {
"name": "DailyGreetingWorkflow",
"version": "v1"
},
"previousStartedEventId": 0
}
taskToken="AAAAKgAAAAIAAAAAAAAAA3P11zBUr7GW6tcACtZHcMl9WsjCRO+aYyn908OXBQwsUa4TN0xNsuZxUeM1/etilQnZEmfc8kaDhhJuAw9wQCN38EddOn7H4FCE0lS4dz9An0FrSxbFO72dezZaFr4QSFELoqkWl05mhZObDlNV2WQgWJVdtoXHgWr16v1fPygT4qxMnpwcphCuvV6bLF8r4Lb8V9tEvAOFCdewduVxqrocZCx4wpJHUq8l6Cr/AtF/8IDYjpVlpazOlnKUN7bhrp41ERXuz/5G8dKgHg18shclTLPGiyrLKPF32ryatzmj"
decInput=$(cat poll-for-decision-task.out.txt | jq ".events[0].workflowExecutionStartedEventAttributes.input"|sed 's/[",A-Z]//g')
if [ $decInput -lt 10 ]; then
inp="Good morning!"
elif [ $decInput -ge 10 -a $decInput -lt 5 ]; then
inp="Good afternoon!"
else
inp="Good evening!"
fi
cat > swf-rdtc.json <<EOT
{
"taskToken": "$taskToken",
"decisions": [
{
"decisionType": "ScheduleActivityTask",
"scheduleActivityTaskDecisionAttributes": {
"activityType": {
"name": "$at",
"version": "av1"
},
"activityId": "SreeACT-ID",
"control": "Sree Control",
"input": "$inp",
"scheduleToCloseTimeout": "600",
"taskList": {
"name": "$tl"
},
"scheduleToStartTimeout": "600",
"startToCloseTimeout": "600",
"heartbeatTimeout": "600"
}
}
]
}
EOT
aws swf respond-decision-task-completed --cli-input-json file://./swf-rdtc.json

Events in History

aws swf get-workflow-execution-history  --domain $dom --execution workflowId=wf-id-01,runId=$runId
{
"events": [
{
"eventTimestamp": 1542553244.86,
"eventType": "WorkflowExecutionStarted",
"eventId": 1,
"workflowExecutionStartedEventAttributes": {
"input": "9",
"executionStartToCloseTimeout": "900",
"taskStartToCloseTimeout": "900",
"childPolicy": "ABANDON",
"taskList": {
"name": "GreetTaskList"
},
"workflowType": {
"name": "DailyGreetingWorkflow",
"version": "v1"
},
"parentInitiatedEventId": 0
}
},
{
"eventTimestamp": 1542553244.86,
"eventType": "DecisionTaskScheduled",
"eventId": 2,
"decisionTaskScheduledEventAttributes": {
"taskList": {
"name": "GreetTaskList"
},
"startToCloseTimeout": "900"
}
},
{
"eventTimestamp": 1542553272.704,
"eventType": "DecisionTaskStarted",
"eventId": 3,
"decisionTaskStartedEventAttributes": {
"scheduledEventId": 2
}
},
{
"eventTimestamp": 1542553348.896,
"eventType": "DecisionTaskCompleted",
"eventId": 4,
"decisionTaskCompletedEventAttributes": {
"scheduledEventId": 2,
"startedEventId": 3
}
},
{
"eventTimestamp": 1542553348.896,
"eventType": "ActivityTaskScheduled",
"eventId": 5,
"activityTaskScheduledEventAttributes": {
"activityType": {
"name": "ShowGreatingsActivityType",
"version": "av1"
},
"activityId": "SreeACT-ID",
"input": "Good morning",
"control": "Sree Control",
"scheduleToStartTimeout": "600",
"scheduleToCloseTimeout": "600",
"startToCloseTimeout": "600",
"taskList": {
"name": "GreetTaskList"
},
"decisionTaskCompletedEventId": 4,
"heartbeatTimeout": "600"
}
}
]
}
aws swf poll-for-activity-task --domain $dom --task-list name=$tl{
"taskToken": "AAAAKgAAAAIAAAAAAAAAA6a9+JJ5FqXijFbnrb1JMLF7hV1Z8m5OGA5be3i6agq1yBui6Jyu0ydWLyBrTto3vxIFfJOpgtl6RQBPeof4ZdzVuM5KUU+A3ga1LrXaP8XT0uvTGDWsbwS98aTd2XFU0Dbi3Iyh0xriCIweADOHlEn11IkXW6MoCkxbvoTENRVOFMqQvc/vCKFOttyDooHaOLFbfTfo6T+q8rj9Dl4IYZzplReWfxr7ZGGraHBNaG/Y53uxoYqCn5W7sUy9R2ctV92VI3x9GW/OLLLblZOUv7JnMgqvFTtQI1TsKOkrqI43",
"activityId": "SreeACT-ID",
"startedEventId": 6,
"workflowExecution": {
"workflowId": "wf-id-01",
"runId": "23f6U6YRO+9M0wmmEpULgEUE07W7eTkF8saZF2CpWrunQ="
},
"activityType": {
"name": "ShowGreatingsActivityType",
"version": "av1"
},
"input": "Good morning!"
}
taskToken2="AAAAKgAAAAIAAAAAAAAAA6a9+JJ5FqXijFbnrb1JMLF7hV1Z8m5OGA5be3i6agq1yBui6Jyu0ydWLyBrTto3vxIFfJOpgtl6RQBPeof4ZdzVuM5KUU+A3ga1LrXaP8XT0uvTGDWsbwS98aTd2XFU0Dbi3Iyh0xriCIweADOHlEn11IkXW6MoCkxbvoTENRVOFMqQvc/vCKFOttyDooHaOLFbfTfo6T+q8rj9Dl4IYZzplReWfxr7ZGGraHBNaG/Y53uxoYqCn5W7sUy9R2ctV92VI3x9GW/OLLLblZOUv7JnMgqvFTtQI1TsKOkrqI43"

Events in History

aws swf get-workflow-execution-history  --domain $dom --execution workflowId=wf-id-01,runId=$runId
{
"events": [
{
"eventTimestamp": 1542553244.86,
"eventType": "WorkflowExecutionStarted",
"eventId": 1,
"workflowExecutionStartedEventAttributes": {
"input": "9",
"executionStartToCloseTimeout": "900",
"taskStartToCloseTimeout": "900",
"childPolicy": "ABANDON",
"taskList": {
"name": "GreetTaskList"
},
"workflowType": {
"name": "DailyGreetingWorkflow",
"version": "v1"
},
"parentInitiatedEventId": 0
}
},
{
"eventTimestamp": 1542553244.86,
"eventType": "DecisionTaskScheduled",
"eventId": 2,
"decisionTaskScheduledEventAttributes": {
"taskList": {
"name": "GreetTaskList"
},
"startToCloseTimeout": "900"
}
},
{
"eventTimestamp": 1542553272.704,
"eventType": "DecisionTaskStarted",
"eventId": 3,
"decisionTaskStartedEventAttributes": {
"scheduledEventId": 2
}
},
{
"eventTimestamp": 1542553348.896,
"eventType": "DecisionTaskCompleted",
"eventId": 4,
"decisionTaskCompletedEventAttributes": {
"scheduledEventId": 2,
"startedEventId": 3
}
},
{
"eventTimestamp": 1542553348.896,
"eventType": "ActivityTaskScheduled",
"eventId": 5,
"activityTaskScheduledEventAttributes": {
"activityType": {
"name": "ShowGreatingsActivityType",
"version": "av1"
},
"activityId": "SreeACT-ID",
"input": "Good morning",
"control": "Sree Control",
"scheduleToStartTimeout": "600",
"scheduleToCloseTimeout": "600",
"startToCloseTimeout": "600",
"taskList": {
"name": "GreetTaskList"
},
"decisionTaskCompletedEventId": 4,
"heartbeatTimeout": "600"
}
},
{
"eventTimestamp": 1542553476.907,
"eventType": "ActivityTaskStarted",
"eventId": 6,
"activityTaskStartedEventAttributes": {
"scheduledEventId": 5
}
}
]
}
aws swf respond-activity-task-completed --task-token=$taskToken2

Events in History

aws swf get-workflow-execution-history  --domain $dom --execution workflowId=wf-id-01,runId=$runId
{
"events": [
{
"eventTimestamp": 1542553244.86,
"eventType": "WorkflowExecutionStarted",
"eventId": 1,
"workflowExecutionStartedEventAttributes": {
"input": "9",
"executionStartToCloseTimeout": "900",
"taskStartToCloseTimeout": "900",
"childPolicy": "ABANDON",
"taskList": {
"name": "GreetTaskList"
},
"workflowType": {
"name": "DailyGreetingWorkflow",
"version": "v1"
},
"parentInitiatedEventId": 0
}
},
{
"eventTimestamp": 1542553244.86,
"eventType": "DecisionTaskScheduled",
"eventId": 2,
"decisionTaskScheduledEventAttributes": {
"taskList": {
"name": "GreetTaskList"
},
"startToCloseTimeout": "900"
}
},
{
"eventTimestamp": 1542553272.704,
"eventType": "DecisionTaskStarted",
"eventId": 3,
"decisionTaskStartedEventAttributes": {
"scheduledEventId": 2
}
},
{
"eventTimestamp": 1542553348.896,
"eventType": "DecisionTaskCompleted",
"eventId": 4,
"decisionTaskCompletedEventAttributes": {
"scheduledEventId": 2,
"startedEventId": 3
}
},
{
"eventTimestamp": 1542553348.896,
"eventType": "ActivityTaskScheduled",
"eventId": 5,
"activityTaskScheduledEventAttributes": {
"activityType": {
"name": "ShowGreatingsActivityType",
"version": "av1"
},
"activityId": "SreeACT-ID",
"input": "Good morning",
"control": "Sree Control",
"scheduleToStartTimeout": "600",
"scheduleToCloseTimeout": "600",
"startToCloseTimeout": "600",
"taskList": {
"name": "GreetTaskList"
},
"decisionTaskCompletedEventId": 4,
"heartbeatTimeout": "600"
}
},
{
"eventTimestamp": 1542553476.907,
"eventType": "ActivityTaskStarted",
"eventId": 6,
"activityTaskStartedEventAttributes": {
"scheduledEventId": 5
}
},
{
"eventTimestamp": 1542553645.283,
"eventType": "ActivityTaskCompleted",
"eventId": 7,
"activityTaskCompletedEventAttributes": {
"scheduledEventId": 5,
"startedEventId": 6
}
},
{
"eventTimestamp": 1542553645.283,
"eventType": "DecisionTaskScheduled",
"eventId": 8,
"decisionTaskScheduledEventAttributes": {
"taskList": {
"name": "GreetTaskList"
},
"startToCloseTimeout": "900"
}
}
]
}
aws swf terminate-workflow-execution --domain $dom --workflow-id wf-id-01aws swf deprecate-activity-type --domain $dom --activity-type name=$at,version=av1aws swf deprecate-workflow-type --domain $dom --workflow-type name=$wf,version=v1aws swf deprecate-domain --name $domaws swf list-domains --registration-status REGISTERED
{
"domainInfos": []
}
aws swf list-workflow-types --domain $dom --registration-status REGISTERED
{
"typeInfos": []
}
aws swf list-activity-types --domain $dom --registration-status REGISTERED
{
"typeInfos": []
}

Sreeprakash Neelakantan

Written by

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade