[AWS] Step Function of Lambda

 

 

Ref: AWS Step Functions with Lambda Tutorial | Step by Step Guide

一、逻辑图

 

 

二、Function code

import json
import datetime
import urllib
import boto3

def lambda_handler(message, context):
    # TODO implement

    print("received messsage from step fn")
    print(message)

    response = {}
    response['TransactionType'] = message['TransactionType']
    response['Timestamp'] = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
    response['Message'] = "Hello from process purchase lambda"

    return response

有了ARN。

 

三、Role of Step Function

给了step function 权限,能够 invoke "lambda function"。

 

四、Step Function -> State machines

{
  "Comment": "A simple AWS Step Functions state machine that automates a call center support session.",
  "StartAt": "ProcessTransaction",
  "States": {
    "ProcessTransaction": {
        "Type" : "Choice",
        "Choices": [ 
          {
            "Variable": "$.TransactionType",
            "StringEquals": "PURCHASE",
            "Next": "ProcessPurchase"
          },
          {
            "Variable": "$.TransactionType",
            "StringEquals": "REFUND",
            "Next": "ProcessRefund"
          }
      ]
    },
"ProcessRefund": { "Type": "Task", "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", "End": true },
"ProcessPurchase": { "Type": "Task", "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME", "End": true } } }

 

进入测试界面,发送request测试即可。

 

 

Ref: How to Start an AWS Step Function Workflow From Lambda | STEP BY STEP GUIDE

通过Lambda触发以上的step function 模块。

import json
import boto3
import uuid

client = boto3.client('stepfunctions')

def lambda_handler(event, context):
    #INPUT -> { "TransactionId": "foo", "Type": "PURCHASE"}
    transactionId = str(uuid.uuid1()) #90a0fce-sfhj45-fdsfsjh4-f23f

    input = {'TransactionId': transactionId, 'Type': 'PURCHASE'}

    response = client.start_execution(
        stateMachineArn='YOUR ARN HERE!',
        name=transactionId,
        input=json.dumps(input)    
        )

 

 

 

 

Step Function适合做什么

Ref: 结合使用 AWS Batch 和 AWS Step Functions 创建视频处理工作流

使用 AWS Batch 和 AWS Step Functions
 
 
posted @ 2022-04-09 16:20  郝壹贰叁  阅读(126)  评论(0)    收藏  举报