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"
}
}
}
data "external" "something" { ... }that runs an arbitrary shell / aws cli command and then operate based on that result.