0Pricing
Serverless Backend with AWS Lambda & API Gateway · 课时

构建状态机

针对不同使用场景设计并实现多种状态机类型(Standard、Express),并与 Lambda 及其他服务集成

构建状态机 是 CoddyKit 上的免费 Serverless Backend with AWS Lambda & API Gateway 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Serverless Backend with AWS Lambda & API Gateway 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Serverless Backend with AWS Lambda & API Gateway 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Build Your First Workflow

Welcome back! In the previous lesson, we learned what AWS Step Functions are. Now, let's dive into building them!

You'll learn how to define different steps (states) and connect them to create powerful, automated workflows. We'll also explore the two main types of workflows: Standard and Express.

Anatomy of a State Machine

A Step Functions state machine is a workflow that defines a series of steps, called states. Each state performs a specific task or makes a decision.

  • States: The individual steps in your workflow.
  • Transitions: Rules that determine which state comes next.
  • Input/Output: Data passed between states.

Think of it like a flowchart, but executed by AWS!

Common State Types

Step Functions offers various state types to build your logic:

  • Task: Performs work by calling a service (e.g., Lambda, ECS).
  • Pass: Passes its input to its output without performing work. Useful for debugging or structuring.
  • Choice: Adds branching logic (if/else).
  • Wait: Pauses the execution for a specified time or until a specific time.
  • Succeed/Fail: Stops the execution successfully or with an error.
  • Parallel: Executes multiple branches in parallel.

Defining a Simple Pass State

Let's start with a simple Pass state. This state simply takes its input and passes it as output. It's often used for testing or structuring your state machine.

State machines are defined using Amazon States Language (ASL), which is a JSON-based structure.

Pass State ASL Example

Here's how a Pass state looks in ASL. It defines the state name, its type, and where to go next.

{ "Comment": "A simple Pass state machine",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Pass",
      "Result": {
        "message": "Hello from Step Functions!"
      },
      "End": true
    }
  }
}

Standard Workflows: The Durable Choice

AWS Step Functions offers two workflow types: Standard and Express.

Standard Workflows are ideal for long-running, durable, and auditable workflows. They can run for up to a year!

  • Durability: Retains full execution history for up to 90 days.
  • Auditability: Easy to track every step and input/output.
  • Use Cases: Order fulfillment, data processing pipelines, human approvals.

Express Workflows: Speed and Scale

Express Workflows are designed for high-volume, event-driven workloads. They are much faster and more cost-effective for short-duration tasks.

  • Speed: Can complete thousands of executions per second.
  • Cost: Priced per execution and duration, often cheaper for short tasks.
  • History: Execution history is limited (up to 90 days in CloudWatch Logs, not directly in Step Functions console).
  • Use Cases: Microservice orchestration, real-time streaming data processing, mobile backend processing.

Integrating Lambda: The Task State

One of the most common ways to perform work in a state machine is using a Task state to invoke an AWS Lambda function.

The Resource field in the ASL specifies the ARN of the Lambda function to call.

Example Lambda for Step Functions

This simple Python Lambda function receives an event (input from Step Functions) and returns a modified message. Step Functions will pass its output to the next state.

import json

def lambda_handler(event, context):
    print(f"Received event: {json.dumps(event)}")
    name = event.get('name', 'World')
    return {
        'statusCode': 200,
        'body': f"Hello, {name}! This is from Lambda."
    }

Workflow Type Challenge

You need to build a workflow for processing thousands of incoming IoT sensor readings every minute. Each reading takes less than 1 second to process.

Recap: Building Workflows

Great job! You've learned the fundamentals of building AWS Step Functions state machines:

  • State machines are defined by a series of states and transitions.
  • Key state types include Task, Pass, Choice, and Wait.
  • Standard Workflows are for long-running, auditable processes.
  • Express Workflows are for high-volume, short-duration tasks.
  • You can integrate Lambda functions using a Task state.

Next, we'll explore orchestrating complex workflows!

常见问题解答

「构建状态机」课时是免费的吗?

是的 — 「构建状态机」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Serverless Backend with AWS Lambda & API Gateway 课程的其余内容,请升级到 CoddyKit PRO。 Serverless Backend with AWS Lambda & API Gateway 课程共包含 4 节课。

「构建状态机」这节课中我会学到什么?

针对不同使用场景设计并实现多种状态机类型(Standard、Express),并与 Lambda 及其他服务集成 你通过在浏览器中直接运行的动手代码来练习 Serverless Backend with AWS Lambda & API Gateway,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Serverless Backend with AWS Lambda & API Gateway 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Serverless Backend with AWS Lambda & API Gateway 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「构建状态机」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Serverless Backend with AWS Lambda & API Gateway 课中编写并运行代码吗?

能。每节 Serverless Backend with AWS Lambda & API Gateway 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. AWS Step Functions 简介
  2. 构建状态机
  3. 编排复杂工作流
  4. 状态机中的错误处理与重试
← 返回 Serverless Backend with AWS Lambda & API Gateway