AWS使用SNS链接Lambda函数

时间:2016-08-18 06:29:03

标签: javascript amazon-web-services amazon-s3 lambda

我有两个lambda函数,当对象上传到S3时应该触发。

要触发这两个脚本,在第一个脚本中,我将发布一个sns主题,后面跟着第二个脚本。发布时我应该通过该事件,以便我可以操纵它。

我遇到了将事件传递给第二个lambda函数的问题。我是这样做的:

第一个脚本像这样传递事件

exports.handler = function(event, context) {
 console.log("Loading metadata function");
 var eventText = JSON.stringify(event, null, 2);
 console.log("Received event:", eventText);
 var sns = new AWS.SNS({region:'us-west-2'});
 var messageparams = {
    Message: eventText, 
    Subject: "Successfully send",
    TopicArn: "MyArn"
 };
 sns.publish(messageparams);

第二个脚本应该使用像这样的事件

def lambda_handler(event, context):
    """
    :param event:
    :param context:
    """
    # Loop through records provided by S3 Event trigger
    for s3_record in event['Records']:
        logger.info("Working on new s3_record...")
        # Extract the Key and Bucket names for the asset uploaded to S3
        key = s3_record['s3']['object']['key']

1 个答案:

答案 0 :(得分:0)

我建议您configure S3 to publish events straight to SNS没有任何中级Lambda。接下来,您可以将两个或更多Lambda配置为invoked by the SNS notifications

另一方面,如果您只需要一个Lambda函数,则可以配置S3直接发布事件和invoke the Lambda with the event data as parameter而不涉及SNS。