简体   繁体   中英

Error when creating S3 bucket notification in Terraform

I'm having an issue when creating a bucket notification to trigger a Lambda function. The error:

Error putting S3 notification configuration: InvalidArgument: Unable to validate the following destination configurations
    status code: 400

I've read that similar problems might be caused by the order in which the resources are created or that Lambda permissions are missing. However, I tried including depends_on in my code as well as applying the template couple of times and waiting in between. I'm using the least restrictive Lambda policy. I also tried using the exact sample code from the Terraform documentation , but that gives me a whole different error.

The exact same setup works fine if created in the console.

Here's the problematic part of my code:

resource "aws_lambda_function" "writeUsersToDB" {
  filename      = "writeUsersToDB.zip"
  function_name = "writeUsersToDB"
  role          = "arn:aws:iam::0000000:role/AWSLambdaFullAccess"
  handler       = "main.lambda_handler"
  memory_size = 256
  timeout = 900
  source_code_hash = filebase64sha256("writeUsersToDB.zip")
  runtime = "python3.8"
  environment {variables = local.parameters}
  layers = [ "arn:aws:lambda:eu-west-2:0000000:layer:pandas-pandas-schema-numpy:1" ]
}



resource "aws_s3_bucket_notification" "event" {
  bucket = aws_s3_bucket.user_data.id

  lambda_function {
    lambda_function_arn = aws_lambda_function.writeUsersToDB.arn
    events              = ["s3:ObjectCreated:*"]
    filter_suffix       = ".csv"
  }
  depends_on = [aws_lambda_function.writeUsersToDB]
}


resource "aws_s3_bucket" "user_data" {
  bucket = "nameofthebucket"
}

You are missing aws_lambda_permission :

resource "aws_lambda_permission" "example" {
  statement_id  = "AllowExecutionFromS3Bucket"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.writeUsersToDB.function_name
  principal     = "s3.amazonaws.com"
  source_arn    = aws_s3_bucket.user_data.arn
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM