概要

AWS 公式の StepFunctions チュートリアル を Serverless フレームワークを使って実装したサンプル

プロジェクトの作成

作業するディレクトリを作成します。 serverless フレームワークもなければインストールしてください。

npm install -g serverless
npm install serverless-pseudo-parameters
mkdir call-center-state-machine

プロジェクトのルートフォルダは以下のような構成になります。

.
├── handlers
│   ├── assign_case.rb
│   ├── close_case.rb
│   ├── escalate_case.rb
│   ├── open_case.rb
│   └── work_on_case.rb
├── package.json
└── serverless.yml

ステートマシンで実行される関数の作成

def handler(event:, context:)
  my_case_id = event['inputCaseID']
  my_message = "Case #{my_case_id} : opend..."

  { Case: my_case_id, Message: my_message }
end
def handler(event:, context:)
  my_case_id = event['Case']
  my_message = "#{event['Message']} assigned..."

  { Case: my_case_id, Message: my_message }
end
def handler(event:, context:)
  my_case_id = event['Case']
  my_message = event['Message']

  my_case_status = Random.rand.round
  my_message += if my_case_status == 1
                  'resolved...'
                else
                  'unresolved...'
                end

  { Case: my_case_id, Status: my_case_status, Message: my_message }
end
def handler(event:, context:)
  my_case_id = event['Case']
  my_message = "#{event['Message']} closed."
  my_case_status = event['Status']

  { Case: my_case_id, Status: my_case_status, Message: my_message }
end
def handler(event:, context:)
  my_case_id = event['Case']
  my_message = "#{event['Message']} escalating."
  my_case_status = event['Status']

  { Case: my_case_id, Status: my_case_status, Message: my_message }
end

IAM ロールとステートマシンのワークフローの設定

service: call-center-state-machine

provider:
  name: aws
  region: us-west-1
  runtime: ruby2.5

plugins:
  - serverless-step-functions
  - serverless-pseudo-parameters

functions:
  openCaseFunc:
    handler: handlers/open_case.handler
  assignCaseFunc:
    handler: handlers/assign_case.handler
  workOnCaseFunc:
    handler: handlers/work_on_case.handler
  closeCaseFunc:
    handler: handlers/close_case.handler
  escalateCaseFunc:
    handler: handlers/escalate_case.handler

stepFunctions:
  stateMachines:
    callCenterStateMachine:
      definition:
        Comment: "A simple AWS Step Functions state machine that automates a call center support session."
        StartAt: OpenCase
        States:
          OpenCase:
            Type: Task
            Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:stage, 'dev'}-openCaseFunc"
            Next: AssignCase
          AssignCase:
            Type: Task
            Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:stage, 'dev'}-assignCaseFunc"
            Next: WorkOnCase
          WorkOnCase:
            Type: Task
            Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:stage, 'dev'}-workOnCaseFunc"
            Next: IsCaseResolved
          IsCaseResolved:
            Type: Choice
            Choices:
              - Variable: "$.Status"
                NumericEquals: 1
                Next: CloseCase
              - Variable: "$.Status"
                NumericEquals: 0
                Next: EscalateCase
          CloseCase:
            Type: Task
            Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:stage, 'dev'}-closeCaseFunc"
            End: true
          EscalateCase:
            Type: Task
            Resource: "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:stage, 'dev'}-escalateCaseFunc"
            Next: Fail
          Fail:
            Type: Fail
            Cause: "Engage Tier 2 Support."

デプロイ

sls deploy