[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 @   郝壹贰叁  阅读(118)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示