terraform-aws-next-js icon indicating copy to clipboard operation
terraform-aws-next-js copied to clipboard

Terraform apply fails with error creating Lambda Event Source Mapping

Open ofhouse opened this issue 3 years ago • 6 comments

When setting up a new Next.js app with 0.9.0, the first apply may fail with the following error message:

Error: error creating Lambda Event Source Mapping (arn:aws:sqs:eu-central-1:430654418481:terraform-next-js-existing-cloudfront20210615141542329600000006): InvalidParameterValueException: The provided execution role does not have permissions to call ReceiveMessage on SQS
│ {
│   RespMetadata: {
│     StatusCode: 400,
│     RequestID: "a08ca594-80e3-4db8-8176-dc140f31565a"
│   },
│   Message_: "The provided execution role does not have permissions to call ReceiveMessage on SQS",
│   Type: "User"
│ }
│ 
│   with module.tf_next.module.statics_deploy.module.deploy_trigger.aws_lambda_event_source_mapping.this["sqs_source"],
│   on .terraform/modules/tf_next.statics_deploy.deploy_trigger/main.tf line 211, in resource "aws_lambda_event_source_mapping" "this":
│  211: resource "aws_lambda_event_source_mapping" "this" {

It seems like there is some race condition when the policy for the Lambda is created.

Workaround

You should be able to runterraform apply again and the stack creation would progreed without this error.

ofhouse avatar Jun 15 '21 15:06 ofhouse

Another error that can pop up:

╷
│ Error: error putting S3 Bucket Notification Configuration: InvalidArgument: Unable to validate the following destination configurations
│       status code: 400, request id: 1ZS0PHX7B4ZBZ8M8, host id: kkKv9mk5ouVUJxrmXYs0Wu2xIJ3YFNfib0yyC/8XvhkYfBVPoETA+HPzUP/LAuNrMcqdOX/IDiQ=
│ 
│   with module.tf_next.module.statics_deploy.aws_s3_bucket_notification.on_create,
│   on ../../modules/statics-deploy/main.tf line 22, in resource "aws_s3_bucket_notification" "on_create":
│   22: resource "aws_s3_bucket_notification" "on_create" {
│ 
╵

ofhouse avatar Jun 19 '21 19:06 ofhouse

Using depends_on = [ aws_iam_role_policy_attachment.EXAMPLE ] doesn't seem to fix the problem either.

thirdender avatar Aug 12 '21 17:08 thirdender

Yes, the problem is probably caused because we use SQS and S3 als event source for a single Lambda. To solve this issue the S3 workflow should also be connected to the SQS queue so that the Lambda is only invoked by SQS.

ofhouse avatar Aug 12 '21 18:08 ofhouse

I had a similar issue. I was using pnpm, do I didn't use yarn or npm at all. The worst part is that my packages use workspace dependencies, hence unresolvable by yarn nor npm. (pnpm workspace..)

Is it possible to not rely on yarn or npm ? I saw the code source underneath and it seems like next build function is using yarn or npm under the hood with a weird detection method (scrapping files, looking for a package-lock.json or yarn.lock)

IzioDev avatar Feb 12 '22 18:02 IzioDev

i can confirm this bug on 1.0.0-canary.5 and the workaround is working fine. thanks.

gipsen92 avatar Aug 24 '22 18:08 gipsen92

Another error that can pop up:

╷
│ Error: error putting S3 Bucket Notification Configuration: InvalidArgument: Unable to validate the following destination configurations
│       status code: 400, request id: 1ZS0PHX7B4ZBZ8M8, host id: kkKv9mk5ouVUJxrmXYs0Wu2xIJ3YFNfib0yyC/8XvhkYfBVPoETA+HPzUP/LAuNrMcqdOX/IDiQ=
│ 
│   with module.tf_next.module.statics_deploy.aws_s3_bucket_notification.on_create,
│   on ../../modules/statics-deploy/main.tf line 22, in resource "aws_s3_bucket_notification" "on_create":
│   22: resource "aws_s3_bucket_notification" "on_create" {
│ 
╵

So what is the workaround for this issue? I'm seeing the same while trying to configure an event notification with sns topic.

ranikaruna avatar Oct 03 '22 18:10 ranikaruna