Tạo state machine đầu tiên

State machine được viết bằng Amazon States Language (JSON). State đơn giản nhất là Pass: chép input sang output và không làm gì cả. State này hữu ích để phác thảo workflow trước khi viết code.

  1. State machine chạy bằng một IAM role. Console có thể tự tạo role nhưng tên role không theo quy tắc FCJ-*, nên bạn tạo role trong CloudShell. Role tin cậy Step Functions và chưa có quyền gì:
BOUNDARY=arn:aws:iam::$(aws sts get-caller-identity --query Account --output text):policy/LabPermissionsBoundary
aws iam create-role --role-name FCJ-SFN-Console-Role ${BOUNDARY:+--permissions-boundary $BOUNDARY} \
  --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"states.amazonaws.com"},"Action":"sts:AssumeRole"}]}' \
  --query 'Role.[RoleName,PermissionsBoundary.PermissionsBoundaryArn]' --output text
  • (1) Role FCJ-SFN-Console-Role đã được tạo kèm permissions boundary.

Role cho console

  1. Mở console Step Functions, chọn State machines (1) và Create state machine.

    State machines

  2. Trong hộp thoại:

    • (1) Giữ Create from blank.
    • (2) State machine name: Process_New_Account_Applications.
    • (3) State machine type: Standard.
    • (4) Chọn Continue.

    Tạo mới

  3. Chọn Code (1) và thay definition bằng đoạn JSON dưới đây (2). Sơ đồ hiện ba state Pass.

{
    "StartAt": "Check Name",
    "States": {
        "Check Name": {
            "Type": "Pass",
            "Next": "Check Address"
        },
        "Check Address": {
            "Type": "Pass",
            "Next": "Approve Application"
        },
        "Approve Application": {
            "Type": "Pass",
            "End": true
        }
    }
}

Definition

  1. Chọn Config. Ở Permissions, mở Execution role, gõ FCJ-SFN để lọc rồi chọn FCJ-SFN-Console-Role (1). Giữ nguyên các thiết lập khác và chọn Create.

    Execution role

    • (1) State machine đã được tạo.
    • (2) Query language là JSONPath, vì definition không khai báo QueryLanguage. Mọi definition trong workshop đều dùng JSONPath.

    Đã tạo

Đặc tả đầy đủ của Amazon States Language có tại states-language.net.

Chạy state machine

  1. Chọn Execute. Mỗi execution có thể nhận một input JSON. Nhập input dưới đây (1) rồi chọn Start execution (2).
{
    "application": {
        "name": "Spock",
        "address": "123 Enterprise Street"
    }
}

Start execution

  1. Execution Succeeded (1). Nó chỉ đi qua ba state Pass.

    Thành công

  2. Kéo xuống Graph view và chọn một state, ví dụ Check Address (1). Input/Output (2) cho thấy state Pass trả về đúng input của nó.

    Input và output