You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在指定公开文件更新时触发AWS State Machine?

解决方案:检测外部文件更新并触发AWS Step Functions状态机

由于目标文件是外部公开资源,没有内置的Webhook通知更新事件,最可靠的实现方式是定期轮询+条件触发的流程,具体步骤如下:

方案1:CloudWatch EventBridge + Lambda + DynamoDB(推荐)

通过定时触发Lambda函数检查文件更新状态,当检测到变化时触发你的State Machine。

1. 创建DynamoDB表存储文件状态

创建一个简单的DynamoDB表(例如命名为FileUpdateTracker),用来记录文件的上次更新标识,字段包括:

  • file_id(字符串类型主键,可填jnto_visitor_arrivals)
  • last_etag(字符串,存储文件的ETag值)
  • last_modified(字符串,存储文件的Last-Modified时间)

2. 编写Lambda函数实现更新检测与触发

用Python编写Lambda函数,核心逻辑是对比文件当前状态与历史记录,若有更新则启动State Machine:

import boto3
import requests

# 初始化AWS客户端
dynamodb = boto3.resource('dynamodb')
step_functions = boto3.client('stepfunctions')

# 配置参数
TABLE_NAME = 'FileUpdateTracker'
FILE_ID = 'jnto_visitor_arrivals'
FILE_URL = 'https://www.jnto.go.jp/jpn/statistics/since2003_visitor_arrivals.xlsx'
STATE_MACHINE_ARN = 'arn:aws:states:你的区域:你的账号ID:stateMachine:你的状态机名称'

def lambda_handler(event, context):
    # 发送HEAD请求获取文件头信息(避免下载完整文件)
    response = requests.head(FILE_URL)
    current_etag = response.headers.get('ETag')
    current_last_modified = response.headers.get('Last-Modified')
    
    # 从DynamoDB读取历史状态
    table = dynamodb.Table(TABLE_NAME)
    db_result = table.get_item(Key={'file_id': FILE_ID})
    existing_record = db_result.get('Item')
    
    # 判断文件是否更新
    if not existing_record or existing_record['last_etag'] != current_etag:
        # 触发State Machine执行
        step_functions.start_execution(
            stateMachineArn=STATE_MACHINE_ARN,
            input='{"file_url": "%s"}' % FILE_URL
        )
        
        # 更新DynamoDB中的历史状态
        if existing_record:
            table.update_item(
                Key={'file_id': FILE_ID},
                UpdateExpression='SET last_etag = :etag, last_modified = :lm',
                ExpressionAttributeValues={
                    ':etag': current_etag,
                    ':lm': current_last_modified
                }
            )
        else:
            table.put_item(
                Item={
                    'file_id': FILE_ID,
                    'last_etag': current_etag,
                    'last_modified': current_last_modified
                }
            )
        
        return {'status': 'triggered', 'message': '文件已更新,状态机已启动'}
    else:
        return {'status': 'no_change', 'message': '文件未更新'}

3. 配置Lambda权限

给Lambda函数添加以下IAM权限:

  • dynamodb:GetItem、dynamodb:PutItem、dynamodb:UpdateItem(针对创建的DynamoDB表)
  • states:StartExecution(针对你的State Machine)

4. 设置EventBridge定时触发

创建EventBridge规则,设置触发频率为每月一次(例如每月1号凌晨2点,匹配管理员的更新周期),触发目标选择你创建的Lambda函数。

方案2:API Gateway接收外部通知(可选)

如果后续该文件的发布方提供更新Webhook,可配置API Gateway作为接收端点直接触发State Machine:

  • 创建API Gateway REST API,设置POST方法,集成目标选择Step Functions的StartExecution动作
  • 将API Gateway端点提供给发布方,让其在文件更新时主动调用该端点

但目前该文件无Webhook支持,因此方案1是更可行的选择。

内容的提问来源于stack exchange,提问作者Luu Quoc Viet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 08:12:44