hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.87k stars 9.21k forks source link

Error creating Lambda event source mapping: Function does not exist #22373

Open Rustege opened 2 years ago

Rustege commented 2 years ago

Community Note

Terraform CLI and Terraform AWS Provider Version

$ terraform -v
Terraform v0.13.7
+ provider registry.terraform.io/hashicorp/aws v3.55.0
+ provider registry.terraform.io/hashicorp/local v1.2.2
+ provider registry.terraform.io/hashicorp/random v3.1.0
+ provider registry.terraform.io/hashicorp/template v2.2.0

Affected Resource(s)

Terraform Configuration Files

Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.

data "aws_s3_bucket_object" "function_package" {
  bucket = local.lambda_s3_bucket
  key    = var.lambda_zip_file_key
}

resource "aws_lambda_function" "lambda" {
    for_each          = local.lambdas_key_set
    s3_bucket         = local.lambda_s3_bucket
    s3_key            = var.lambda_zip_file_key
    s3_object_version = data.aws_s3_bucket_object.function_package.version_id
    function_name     = "${var.environment}-${var.app_name}-${each.key}"
    description       = var.lambda_details[each.key].lambda_description
    runtime           = var.lambda_details[each.key].runtime
    handler           = var.lambda_details[each.key].handler
    kms_key_arn       = var.kms_arn
    memory_size       = lookup(var.lambda_details[each.key], "memory_size", "128")
    timeout           = lookup(var.lambda_details[each.key], "timeout", "3")
    role              = lookup(var.lambda_details[each.key], "role", var.workspace_lambda_roles[terraform.workspace])
    publish           = var.is_lambda_published

  }

resource "aws_lambda_event_source_mapping" "lambda_event_source" {
    for_each = local.lambdas_key_set

    event_source_arn = var.lambda_details[each.key].event_source_mapping.event_source_arn
    function_name    = "${var.environment}-${var.app_name}-${var.lambda_details[each.key].function_name}"
    batch_size       = var.lambda_details[each.key].event_source_mapping.batch_size
    enabled          = var.lambda_details[each.key].event_source_mapping.enabled

    depends_on = [aws_lambda_function.lambda]
  }

Debug Output

$ terraform apply tfplan.out
random_id.id: Creating...
random_id.id2: Creating...
random_id.id2: Creation complete after 0s [id=xHs]
random_id.id: Creation complete after 0s [id=dLQ]
aws_security_group.inbound_sg[0]: Creating...
aws_security_group.inbound_sg[0]: Still creating... [10s elapsed]
aws_security_group.inbound_sg[0]: Creation complete after 11s [id=sg-0eedf3baf49756e57]
aws_lambda_function.lambda["lambda-sqs-test"]: Creating...
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [10s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [20s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [30s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [40s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [50s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [1m0s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [1m10s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [1m20s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [1m30s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [1m40s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [1m50s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [2m0s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [2m10s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [2m20s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [2m30s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [2m40s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [2m50s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [3m0s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [3m10s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [3m20s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [3m30s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [3m40s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Still creating... [3m50s elapsed]
aws_lambda_function.lambda["lambda-sqs-test"]: Creation complete after 3m53s [id=deve-inspec-lambdas-mod-aws-test-lambda-sqs-test]
Error: error creating Lambda Event Source Mapping (arn:aws:sqs:us-east-1:xxxxxxxxx:deve-reid-example-lambda-function-event-appq): InvalidParameterValueException: Function does not exist
       {
         RespMetadata: {
           StatusCode: 400,
           RequestID: "265da151-044e-4273-9dbe-d9afbd3d1a5d"
         },
         Message_: "Function does not exist",
         Type: "User"
       }

         on lambda.tf line 61, in resource "aws_lambda_event_source_mapping" "lambda_event_source":
        61: resource "aws_lambda_event_source_mapping" "lambda_event_source" {

Panic Output

Expected Behavior

Lambda Event Source Mapping should be created without any issue

Actual Behavior

Lambda Event Source Mapping creation fails

Steps to Reproduce

  1. terraform init --upgrade
  2. terraform plan -var-file=testing.tfvars -out=tfplan.out
  3. terraform apply tfplan.out (This would fail with function does not exists)
  4. terraform plan -var-file=testing.tfvars -out=tfplan.out
  5. terraform apply tfplan.out (Rerunning with same configuration works)

Important Factoids

Creation of Lambda SQS Event Source mapping should start after Lambda is CREATED; Even though aws_lambda_event_source_mapping depends_on aws_lambda_function, I'm receiving Function does not exist; (Lambda was created without any issue); Rerunning terraform plan/apply works; I believe there are some wait state needed though we mentioned _dependson; And this issue occurs at random intervals(not always)

References

Rustege commented 2 years ago

This is still happening with terraform version 1.0.11 and AWS Provider 4.20.0; Is there any update on this

anmshepm commented 2 years ago

This just happened to us with version 1.2.6 and AWS provider 4.9.0. Could someone please provide some insight into this?

@Rustege were you ever able to solve it?

callum-spencer commented 1 year ago

Just experienced this problem, did anyone manage to solve it?