🔁

AWS Step Functions and Lambda Integration

Jul 9, 2024

AWS Step Functions and Lambda Integration

Overview:

  • Topic: Building a transaction processor state machine for credit card transactions.
  • Objective: Integrate AWS Step Functions with Lambda functions to process purchase and refund transactions.
  • State Machine Input: JSON object with an event type (purchase or refund).

Process Overview:

  1. Input Event:
    • JSON object indicating event type (purchase or refund).
  2. State Machine:
    • Initial step is a choice task.
    • Decision is made based on whether the event type is a purchase or refund.
  3. Lambda Functions:
    • Two lambda functions for processing: process_purchase and process_refund.
    • Each function logs the input, constructs a response object, and returns the result to the state machine.

Implementing in AWS Console:

  1. **Creating Lambda Functions:

    • Navigate to AWS Console.
    • Create two functions: process_purchase and process_refund.
    • Use Python 3.6 and basic Lambda permissions.
  2. Writing Lambda Code (process_purchase):

    • Log the input message.
    • Construct a response object with:
      • transaction_type set to input value.
      • timestamp using current date/time.
      • Custom message: "hello from lambda inside process_purchase".
    • Return the response object.
  3. Duplicate for Refund Function:

    • Create process_refund with identical logic, named accordingly.**

Setting Up IAM Role:

  1. Role Creation:
    • Create a role for step functions to invoke lambda functions.
    • Use default Step Functions role with Lambda invocation permissions.

Configuring Step Functions:

  1. Gather ARNs:
    • Copy ARNs of both lambda functions.
  2. **Create State Machine:
    • Amazon States Language to define state machine.
    • Decision step (process_transaction) routes to correct Lambda based on event type.
    • Terminal states for both processes.
  3. Paste Python Code into State Machine:
    • Use ARNs gathered for reference in resource fields.**

Testing:

  1. Run Instances:
    • Start execution with input (transaction_type set to purchase or refund).
    • Monitor the process in real time with visual cues.
  2. Validate Outputs:
    • Confirm inputs and outputs in each state.
    • Verify custom message and timestamp in Lambda outputs.

Conclusion:

  • Next Steps: Upcoming videos will cover high-level overviews and advanced capabilities of step functions.