Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_lambda_function data source doesn't fail when function doesn't exist #6966

Closed
apparentlymart opened this issue Dec 22, 2018 · 3 comments · Fixed by #7663
Closed

aws_lambda_function data source doesn't fail when function doesn't exist #6966

apparentlymart opened this issue Dec 22, 2018 · 3 comments · Fixed by #7663
Labels
bug Addresses a defect in current functionality. service/lambda Issues and PRs that pertain to the lambda service.
Milestone

Comments

@apparentlymart
Copy link
Contributor

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform Version

$ terraform --version
Terraform v0.12.0-dev
+ provider.aws v1.40.0-6-gb23683732-dev

I am currently testing with a v0.12.0 prerelease build, but I have verified that this bug is still present in master at the time I write this issue.

Affected Resource(s)

  • data.aws_lambda_function

Terraform Configuration Files

data "aws_lambda_function" "auth" {
  function_name = "anything-that-doesnt-exist"
}

Expected Behavior

As with all data sources, the read request should fail with an error if the requested function doesn't exist.

Actual Behavior

Because the data source shares the same read function with the corresponding managed resource type, a failure results in an invalid object (with all of its attributes empty) being left in the state.

https://github.com/terraform-providers/terraform-provider-aws/blob/25303d5f4b90adab9d0f08d0d7b397cace7c3532/aws/data_source_aws_lambda_function.go#L151-L154

This is annoying because it causes the problem to be hidden and crop up instead as difficult-to-debug downstream problems, such as errors about required fields on other resources not being populated.

In my case, I was using the arn attribute to populate an IAM policy, leading to the following unhelpful error:

MalformedPolicyDocument: Syntax errors in policy.
status code: 400, request id: 2b441449-0582-11e9-89f1-b9230e0005a0

This is resulting from it attempting to specify the Resource in the policy as the empty string, because data.aws_lambda_function.auth.arn isn't populated.

@apparentlymart apparentlymart added bug Addresses a defect in current functionality. service/lambda Issues and PRs that pertain to the lambda service. labels Dec 22, 2018
bflad added a commit that referenced this issue Feb 23, 2019
…nd refactor testing to check data source state values against the resource state values

References:
* #5812
* hashicorp/terraform#10810 (comment)
* #6966

Previously, the `aws_lambda_function` data source was utilizing the Read function from the `aws_lambda_function` resource. This legacy practice has longterm maintenance issues with missing schema and documentation updates.

Here we implement a fresh new Read function for the data source that includes the following changes:

* Properly error when Lambda Function is not found
* Always return the `arn` attribute as unqualified (e.g. without a qualifier or version suffix)
* Always return the `qualified_arn` attribute as qualified (e.g. with the qualifier or version suffix)
* Always return the `tags` attribute

The acceptance testing changes modernize and simplify the testing:

* Utilize `resource.TestCheckResourceAttrPair()` where possible to ensure data source state values match appropriate resource state values
* Consolidate random naming to single variable
* Only provision VPC resources in VPC specific test

Output from acceptance testing:

```
--- PASS: TestAccDataSourceAWSLambdaFunction_version (20.89s)
--- PASS: TestAccDataSourceAWSLambdaFunction_environment (22.75s)
--- PASS: TestAccDataSourceAWSLambdaFunction_alias (23.68s)
--- PASS: TestAccDataSourceAWSLambdaFunction_basic (23.76s)
--- PASS: TestAccDataSourceAWSLambdaFunction_layers (28.82s)
--- PASS: TestAccDataSourceAWSLambdaFunction_vpc (36.48s)
```
@bflad bflad added this to the v2.0.0 milestone Feb 23, 2019
@bflad
Copy link
Contributor

bflad commented Feb 23, 2019

Fix submitted: #7663 👍

@bflad
Copy link
Contributor

bflad commented Feb 24, 2019

The fix for this has been merged and will release in version 2.0.0 of the Terraform AWS Provider, likely in the next week or two. Thanks for reporting this, @apparentlymart!

@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/lambda Issues and PRs that pertain to the lambda service.
Projects
None yet
2 participants