Terraform-同一个bucket上有多个aws_s3_bucket_notification触发器



我需要为S3存储桶创建一个触发器。我们使用以下内容来创建触发器:

resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = var.aws_s3_bucket_id
lambda_function {
lambda_function_arn = var.lambda_function_arn
events              = ["s3:ObjectCreated:Put"]
filter_prefix       = var.filter_prefix
filter_suffix       = var.filter_suffix
}
}

当bucket还没有触发器时,这种情况很好,除了生产环境之外的所有环境都是这样。当我们部署生产时,我们看到bucket上已经存在的触发器被删除了。我们需要两个触发器。我可以手动添加另一个触发器,例如,只需更改前缀就可以添加PUT事件触发器,但当我从Terraform中添加时,前一个总是会被删除。我缺什么了吗?

aws_s3_bucket_notification资源文档在顶部提到了这一点:

注意:S3 Buckets仅支持单个通知配置。向同一个S3 Bucket声明多个aws_s3_bucket_notification资源将导致配置上的永久差异。参见示例";触发多个Lambda函数";选择。

他们的例子展示了如何通过在aws_s3_bucket_notification资源中添加多个lambda_function块来实现这一点:

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_lambda_permission" "allow_bucket1" {
statement_id  = "AllowExecutionFromS3Bucket1"
action        = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.func1.arn}"
principal     = "s3.amazonaws.com"
source_arn    = "${aws_s3_bucket.bucket.arn}"
}
resource "aws_lambda_function" "func1" {
filename      = "your-function1.zip"
function_name = "example_lambda_name1"
role          = "${aws_iam_role.iam_for_lambda.arn}"
handler       = "exports.example"
runtime       = "go1.x"
}
resource "aws_lambda_permission" "allow_bucket2" {
statement_id  = "AllowExecutionFromS3Bucket2"
action        = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.func2.arn}"
principal     = "s3.amazonaws.com"
source_arn    = "${aws_s3_bucket.bucket.arn}"
}
resource "aws_lambda_function" "func2" {
filename      = "your-function2.zip"
function_name = "example_lambda_name2"
role          = "${aws_iam_role.iam_for_lambda.arn}"
handler       = "exports.example"
}
resource "aws_s3_bucket" "bucket" {
bucket = "your_bucket_name"
}
resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = "${aws_s3_bucket.bucket.id}"
lambda_function {
lambda_function_arn = "${aws_lambda_function.func1.arn}"
events              = ["s3:ObjectCreated:*"]
filter_prefix       = "AWSLogs/"
filter_suffix       = ".log"
}
lambda_function {
lambda_function_arn = "${aws_lambda_function.func2.arn}"
events              = ["s3:ObjectCreated:*"]
filter_prefix       = "OtherLogs/"
filter_suffix       = ".log"
}
}

相关内容

  • 没有找到相关文章

最新更新