Reference to undeclared resource - when resource exists

Hello

I have 3 terraform files. main,tf, sqs.tf and mapping.tf

main.tf - Creates lambda function
sqs.tf - Creates SQS queue
mapping.tf - creates - Lambda event source mapping

main.tf

resource "aws_lambda_function" "reminder_function" {
  ephemeral_storage {
    size = 512
  }

  function_name                  = "reminder-function"
  handler                        = "reminder.handler"
  filename                       = "./src/node.zip" 
  memory_size                    = 512
  package_type                   = "Zip"
  reserved_concurrent_executions = -1
  role                           = aws_iam_role.reminder.arn
  runtime                        = "nodejs16.x"
  source_code_hash               = "sdsdlsll2smekjdkdfjjd/02aYdnbRedk="
  timeout                        = 30
  tracing_config {
    mode = "PassThrough"
  }
}

sqs.tf

resource "aws_sqs_queue" "reminder_queue" {
  kms_data_key_reuse_period_seconds = 300
  max_message_size                  = 204800
  message_retention_seconds         = 360000
  name                              = "reminder-queue"
  sqs_managed_sse_enabled           = true
  visibility_timeout_seconds        = 30
}

mapping.tf

resource "aws_lambda_event_source_mapping" "reminder_function_mapping" {
  event_source_arn = aws_sqs_queue.reminder_queue.arn
  function_name    = aws_lambda_function.reminder_function.arn
}

When applying the project, I am getting the following message

Error: Reference to undeclared resource

  on mapping.tf line 3, in resource "aws_lambda_event_source_mapping" "reminder_function_mapping":
   3:   function_name    = aws_lambda_function.reminder_function.arn

A managed resource "aws_lambda_function" "reminder_function" has not
been declared in the root module.

Can anyone help me to understand what is wrong here?
Apparently I have function created, but for some reason aws_lambda_event_source_mapping ignores that.

P.S. -1
I am using Terraform Cloud, with VCS driven workspace

P.S. -2
I’ve tried to add depends_on... lambda function, with no luck

Thanks in advance

Hi @eduard.baghdasaryan,

I’m afraid I am not able to suggest a strong answer to this question with the information given. I agree that the configuration examples you shared ought to work, and so I suspect that the problem is instead that for some reason Terraform is not able to see your entire configuration.

I cannot say for certain why that would be, but here are some ideas of situations that might cause a problem like this:

  • You’ve created main.tf in your editor but you’ve not saved it since you added the new resource configuration, and so Terraform sees that file as either missing or empty.
  • The file exists on your local computer but is, for some reason, excluded from the package sent to HCP Terraform (formerly Terraform Cloud) for remote execution. One reason that could occur is if you had a .terraformignore file that excluded main.tf.
  • The file exists on your local computer but is not included in what you’ve committed to your version control repository. This is essentially the same as the previous idea but with the version control system ignoring the file instead of Terraform itself. For example, if you are using Git then perhaps the .gitignore file is excluding main.tf.

There are probably some other reasons why this could occur, but those are the ones that I thought of first and are hopefully relatively easy to check.

Hey @apparentlymart

Thanks for the reply

I’ve tried to run main.tf and sqs.tf, and only when Lambda function and SQS queues is ready, add mapping, and got the same message.
Bellow is my project’s structure

├── lambda
│   ├── main.tf
│   ├── sqs.tf
│   ├── mapping.tf

What information I can provide to help you better understand the situation?

FYI - My edit is set to automatically save changes

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.