基于多个s3存储桶事件通知触发了多个lambda

时间:2020-07-24 01:05:14

标签: amazon-s3 terraform terraform-provider-aws

我是terraform领域的新手,并且一直在尝试从s3存储桶上配置的不同事件中触发多个lambda。每次我尝试创建它时,它实际上都会覆盖上一个事件,并按照terraform计划创建最后一个事件。不知道我在做什么错。我看到一个git问题,可以通过它找到明确的解决方案。

希望对此有任何帮助!

随附的是我编写的示例代码,可以在环境级别上对其进行配置。

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]
}

0 个答案:

没有答案