1

We want to check whether a Lambda exists or not. So we can provide it as a parameter for a count to do some other stuff. However, when we are running the terraform plan and the lambda doesn't exist we get a fatal exception 404 ResourceNotFoundException.

locals { 
  #Try 1 fails
  myLambda_exist         = data.aws_lambda_function.existing != null
  #Try 2 fails
  myLambda_exist         = try(data.aws_lambda_function.existing, false)
  #Try 3 fails
  myLambda_exist         = can(data.aws_lambda_function.existing)
}

data "aws_lambda_function" "existing" {
  function_name = "MyLambda"
}

Exception

Error: error getting Lambda Function (MyLambda): ResourceNotFoundException: Function not found: arn:aws:lambda:region:XXXXXX:function:MyLambda
{
  RespMetadata: {
    StatusCode: 404,
    RequestID: "12345"
  },
  Message_: "Function not found: arn:aws:lambda:region:XXXXXX:function:MyLambda",
  Type: "User"
}

In this case, it's ok not having the lambda created yet!

Versions:

terraform {
  required_version = "=0.14.5"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "=3.11"
    }
  }
}

1
  • 3
    That is generally not how terraform and data sources work. They are for referencing something external, not for checking wether or not it exists. You can achieve what you want to achieve by writing a custom data "external" "something" { ... } that runs an arbitrary shell / aws cli command and then operate based on that result. Commented Aug 7, 2021 at 18:40

1 Answer 1

3

We want to check whether a Lambda exists or not.

You can't do this. Your data source will always error out if a resource does not exist. TF does not have any capability to check if some external resource exists or not. You would have to develop such a solution yourself, for example, using external data source.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.