ablog

不器用で落着きのない技術者のメモ

Step Functions から Lambda 経由で AppFlow を実行する

Step Functions ステートマシン

{
    "StartAt": "Execute lambda function",
    "States": {
        "Execute lambda function": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:ap-northeast-1:123456789012:function:execAppFlowGetAccount",
            "ResultPath": "$.statusCode",
            "End": true
        }
    }
}

Lambda 関数

import json
import boto3
import time

flow_name = 'account-ondemand'

def check_status(exec_id):
    appflow_client = boto3.client('appflow')
    exec_status = None
    desc_response = appflow_client.describe_flow_execution_records(
        flowName = flow_name
    )
    for rec in desc_response['flowExecutions']:
        if rec['executionId'] == exec_id:
            exec_status = rec['executionStatus']
            break
    return exec_status

def lambda_handler(event, context):
    appflow_client = boto3.client('appflow')
    exec_response = appflow_client.start_flow(
        flowName = flow_name
    )
    exec_id = exec_response['executionId']

    exec_status = None
    while True:
        time.sleep(5)
        exec_status = check_status(exec_id)
        if exec_status != "InProgress":
            break

    return {
        'statusCode': exec_status
    }

AppFlow の設定と実行履歴

f:id:yohei-a:20210502163648p:plain

S3 に出力されたファイル

f:id:yohei-a:20210502163915p:plain
f:id:yohei-a:20210502164045p:plain