javascript - AWS 将 Lambda 函数与 SNS 链接起来

标签 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']

最佳答案

我建议您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 参与。

关于javascript - AWS 将 Lambda 函数与 SNS 链接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39011307/

相关文章:

amazon-web-services - aws 安全组的 Terraform 中的变量值

javascript - 如何禁止人们保存我网站的源代码?

javascript - 如何使用 Bluebird 将原始数据与 Promise.map 的响应一起传递?

javascript - Chrome 扩展程序 : can detect drag&drop in address bar?

amazon-web-services - Ansible 和 s3 模块

amazon-ec2 - 允许用户放置、获取、删除和修改权限的 S3 策略

amazon-web-services - 为什么我的无服务器 Lambda 无法访问 S3 存储桶和项目?

javascript - 将对象转换为对象数组

android - 无法在 s3 中获取更新的对象?

amazon-web-services - 将 Snowflake 中的数据作为 XLSX 文件保存到 S3 存储桶