AWS Step Functions

Hands-On

Demo - V2

Hands-On Demo Overview

checkInventory

Create Lambda Function - 1

export const handler = async (event) => {
    console.log("Checking inventory for order:", event.orderId);
    // Simulate inventory check
    const inStock = Math.random() < 0.9;
    return {
        orderId: event.orderId,
        inStock: inStock
    };
};

checkInventory - Code

Create Lambda Function - 2

processPayment
export const handler = async (event) => {
    console.log("Processing payment for order:", event.orderId);
    // Simulate payment processing
    const paymentSuccessful = Math.random() < 0.9;
    return {
        orderId: event.orderId,
        paymentSuccessful: paymentSuccessful
    };
};

processPayment - Code

Create Lambda Function - 3

updateOrderStatus
export const handler = async (event) => {
    console.log("Updating status for order:", event.orderId);
    // Simulate order status update
    return {
        orderId: event.orderId,
        status: "Completed"
    };
};

updateOrderStatus - Code

Step Functions - Create State Machine

Start a Blank Workflow

{
  "Comment": "A simple order processing workflow",
  "StartAt": "Check Inventory",
  "States": {
    "Check Inventory": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:211125437318:function:checkInventory",
      "Next": "Is In Stock?"
    },
    "Is In Stock?": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.inStock",
          "BooleanEquals": true,
          "Next": "Process Payment"
        }
      ],
      "Default": "Out of Stock"
    },
    "Process Payment": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:211125437318:function:processPayment",
      "Next": "Payment Successful?"
    },
    "Payment Successful?": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.paymentSuccessful",
          "BooleanEquals": true,
          "Next": "Update Order Status"
        }
      ],
      "Default": "Payment Failed"
    },
    "Update Order Status": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:211125437318:function:updateOrderStatus",
      "End": true
    },
    "Out of Stock": {
      "Type": "Fail",
      "Cause": "Item is out of stock",
      "Error": "OutOfStockError"
    },
    "Payment Failed": {
      "Type": "Fail",
      "Cause": "Payment processing failed",
      "Error": "PaymentError"
    }
  }
}

State Machine Code

MAKE SURE TO REPLACE THE AWS LAMBDA ARN

Confirm Role Creation

Start Execution 

Start Execution

{
  "orderId": "12345"
}

Successful Execution

Graph View

Event History

Clean Up

Delete Lambda Functions

Delete State Machine

Delete Any StepFunctions Role

StepFunc

🙏

Thanks

for

Watching