ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Step Functions က AWS က သင့်ကိုယ်စား run ပေးတဲ့ flowchart တစ်ခုပါ။ Box တစ်ခုချင်းစီက state တစ်ခုဖြစ်ပြီး 'Lambda ကို ခေါ်'၊ 'value ကို စစ်ပြီး လမ်းခွဲ'၊ 'တစ်နာရီ စောင့်'၊ 'ဒီ item တွေကို parallel လုပ်' စတာတွေ လုပ်ပါတယ်။ Flowchart ကိုယ်တိုင်က JSON ဖြစ်ပြီး Amazon States Language (ASL) လို့ ခေါ်ပါတယ်။
အရေးကြီးတာက Step Functions က ဘယ်အဆင့် ရောက်နေလဲ ဆိုတာကို မှတ်ထားပေးတာပါ။ Step တစ်ခု ကျရှုံးရင် သတ်မှတ်ထားတဲ့အတိုင်း ပြန်ကြိုးစားပေး၊ ရက်ပေါင်းများစွာ စောင့်ပေး၊ console မှာ execution တစ်ခုချင်းစီ ဘယ်လမ်းကြောင်း သွားခဲ့လဲ ပုံနဲ့ ပြပေးပါတယ်။ Lambda တွေက business logic ကိုပဲ အာရုံစိုက်ပြီး 'နောက်ဘာလုပ်ရမလဲ' ဆိုတာကို workflow က ကိုင်တွယ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: Lambda တွေကို ကွင်းဆက်ချိတ်တာ
Order တစ်ခုအတွက် stock reserve လုပ်၊ card ကို charge လုပ်၊ shipment ဖန်တီးရမယ်ဆိုပါစို့။ အလွယ်ဆုံးနည်းက Lambda တစ်ခုက နောက်တစ်ခုကို ခေါ်တာပါ။ ဒါပေမယ့် shipping ကျရှုံးသွားရင် card ကို charge ပြီးသား၊ stock ကို reserve ပြီးသား ဖြစ်နေပါပြီ — ဘယ်သူက ပြန်ဖျက်မလဲ။
Retry, timeout, error branching တွေ အားလုံးကို code ထဲမှာ ကိုယ်တိုင် ရေးရပါတယ်။ ပထမ Lambda က နောက်ဆုံး Lambda ပြီးတဲ့အထိ စောင့်နေရလို့ အလုပ်မလုပ်ဘဲ စောင့်နေတဲ့ အချိန်အတွက် ပေးရပြီး Lambda ရဲ့ 15 မိနစ် ကန့်သတ်ချက်ကိုလည်း ထိနိုင်ပါတယ်။ Execution တစ်ခု ဘယ်နေရာမှာ ရပ်သွားလဲ သိဖို့ log group သုံးခုကို လိုက်ရှာရပါတယ်။
import json
import boto3
lambda_client = boto3.client("lambda")
def handler(event, context):
# Orchestration logic is buried inside business code.
stock = lambda_client.invoke(
FunctionName="reserve-inventory", Payload=json.dumps(event).encode()
)
charge = lambda_client.invoke(
FunctionName="charge-payment", Payload=stock["Payload"].read()
)
# If this fails: no retry, no refund, no stock release, no visibility.
return lambda_client.invoke(
FunctionName="create-shipment", Payload=charge["Payload"].read()
)ဘာကြောင့် ပြဿနာ ဖြစ်တာလဲ
Orchestration (အစဉ်၊ retry၊ error handling၊ undo) က business logic ထဲ ရောနေပြီး state ကို ဘယ်နေရာမှာမှ မသိမ်းထားပါဘူး။ တစ်ဝက်တစ်ပျက် ပြီးသွားတဲ့ order တွေက inconsistent data အဖြစ် ကျန်ခဲ့ပါတယ်။
အဓိက သဘောတရား: Standard, Express နဲ့ ASL
State machine တစ်ခု ဆောက်တဲ့အခါ type နှစ်မျိုးထဲက တစ်ခု ရွေးရပြီး နောက်မှ ပြောင်းလို့ မရပါဘူး။ Standard workflow တွေက တစ်နှစ်အထိ run နိုင်ပြီး step တစ်ခုချင်းစီကို exactly-once execute လုပ်ပါတယ် (retry သတ်မှတ်ထားရင်တော့ ပြန်လုပ်ပါမယ်)၊ execution history အပြည့်အစုံကို console မှာ ကြည့်လို့ရပါတယ်။ Express workflow တွေကတော့ အများဆုံး ၅ မိနစ်ပဲ run နိုင်ပြီး တစ်စက္ကန့်မှာ execution အများကြီး စတင်နိုင်အောင် ဒီဇိုင်းထားကာ history ကို CloudWatch Logs ထဲ ပို့ပါတယ်။
| Standard | Express | |
|---|---|---|
| အများဆုံး ကြာချိန် | ၁ နှစ် | ၅ မိနစ် |
| Semantics | Exactly-once workflow execution | At-least-once (async), at-most-once (sync) |
| ဈေးနှုန်း | State transition အလိုက် | Request အရေအတွက် + duration + memory |
| .sync / waitForTaskToken | ရ | မရ |
ASL document တစ်ခုမှာ StartAt (စမယ့် state နာမည်) နဲ့ States (state တွေရဲ့ map) ပါပါတယ်။ Task က အလုပ်လုပ်တယ် (Lambda ခေါ်တာ၊ AWS API ခေါ်တာ)၊ Choice က input ကို စစ်ပြီး လမ်းခွဲတယ်၊ Parallel က branch တွေကို တစ်ပြိုင်နက် run တယ်၊ Map က array ထဲက item တိုင်းအတွက် step တွေ run တယ်၊ Wait က အချိန်တစ်ခု ဒါမှမဟုတ် timestamp တစ်ခုအထိ ရပ်စောင့်တယ်၊ Pass က data ကို ပြောင်းပြီး ရှေ့ဆက်ပို့တယ်၊ Succeed နဲ့ Fail က execution ကို အဆုံးသတ်ပါတယ်။
Data က state တစ်ခုကနေ နောက်တစ်ခုကို JSON document အဖြစ် စီးဆင်းပါတယ်။ Path field တွေက ဒါကို ထိန်းပါတယ်: InputPath က input ထဲက ဘယ်အပိုင်းကို ယူမလဲ၊ Parameters က task ကို ပို့မယ့် payload ကို ဆောက်ပေးတယ်၊ ResultSelector က task ရလဒ်ကို ဖြတ်ညှိတယ်၊ ResultPath က ရလဒ်ကို input ထဲ ဘယ်နေရာ ထည့်မလဲ ဆုံးဖြတ်တယ်၊ OutputPath က နောက် state ကို ဘာပို့မလဲ ရွေးပါတယ်။ 2024 နှောင်းပိုင်းကစပြီး ဒီ path တွေအစား JSONata expression တွေကိုလည်း opt-in လုပ်ပြီး သုံးနိုင်ပါတယ်၊ ဒါပေမယ့် ဒီ lesson မှာ အသုံးအများဆုံး JSONPath style ကို သုံးပါမယ်။
Step Functions မှာ error တွေကို string နာမည်နဲ့ ခွဲခြားပါတယ်။ Built-in နာမည်တွေက States. နဲ့ စပါတယ် — States.ALL (error အားလုံး)၊ States.Timeout၊ States.TaskFailed စတာတွေပါ။ Lambda ကနေ PaymentDeclined လို custom exception class တစ်ခု raise လုပ်ရင် အဲဒီ class နာမည်က error နာမည် ဖြစ်လာလို့ Retry နဲ့ Catch မှာ business error နဲ့ ခေတ္တ ဖြစ်တဲ့ error ကို ခွဲပြီး ကိုင်တွယ်နိုင်ပါတယ်။
DATA FLOW THROUGH ONE TASK STATE
--------------------------------
state input {"orderId": "1042", "total": 1500}
|
v InputPath / Parameters -> payload sent to the task
+-----------------+
| Task (Lambda) | -> raw result {"Payload": {"chargeId": "ch_9"}}
+-----------------+
| ResultSelector -> {"chargeId": "ch_9"}
| ResultPath "$.payment" -> merged into the original input
v OutputPath
{"orderId": "1042", "total": 1500, "payment": {"chargeId": "ch_9"}}- JSON input ဝင်လာ
- State က အလုပ်လုပ်ပြီး ရလဒ်ထည့်
- Retry/Catch က error ကို ကိုင်တွယ်
- Next က နောက် state ကို ရွေး
ResultPath ကို မသတ်မှတ်ရင် task ရလဒ်က input တစ်ခုလုံးကို အစားထိုးသွားပါတယ် — orderId လို field တွေ ပျောက်သွားတတ်တာ ဒါကြောင့်ပါ။
အနည်းဆုံး ASL ပုံစံ
{
"Comment": "Wait, then branch on a flag",
"StartAt": "WaitABit",
"States": {
"WaitABit": { "Type": "Wait", "Seconds": 10, "Next": "IsPriority" },
"IsPriority": {
"Type": "Choice",
"Choices": [
{ "Variable": "$.priority", "BooleanEquals": true, "Next": "Done" }
],
"Default": "Tag"
},
"Tag": {
"Type": "Pass",
"Result": "standard",
"ResultPath": "$.lane",
"Next": "Done"
},
"Done": { "Type": "Succeed" }
}
}End မဟုတ်တဲ့ state တိုင်းမှာ Next ပါရပါတယ် (ဒါမှမဟုတ် "End": true)။ Choice state မှာ Next မပါဘဲ Choices နဲ့ Default ပါပါတယ်။ Succeed နဲ့ Fail က terminal state တွေဖြစ်လို့ Next မလိုပါဘူး။
လက်တွေ့ ဥပမာ: order saga state machine
ဒီ Standard workflow က stock reserve လုပ်၊ card charge လုပ်၊ $1000 နဲ့ အထက် order တွေအတွက် လူက approve လုပ်တာကို စောင့်ပြီး ship လုပ်ပါတယ်။ Step တစ်ခု ကျရှုံးရင် ပြီးသွားတဲ့ step တွေကို ပြောင်းပြန် အစဉ်နဲ့ undo လုပ်ပါတယ် (refund၊ ပြီးရင် stock release)။ ဒါက saga pattern ပါ — distributed transaction မရှိတဲ့ နေရာမှာ compensation step တွေနဲ့ consistency ကို ပြန်ရအောင် လုပ်တာပါ။
{
"Comment": "Order saga: reserve, charge, approve, ship; compensate on failure",
"StartAt": "ReserveInventory",
"States": {
"ReserveInventory": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "reserve-inventory", "Payload.$": "$" },
"ResultSelector": { "reservationId.$": "$.Payload.reservationId" },
"ResultPath": "$.reservation",
"Retry": [
{
"ErrorEquals": ["Lambda.TooManyRequestsException", "Lambda.ServiceException"],
"IntervalSeconds": 2,
"MaxAttempts": 3,
"BackoffRate": 2.0,
"JitterStrategy": "FULL"
}
],
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"ResultPath": "$.error",
"Next": "OrderFailed"
}
],
"Next": "ChargePayment"
},
"ChargePayment": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "charge-payment", "Payload.$": "$" },
"ResultSelector": { "chargeId.$": "$.Payload.chargeId" },
"ResultPath": "$.payment",
"Retry": [
{
"ErrorEquals": ["PaymentProviderUnavailable"],
"IntervalSeconds": 5,
"MaxAttempts": 4,
"BackoffRate": 2.0,
"MaxDelaySeconds": 30
}
],
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"ResultPath": "$.error",
"Next": "ReleaseInventory"
}
],
"Next": "NeedsApproval"
},
"NeedsApproval": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.total",
"NumericGreaterThanEquals": 1000,
"Next": "WaitForApproval"
}
],
"Default": "ShipOrder"
},
"WaitForApproval": {
"Type": "Task",
"Resource": "arn:aws:states:::sqs:sendMessage.waitForTaskToken",
"Parameters": {
"QueueUrl": "https://sqs.us-east-1.amazonaws.com/111122223333/order-approvals",
"MessageBody": { "orderId.$": "$.orderId", "taskToken.$": "$$.Task.Token" }
},
"ResultPath": "$.approval",
"TimeoutSeconds": 86400,
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"ResultPath": "$.error",
"Next": "RefundPayment"
}
],
"Next": "ShipOrder"
},
"ShipOrder": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "create-shipment", "Payload.$": "$" },
"ResultSelector": { "trackingId.$": "$.Payload.trackingId" },
"ResultPath": "$.shipment",
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"ResultPath": "$.error",
"Next": "RefundPayment"
}
],
"Next": "OrderComplete"
},
"RefundPayment": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "refund-payment", "Payload.$": "$" },
"ResultPath": null,
"Retry": [
{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 5,
"MaxAttempts": 5,
"BackoffRate": 2.0
}
],
"Next": "ReleaseInventory"
},
"ReleaseInventory": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "release-inventory", "Payload.$": "$" },
"ResultPath": null,
"Retry": [
{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 5,
"MaxAttempts": 5,
"BackoffRate": 2.0
}
],
"Next": "OrderFailed"
},
"OrderFailed": {
"Type": "Fail",
"Error": "OrderFailed",
"Cause": "A step failed and the completed steps were compensated"
},
"OrderComplete": { "Type": "Succeed" }
}
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Retry
"ErrorEquals": ["Lambda.TooManyRequestsException", "Lambda.ServiceException"]Throttling နဲ့ ခေတ္တ ဖြစ်တဲ့ Lambda service error တွေကိုပဲ retry လုပ်ပါတယ်။ Interval က 2, 4, 8 စက္ကန့် ဖြစ်သွားပြီး JitterStrategy FULL က execution အများကြီး တစ်ပြိုင်နက် ပြန်ကြိုးစားတာကို ဖြန့်ပေးပါတယ်။ Business error (card declined) ကို retry မလုပ်ပါနဲ့ — ဘယ်နှခါ ကြိုးစားကြိုးစား ရလဒ် တူတူပါပဲ။
- Catch + ResultPath
{ "ErrorEquals": ["States.ALL"], "ResultPath": "$.error", "Next": "ReleaseInventory" }Retry ကုန်သွားရင် ဒါမှမဟုတ် retry မသတ်မှတ်ထားတဲ့ error ဖြစ်ရင် Catch က compensation step ဆီ လမ်းလွှဲပါတယ်။ ResultPath "$.error" က error ကို input ထဲ ထပ်ထည့်ပေးလို့ ReleaseInventory က reservationId ကို ဆက်မြင်ရပါတယ်။
- waitForTaskToken
"taskToken.$": "$$.Task.Token"$$ က context object ပါ။ Task token ကို SQS message ထဲ ထည့်ပို့ပြီး execution က pause ဖြစ်နေပါတယ်။ Approval app က SendTaskSuccess ဒါမှမဟုတ် SendTaskFailure ကို ဒီ token နဲ့ ခေါ်မှ ဆက်သွားပြီး 24 နာရီ (TimeoutSeconds) ကျော်ရင် States.Timeout ဖြစ်ပါတယ်။
- Compensation
"ResultPath": nullRefund နဲ့ release step တွေက ရလဒ်ကို ပစ်လိုက်ပြီး input ကို မပြောင်းဘဲ ရှေ့ဆက်ပို့ပါတယ်။ ဒီ step တွေမှာ retry ပိုများပါတယ် — compensation ကျရှုံးတာက အဆိုးဆုံး အခြေအနေမို့ပါ။
Sample execution ($1,500 order၊ card declined):
ReserveInventory -> succeeded
ChargePayment -> failed (PaymentDeclined), caught
ReleaseInventory -> succeeded
OrderFailed -> execution status FAILED- ReserveInventory
- ChargePayment
- NeedsApproval (Choice)
- ShipOrder
အဆင့်တိုင်း အောင်မြင်
OrderComplete
Ship ကျရှုံး
Refund -> Release -> OrderFailed
Service integration သုံးမျိုး
| Pattern | Resource ပုံစံ | ဘာကို စောင့်လဲ |
|---|---|---|
| Request-response | arn:aws:states:::sns:publish | API call ပြန်လာတာကိုပဲ |
| Run a job (.sync) | arn:aws:states:::ecs:runTask.sync | Job ပြီးဆုံးတဲ့အထိ |
| Callback | ...sqs:sendMessage.waitForTaskToken | Token နဲ့ SendTaskSuccess ခေါ်တဲ့အထိ |
Request-response က API ကို ခေါ်ပြီး ချက်ချင်း ရှေ့ဆက်ပါတယ်။ .sync က ECS task, Glue job, CodeBuild build ဒါမှမဟုတ် child state machine တစ်ခု (states:startExecution.sync:2) စတဲ့ job တစ်ခုကို စပြီး ပြီးဆုံးတဲ့အထိ Step Functions က ကိုယ်တိုင် poll လုပ်ပေးလို့ polling loop ရေးစရာ မလိုပါဘူး။ Callback pattern ကတော့ လူ ဒါမှမဟုတ် ပြင်ပ system ရဲ့ အဖြေကို စောင့်ဖို့ပါ၊ HeartbeatSeconds သတ်မှတ်ထားရင် worker က SendTaskHeartbeat နဲ့ အသက်ရှင်နေကြောင်း ပြရပါတယ်။
aws stepfunctions create-state-machine --name order-saga --type STANDARD \
--definition file://order-saga.asl.json \
--role-arn arn:aws:iam::111122223333:role/order-saga-sfn
SM_ARN=$(aws stepfunctions list-state-machines \
--query "stateMachines[?name=='order-saga'].stateMachineArn" --output text)
aws stepfunctions start-execution --state-machine-arn "$SM_ARN" \
--name order-1042 --input '{"orderId": "1042", "total": 1500}'
# The approval app resumes the paused execution with the token it received
aws stepfunctions send-task-success --task-token "$TASK_TOKEN" \
--task-output '{"approved": true, "approver": "ops-lead"}'Parallel, Map နဲ့ CDK နဲ့ ရေးတာ
Parallel state က branch တစ်ခုချင်းစီကို သီးခြား sub-workflow အဖြစ် တစ်ပြိုင်နက် run ပြီး ရလဒ်တွေကို array တစ်ခုအဖြစ် ပြန်ပေးပါတယ်။ Branch တစ်ခု ကျရှုံးရင် Parallel တစ်ခုလုံး ကျရှုံးပါတယ်။ Map state က array ထဲက item တိုင်းအတွက် ItemProcessor ကို run ပြီး MaxConcurrency နဲ့ တစ်ပြိုင်နက် run တဲ့ အရေအတွက်ကို ကန့်သတ်နိုင်ပါတယ်။ S3 ထဲက object သန်းချီလို dataset ကြီးတွေအတွက် Distributed Map mode က child execution တွေ အများကြီးကို parallel run ပေးပါတယ်။
{
"StartAt": "Enrich",
"States": {
"Enrich": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "FraudScore",
"States": {
"FraudScore": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": { "FunctionName": "fraud-score", "Payload.$": "$" },
"OutputPath": "$.Payload",
"End": true
}
}
},
{
"StartAt": "LoyaltyTier",
"States": {
"LoyaltyTier": { "Type": "Pass", "Result": { "tier": "gold" }, "End": true }
}
}
],
"ResultPath": "$.enrichment",
"Next": "NotifyEachItem"
},
"NotifyEachItem": {
"Type": "Map",
"ItemsPath": "$.items",
"MaxConcurrency": 5,
"ItemProcessor": {
"ProcessorConfig": { "Mode": "INLINE" },
"StartAt": "Notify",
"States": {
"Notify": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:aws:sns:us-east-1:111122223333:item-updates",
"Message.$": "States.JsonToString($)"
},
"End": true
}
}
},
"ResultPath": null,
"End": true
}
}
}Production မှာ ASL ကို လက်နဲ့ ရေးတာထက် CDK နဲ့ ရေးလေ့ ရှိပါတယ်။ Construct တွေက Lambda ARN၊ IAM permission နဲ့ state နာမည်တွေကို type-safe ဖြစ်အောင် ချိတ်ပေးလို့ invoke permission မေ့တာ၊ state နာမည် စာလုံးပေါင်း မှားတာ စတဲ့ အမှားတွေ လျော့သွားပါတယ်။
import { App, Duration, Stack } from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as sfn from "aws-cdk-lib/aws-stepfunctions";
import * as tasks from "aws-cdk-lib/aws-stepfunctions-tasks";
const app = new App();
const stack = new Stack(app, "OrderSagaStack");
const step = (id: string, resultPath?: string): tasks.LambdaInvoke =>
new tasks.LambdaInvoke(stack, id, {
lambdaFunction: new lambda.Function(stack, id + "Fn", {
runtime: lambda.Runtime.PYTHON_3_12,
handler: "index.handler",
code: lambda.Code.fromInline("def handler(event, context):\n return event\n"),
}),
payloadResponseOnly: true,
resultPath: resultPath ?? sfn.JsonPath.DISCARD,
});
const orderFailed = new sfn.Fail(stack, "OrderFailed", { error: "OrderFailed" });
const release = step("ReleaseInventory").next(orderFailed);
const refund = step("RefundPayment").next(release);
const reserve = step("ReserveInventory", "$.reservation");
reserve.addCatch(orderFailed, { resultPath: "$.error" });
const charge = step("ChargePayment", "$.payment");
charge.addRetry({
errors: ["PaymentProviderUnavailable"],
interval: Duration.seconds(5),
maxAttempts: 4,
backoffRate: 2,
});
charge.addCatch(release, { resultPath: "$.error" });
const ship = step("ShipOrder", "$.shipment");
ship.addCatch(refund, { resultPath: "$.error" });
new sfn.StateMachine(stack, "OrderSaga", {
stateMachineType: sfn.StateMachineType.STANDARD,
timeout: Duration.days(2),
definitionBody: sfn.DefinitionBody.fromChainable(
reserve.next(charge).next(ship).next(new sfn.Succeed(stack, "OrderComplete")),
),
});
app.synth();Step Functions က ဘယ်အချိန် ပိုကောင်းလဲ (ဘယ်အချိန် မလိုလဲ)
Step သုံးခုနဲ့ အထက်၊ branching ရှိတာ၊ retry policy မတူတာ၊ compensation လိုတာ၊ မိနစ်ပိုင်းကနေ ရက်ပိုင်းအထိ စောင့်ရတာ ဒါမှမဟုတ် audit အတွက် execution တစ်ခုချင်းစီရဲ့ history လိုတာ ရှိရင် Step Functions က Lambda ကွင်းဆက်ထက် ရှင်းပါတယ်။ AWS SDK integration တွေနဲ့ DynamoDB, SQS, SNS စတာတွေကို Lambda မပါဘဲ တိုက်ရိုက် ခေါ်နိုင်လို့ code နဲ့ cold start နှစ်ခုလုံး လျော့သွားပါတယ်။
ဒါပေမယ့် Lambda တစ်ခုက queue တစ်ခုထဲ message တစ်ခု ထည့်ရုံ ဆိုရင် workflow ထည့်စရာ မလိုပါဘူး။ Request တစ်ခုကို millisecond အနည်းငယ်အတွင်း ပြန်ပေးရမယ့် synchronous API path မှာလည်း state transition တိုင်းက latency ထပ်တိုးပါတယ်။ တစ်စက္ကန့်မှာ ထောင်ချီ run မယ့် workflow တိုတိုလေးတွေမှာ Standard ကို သုံးရင် transition အလိုက် ကုန်ကျစရိတ်က မြန်မြန် တက်လာလို့ Express ကို ရွေးပါ။
Production မှာ
Production မှာ ဘယ်လို သုံးကြလဲ
အသုံးများတဲ့ ပုံစံ တစ်ခုက Standard နဲ့ Express ကို ပေါင်းသုံးတာပါ။ Order lifecycle လို ကြာမြင့်ပြီး audit လိုတဲ့ parent workflow က Standard ဖြစ်ပြီး item ထောင်ချီကို validate လုပ်တာလို တိုတိုနဲ့ volume များတဲ့ အပိုင်းကို Express child workflow အဖြစ် ခွဲထုတ်ပါတယ်။ ဒါမှ history နဲ့ exactly-once ကို အရေးကြီးတဲ့နေရာမှာပဲ ငွေပေးရပါတယ်။
- Data pipeline တွေ: Glue job .sync → Athena query → ရလဒ်ကို SNS နဲ့ အကြောင်းကြား။
- လူ approve လုပ်ရတဲ့ flow တွေ: waitForTaskToken နဲ့ email/Slack link နောက်ကွယ်မှာ token ကို သိမ်းထား။
- ExecutionsFailed နဲ့ ExecutionsTimedOut metric တွေပေါ် CloudWatch alarm၊ X-Ray tracing ဖွင့်ထား။
- State machine version နဲ့ alias တွေသုံးပြီး definition အသစ်ကို traffic နည်းနည်းနဲ့ အရင် စမ်း။
Saga တွေမှာ compensation step တိုင်းကို idempotent ဖြစ်အောင် ရေးကြပါတယ် — refund-payment ကို နှစ်ခါ ခေါ်မိရင်လည်း ငွေနှစ်ခါ ပြန်မအမ်းရပါဘူး။ ဒါကြောင့် chargeId လို ID တွေကို payment provider ဆီ idempotency key အဖြစ် ပို့ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
အမှား ၁: ResultPath မေ့ပြီး input ပျောက်တာ
Task ရလဒ်က default အနေနဲ့ state input တစ်ခုလုံးကို အစားထိုးပါတယ်။ ChargePayment ပြီးရင် orderId နဲ့ reservationId ပျောက်သွားပြီး compensation step ကျမှ 'ဘာကို release လုပ်ရမှန်း မသိ' ဖြစ်ပါတယ်။ Task တိုင်းမှာ ResultPath (ဥပမာ "$.payment") ကို သတ်မှတ်ပြီး Catch မှာလည်း "$.error" ကို သုံးပါ။
အမှား ၂: payload ကြီးတွေကို state ထဲ ထည့်တာ
State input/output တစ်ခုစီမှာ 256 KB ကန့်သတ်ချက် ရှိပါတယ်။ CSV file တစ်ခုလုံး ဒါမှမဟုတ် Map ရလဒ် ထောင်ချီကို state ထဲ ထည့်ရင် States.DataLimitExceeded နဲ့ ကျရှုံးပါတယ်။ Data ကို S3 ထဲ သိမ်းပြီး bucket နဲ့ key ကိုပဲ ပို့ပါ၊ Map ရလဒ်တွေကို ResultPath null ဒါမှမဟုတ် Distributed Map ResultWriter နဲ့ ထိန်းပါ။
အမှား ၃: workflow type မှားရွေးတာ
Express workflow က .sync နဲ့ waitForTaskToken ကို မထောက်ပံ့ပါဘူး၊ ၅ မိနစ်ကျော်ရင်လည်း timeout ဖြစ်ပါတယ်။ ဒါကြောင့် approval flow ကို Express နဲ့ ဆောက်လို့ မရပါဘူး။ ဆန့်ကျင်ဘက်အနေနဲ့ IoT event တစ်ခုချင်းစီကို Standard workflow နဲ့ process လုပ်ရင် transition ခ အများကြီး ကျပါတယ်။ Type ကို နောက်မှ ပြောင်းလို့ မရလို့ အစကတည်းက စဉ်းစားပါ။
💰 Cost & Cleanup
Standard workflow တွေကို state transition အလိုက် ကျသင့်ပါတယ် (us-east-1 မှာ transition ၁၀၀၀ ကို ဒေါ်လာ ၀.၀၂၅ ဝန်းကျင်ဖြစ်ပြီး လစဉ် free tier transition ၄၀၀၀ ပါတယ်လို့ ခန့်မှန်းရပါတယ် — pricing page မှာ စစ်ပါ)။ Retry တစ်ခုချင်းစီလည်း transition အဖြစ် ရေတွက်ပါတယ်။ Express ကိုတော့ request အရေအတွက်နဲ့ duration × memory အလိုက် ကျသင့်ပါတယ်။ Lambda invocation, SQS message, CloudWatch Logs တွေကလည်း သီးခြား ကုန်ကျပါတယ်။ ဒီ lab ရဲ့ execution အနည်းငယ်က free tier ထဲ ဝင်တတ်ပေမယ့် waitForTaskToken မှာ ရပ်နေတဲ့ execution တွေကို ရပ်ပြီး resource အားလုံး ဖျက်ပါ။
စမ်းပြီးရင် ဖျက်ဖို့ command
aws stepfunctions stop-execution --execution-arn "$EXECUTION_ARN"
aws stepfunctions delete-state-machine --state-machine-arn "$SM_ARN"
for fn in reserve-inventory charge-payment create-shipment refund-payment \
release-inventory; do
aws lambda delete-function --function-name "$fn"
done
aws sqs delete-queue --queue-url "$APPROVALS_QUEUE_URL"
cdk destroy OrderSagaStack --forceMini challenge
Mini challenge: ship မလုပ်ခင် တစ်နာရီ စောင့်
Customer တွေက order တင်ပြီး တစ်နာရီအတွင်း cancel လုပ်နိုင်ရပါမယ်။ ChargePayment နဲ့ ShipOrder ကြားမှာ ထည့်ဖို့ state နှစ်ခုပါတဲ့ ASL ကို ရေးပါ။
- 3600 စက္ကန့် စောင့်တဲ့ Wait state တစ်ခု
- $.cancelled true ဖြစ်ရင် RefundPayment ကို၊ မဟုတ်ရင် ShipOrder ကို သွားတဲ့ Choice state တစ်ခု
- Valid JSON ဖြစ်ပြီး state တိုင်းမှာ Next ဒါမှမဟုတ် Default ပါရမယ်
ရလဒ်: Cancel မလုပ်တဲ့ order တွေက တစ်နာရီ နောက်ကျပြီး ship ဖြစ်ပြီး cancel လုပ်တဲ့ order တွေက refund နဲ့ release compensation လမ်းကြောင်းကို သွားပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
BooleanEquals comparison ကို သုံးပါ။ Field မရှိနိုင်ရင် IsPresent နဲ့ And ကို ပေါင်းသုံးနိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
{
"CoolingOff": { "Type": "Wait", "Seconds": 3600, "Next": "WasCancelled" },
"WasCancelled": {
"Type": "Choice",
"Choices": [
{
"And": [
{ "Variable": "$.cancelled", "IsPresent": true },
{ "Variable": "$.cancelled", "BooleanEquals": true }
],
"Next": "RefundPayment"
}
],
"Default": "ShipOrder"
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Standard က ကြာမြင့်တဲ့၊ audit လိုတဲ့ workflow အတွက်၊ Express က တိုတိုနဲ့ volume များတဲ့ workflow အတွက်ပါ။
- Transient error တွေကို backoff နဲ့ jitter ပါတဲ့ Retry နဲ့၊ ကျန်တာကို Catch နဲ့ compensation ဆီ လွှဲပါ။
- ResultPath ကို အမြဲသုံးပြီး input ကို ထိန်းပါ၊ payload ကြီးတွေကို S3 ထဲ ထားပါ။
- Saga pattern က ပြီးသွားတဲ့ step တွေကို ပြောင်းပြန် အစဉ်နဲ့ idempotent compensation တွေနဲ့ undo လုပ်ပါတယ်။
AWS Step Functions Developer Guide — AWS Documentation
Amazon States Language — AWS Documentation
Service integration patterns — AWS Documentation