基于多个 s3 存储桶事件通知触发的多个 lambda [英] Multiple lambda triggered based on multiple s3 bucket event notifications

查看:45
本文介绍了基于多个 s3 存储桶事件通知触发的多个 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 terraform 世界的新手,一直在尝试从 s3 存储桶上配置的不同事件中触发多个 lambda.每次我尝试创建它时,它基本上都会覆盖前一个事件并根据 terraform 计划创建最后一个事件.不确定我在这里做错了什么.我看到了一个 git 问题,我可以用它找到明确的解决方案.

I am new to the world of terraform and have been trying to essentially trigger multiple lambdas from different events configured on an s3 bucket. Every time I try to create it, it essentially overwrites the previous event and creates the last one as per the terraform plan. Not sure what I am doing wrong here. I saw a git issue which i could find clear resolution with.

非常感谢您的帮助!

附上我编写的示例代码,可在环境级别进行配置.

Attached is the sample code i have written which is configurable at the environment level.

https://github.com/hashicorp/terraform/issues/6934

resource "aws_lambda_function" "lambda-function" {
  s3_bucket     = "${var.project}-${var.env}-${var.artifacts-postfix}"
  s3_key        = "lambda/${var.project}-${var.env}-${var.lambda-name}/function.zip"
  function_name = "${var.project}-${var.env}-${var.lambda-name}"
  role          = var.lambda-role
  handler       = "handler.lambda_handler"
  timeout       = "60"

  runtime = "python3.7"

  environment {
    variables = var.env-variables
  }

  tags = {
    Name    = "${var.project}-${var.env}-${var.lambda-name}"
    Project = var.project
    Env     = var.env
  }
}


resource "aws_lambda_permission" "s3-trigger" {
  count         = var.activate-trigger ? 1 : 0 
  statement_id  = "${var.project}-${var.env}-${var.lambda-name}-statement-id"
  action        = "lambda:InvokeFunction"
  function_name = "${var.project}-${var.env}-${var.lambda-name}"
  principal     = "s3.amazonaws.com"
  source_arn    = "arn:aws:s3:::${var.bucket-name}"
  depends_on    = [aws_lambda_function.lambda-function]
}


resource "aws_s3_bucket_notification" "bucket_notification" {
  count  = var.activate-trigger ? 1 : 0 
  bucket = var.bucket-name

  lambda_function {
    lambda_function_arn = aws_lambda_function.lambda-function.arn
    events              = ["s3:ObjectCreated:*"]
    filter_prefix       = var.s3-key-prefix
    filter_suffix       = var.s3-key-suffix
    id                  = "${var.project}-${var.env}-${var.lambda-name}-event-id"
  }

  depends_on = [aws_lambda_permission.s3-trigger]
}

推荐答案

一年后,我又碰壁了.ID 不是会创建多个通知的唯一标识符,它是一个可选值.AWS 分配真正的唯一标识符.

A year after, and I hit the same wall. ID is not a unique identifier that will create multiple notifications, is an optional value. AWS assigns the real unique identifier.

在我的场景中,我收到了通过 SES 发送并存储在 S3 中的电子邮件,因此当我使用 SES 操作将电子邮件存储在 s3 中时,我结束了通过 SNS 发布.让 lambda 订阅了 SNS 并在收到消息后开始工作.

In my scenario, I got emails coming through SES and stored in S3, so I ending publishing through SNS when I got the email stored in s3 using SES actions. Got lambda subscribed to SNS and doing the work once it gets the message.

从 aws_s3_bucket_notification 切换到 aws_sns_topic_subscription

Switched from aws_s3_bucket_notification to aws_sns_topic_subscription

这篇关于基于多个 s3 存储桶事件通知触发的多个 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