Ravindra BagaleCourses & study guides मराठी Track your progress

Project: Build an Order Workflow with Step Functions, Lambda, DynamoDB and SNS (Source Code)

Flipkart order flow: paid → packed → shipped. Step Functions runs each Lambda step in order, updates DynamoDB, then SNS alerts you. ASL definition is in the zip.

What you'll learn in this chapter

  • Write a linear state machine
  • Wire task Lambdas + DynamoDB
  • SNS on completion
  • Start execution and cleanup

1. Everyday example

Why do we need this?

If you chain Lambdas with raw invoke, failures and retries get messy. Step Functions shows every step, retries, and the input/output path.

What is it?

State machine: MarkPaid → MarkPacked → MarkShipped → Notify. Each task Lambda sets status on orderId. Notify publishes SNS.

WorkflowStartExecutionorderIdMarkPaidDynamoDBMarkPackedMarkShippedNotifySNS
Replace ARNs in order.asl.json.

Download: order-workflow.zip — ASL + four Lambdas.

2. Lab

How to do it (step by step)

A. Table + SNS + Lambdas

  1. Table yourname-workflow, PK orderId.
  2. SNS topic yourname-order-status + email confirm.
  3. Deploy mark_paid, mark_packed, mark_shipped, notify with shared role (DynamoDB + sns:Publish). Env TABLE_NAME / TOPIC_ARN.

B. State machine

  1. Step Functions → Create state machine → Standard. Paste ASL from zip with real Lambda ARNs.
  2. IAM role for Step Functions to invoke those Lambdas (console can create it).
  3. Start execution with input {"orderId":"lab-1","sku":"TEE-1"}.
  4. Watch Graph view go green. Check DynamoDB status=shipped and email.
{
  "orderId": "lab-1",
  "sku": "TEE-1"
}

Cleanup

  1. Delete state machine. Delete Lambdas. Delete table, topic, roles.
  2. Old executions remain in history briefly — fine for lab.

3. Interview

Q. Standard vs Express?

Standard for long audit workflows; Express for high-volume short flows.

Q. Why not one fat Lambda?

Visible steps, per-state retry, easier ops.

Q. Callback patterns?

Task tokens for human approval — next level after this lab.

Q. Input passing?

Each state returns event; next state receives it.

Q. Cleanup?

State machine + Lambdas + table + topic.

Try at home

  1. Add a Fail state if sku missing with Choice.
  2. Add retry on MarkPacked with maxAttempts 2.

In short

  1. Step Functions orchestrates Lambda tasks.
  2. DynamoDB holds order status.
  3. SNS announces the end.
  4. Clean up state machines too.

Practice Tasks

About this scenario

Amravati Agro Market is a agriculture business in Amravati. Anjali Thakre looks after its IT and cloud work. They count money in INR and run their AWS resources in the ap-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 12

Easy List functions

Why: See which functions exist in this Region.

What to do: List the Lambda functions in ap-south-1.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Invoke a function

Why: Invoke runs the function now and saves its answer in a file.

What to do: Invoke amravatiagro-orders-fn and save the response to out.json.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Trust policy for a Lambda role

Why: The trust policy says WHO may take the role.

What to do: Write a trust policy that lets lambda.amazonaws.com assume the role.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium Put an item

Why: DynamoDB JSON shows the type of each value: S for string, N for number.

What to do: Put an item into amravatiagro-orders with orderId "o-101" (string) and amount 2500 (number).

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Change timeout and memory

Why: More memory also gives more CPU; timeout stops runaway runs.

What to do: Set amravatiagro-orders-fn to timeout 30 seconds and memory 512 MB.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Let a function write logs

Why: Without these actions, Lambda logs never appear in CloudWatch.

What to do: Allow logs:CreateLogGroup, logs:CreateLogStream, logs:PutLogEvents on arn:aws:logs:*:*:* only.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Create a Lambda function

Why: Lambda needs the code zip, a runtime, a handler name and an execution role.

