0
votes

Hi I want to trigger a lambda function from an s3 bucket on a any *.csv file upload. My lambda works fine i can run it. but seems like when i upload a csv to s3 the lambda is not triggered. below is the code for my s3 bucket notification

resource "aws_s3_bucket" "myfirst-s3-bucket" {
  bucket = "myfirst-s3-bucket"
}

resource "aws_s3_bucket_notification" "bucket_notification" {
  bucket = "${aws_s3_bucket.myfirst-s3-bucket.id}"

  lambda_function {
    lambda_function_arn = "${aws_lambda_function.lambda.arn}"
    events              = ["s3:ObjectCreated:*"]
    filter_suffix       = ".jpg"
  }
}
resource "aws_lambda_permission" "perme_bucket" {
  statement_id  = "AllowExecutionFromCloudWatch"
  action        = "lambda:InvokeFunction"
  function_name = "${aws_lambda_function.lambda.arn}"
  principal     = "s3.amazonaws.com"
  source_arn    = "${aws_s3_bucket.myfirst-s3-bucket.arn}"
}
1

1 Answers

0
votes
  • I think you should be using AllowExecutionFromS3Bucket and not AllowExecutionFromCloudWatch if you want to trigger it from S3 bucket.
  • Also the filter_suffix is for jpg and i think you want .csv

  • Please find the code on github that will be helpful github code

resource "aws_lambda_permission" "perme_bucket" {
 statement_id  = "AllowExecutionFromS3Bucket"
 action        = "lambda:InvokeFunction"
 function_name = "${aws_lambda_function.lambda.arn}"
 principal     = "s3.amazonaws.com"
 source_arn    = "${aws_s3_bucket.myfirst-s3-bucket.arn}"
}