Aws_lambda_function data source outputs qualified ARN for arn attribute

Hello,

I am trying to pull the arn from aws_lambda_function and pass to a policy:

data “aws_lambda_function” “auth” {
function_name = “${var.env}-session-name”
}

policy = <<EOF
{
“Version”: “2012-10-17”,
“Statement”: [
{
“Action”: “lambda:InvokeFunction”,
“Effect”: “Allow”,
“Resource”: “${data.aws_lambda_function.auth.arn}”
}
]
}
EOF

Unfortunately when I apply i am receiving an error:

aws_iam_role_policy.invocation_policy: Resource ‘data.aws_lambda_function.auth’ not found for variable ‘data.aws_lambda_function.auth.arn’

I have tried the work around found in: https://github.com/terraform-providers/terraform-provider-aws/issues/4446 with no success. Can anyone help point me in the right direction?

“Resource X not found” is a very common error in Terraform, and often obscures a different underlying error condition. In this case, I’m wondering if data.aws_lambda_function.auth actually found any Lambda function? Can you confirm that a Lambda function with the name ${var.env}-session-name actually exists for your value of ${var.env}?

One way to dig into this further is to run terraform apply on a simplified version of your Terraform configuration:

data “aws_lambda_function” “auth” {
   function_name = “${var.env}-session-name”
}

Also, be sure that you’re looking up the AWS Lambda function in the correct AWS region.

1 Like

You were 100% correct the lambda it was looking for did not exist in the AWS account. Thank you for your help.