What to do: Create function amravatiagro-orders-fn: runtime python3.12, handler app.handler, role arn:aws:iam::120348739971:role/lambda-basic, code in function.zip.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Set environment variables

Why: Keep settings like table names out of the code.

What to do: Set the environment variable TABLE=amravatiagro-orders on amravatiagro-orders-fn.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Invoke with a JSON payload

Why: In CLI v2, raw JSON needs --cli-binary-format raw-in-base64-out.

What to do: Invoke amravatiagro-orders-fn with payload {"orderId":"o-101"} (raw JSON) and save the reply in out.json.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Lambda GB-seconds

Why: Lambda bills memory × time.

What to do: 1,000,000 invocations, 512 MB memory, 200 ms each. How many GB-seconds?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Connect an SQS queue to Lambda

Why: An event source mapping makes Lambda poll the queue and process messages in batches.

What to do: Create an event source mapping from queue arn:aws:sqs:ap-south-1:120348739971:amravatiagro-orders to amravatiagro-orders-fn with batch size 10.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Let S3 call your function

Why: A resource-based permission is needed before S3 can invoke the function.

What to do: Add permission statement s3invoke to amravatiagro-orders-fn so s3.amazonaws.com can call lambda:InvokeFunction from bucket arn:aws:s3:::amravatiagro-uploads.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: Order workflow with Step Functions. Flipkart-style flow paid → packed → shipped needs ordered steps, retries, and a clear status — not one giant Lambda. AWS Step Functions runs a state machine. Each Task state invokes a Lambda; DynamoDB holds status; SNS alerts on shipped. Write ASL JSON (zip), create state machine, three Lambdas (markPaid, markPacked, markShipped), start execution with orderId. StartExecution {"orderId":"O-42"} → DynamoDB status moves paid→packed→shipped and you get an SNS email.
Revision card: Order workflow with Step Functions · Download PNG · Open card page
Revision card: Why not one fat Lambda?. One function that sleeps between steps wastes duration billing and mixes failures. Timeouts hit the whole flow. Step Functions orchestrates: visual history, per-state retries, wait states, and parallel branches without blocking billing the same way. Define Task → Task → Task in ASL. Add Retry on transient errors. Keep each Lambda under a few seconds. Packed Lambda fails once → Step Functions retries that state only; paid stays recorded.
Revision card: Why not one fat Lambda? · Download PNG · Open card page
Revision card: Standard vs Express workflows. Two Step Functions types confuse billing and how long a flow may run. Standard: up to 1 year, exactly-once, full execution history — great for business orders. Express: high event rate, at-least-once, cheaper short flows. For paid→packed→shipped lab choose Standard. Express fits IoT-scale streams under 5 minutes. Console executions list shows each state timestamp for O-42 — interview-friendly evidence.
Revision card: Standard vs Express workflows · Download PNG · Open card page
Comparison: EventBridge schedule vs Step Functions. EventBridge schedule: Fire a target on cron/rate. Step Functions: Ordered multi-step state machine with retries.
Comparison: EventBridge schedule vs Step Functions · Download PNG · Open card page
Comparison: Standard workflow vs Express workflow. Standard workflow: Up to 1 year; full execution history; exactly-once. Express workflow: Short high-rate; limited history; at-least-once.
Comparison: Standard workflow vs Express workflow · Download PNG · Open card page
Interview card: Why not one Lambda for paid→packed→shipped?. Timeouts, retries, and billing couple unrelated steps; debugging one failure is harder. Step Functions gives per-state retries and a visual execution history. Three Task Lambdas in ASL; StartExecution with orderId.
Interview card: Why not one Lambda for paid→packed→shipped? · Download PNG · Open card page
Interview card: Standard or Express for an order workflow?. Standard fits long-running business workflows with exactly-once and full history. Express fits high-volume short flows with at-least-once semantics. Choose Standard for paid→packed→shipped lab executions you inspect in the console.
Interview card: Standard or Express for an order workflow? · Download PNG · Open card page

More cards: Ravi Sir's Cards.