从Airflow触发AWS Lambda函数

时间:2020-04-20 12:15:37

标签: python amazon-web-services aws-lambda airflow

我已经在AWS lambda中创建了一个函数,如下所示:

import boto3
import numpy as np
import pandas as pd
import s3fs
from io import StringIO


def test(event=None, context=None):
    # creating a pandas dataframe from an api
    # placing 2 csv files in S3 bucket

此函数查询外部API,并将2个csv文件放在S3存储桶中。我想在Airflow中触发此功能,我找到了以下代码:

import boto3, json, typing

def invokeLambdaFunction(*, functionName:str=None, payload:typing.Mapping[str, str]=None):
    if  functionName == None:
        raise Exception('ERROR: functionName parameter cannot be NULL')
    payloadStr = json.dumps(payload)
    payloadBytesArr = bytes(payloadStr, encoding='utf8')
    client = boto3.client('lambda')
    response = client.invoke(
        FunctionName=test,
        InvocationType="RequestResponse",
        Payload=payloadBytesArr
    )
    return response



if __name__ == '__main__':
    payloadObj = {"something" : "1111111-222222-333333-bba8-1111111"}
    response = invokeLambdaFunction(functionName='test',  payload=payloadObj)
    print(f'response:{response}')

但是据我了解,此代码段未连接到S3。这是从Airflow触发AWS Lambda功能的正确方法,还是有更好的方法?