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

Error message without context when validating provider configuration #28482

Closed
gtmtech opened this issue Apr 22, 2021 · 3 comments · Fixed by #34595
Closed

Error message without context when validating provider configuration #28482

gtmtech opened this issue Apr 22, 2021 · 3 comments · Fixed by #34595
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code v0.15 Issues (primarily bugs) reported against v0.15 releases v1.3 Issues (primarily bugs) reported against v1.3 releases

Comments

@gtmtech
Copy link

gtmtech commented Apr 22, 2021

Terraform Version 0.15.0

I spent hours trying to track down an error because the error had no context as to what resource it related to. In this case, it was because I only had aliased providers (deliberately, I dont like "default" providers when doing multi-region terraforming because it can lead to errors creating resources in the wrong place). In my codebase I had a single resource (a data source of aws_caller_identity) defined, amongst 1000s of resources. There was rightly an error - but I had to individually take out files until I understood what the error was - a much more useful error message should be had here.

In addition, there is (in my view) a far more serious bug.... I defined no aws default provider , yet terraform made its own up. IE - in the example below, if AWS_DEFAULT_REGION is set to us-east-1 then the command works. This has no right to work if there is no generic aws provider set.

However this bug is actually about the error message - imagine seeing The argument "region" is required, but was not set and having no idea where in your huge codebase it is talking about. - Thanks

Terraform Configuration Files

provider "aws" {
  alias  = "me"
  region = "eu-west-1"
}
data "aws_caller_identity" "current" {}

Additional Context

Before running, make sure AWS_DEFAULT_REGION is unset.

Expected Behavior

I expected any error messages about the above to be more useful

Actual Behavior

Error message was:

╷
│ Error: Missing required argument
│ 
│ The argument "region" is required, but was not set.
╵

The difficulty with this error message was that plenty of resources may have a region attribute - and same with providers - I had no idea to which resource or provider it was referring.

Steps to Reproduce

Just the above

@gtmtech gtmtech added bug new new issue not yet triaged labels Apr 22, 2021
@alisdair alisdair added confirmed a Terraform Core team member has reproduced this issue v0.15 Issues (primarily bugs) reported against v0.15 releases and removed new new issue not yet triaged labels Apr 29, 2021
@alisdair
Copy link
Contributor

Hi @gtmtech, thanks for reporting this. I'm able to reproduce the issue.

I spent some time looking into how we might improve this today, and I haven't come to any solid conclusions. The source of the diagnostic you're seeing is here:

configVal, configBody, evalDiags := ctx.EvaluateBlock(configBody, configSchema, nil, EvalDataForNoInstanceKey)
diags = diags.Append(evalDiags)
if evalDiags.HasErrors() {
return diags
}

At this point, Terraform is evaluating the provider configuration for an implied provider. In this case, the configuration is missing (as there is no provider block), and we don't have context on which resource caused the implied provider to be required.

Given the language design constraints described in #28483, it's not clear to me what useful error message we could return here. The best I'm able to propose is that we check for a nil configuration when evaluation fails, and return an additional diagnostic reporting which provider has a missing configuration. For the case in this issue, that would result in something like:

╷
│ Error: Missing required argument
│
│ The argument "region" is required, but was not set.
╵
╷
│ Error: Missing provider configuration
│
│ No configuration found for provider["registry.terraform.io/hashicorp/aws"].
╵

This would at least tell you which provider has missing configuration, but I don't know how helpful that is. Ideally we'd be able to point to which resource(s) caused this provider to be required, but it's not clear to me how we might be able to do that without significant refactoring.

@alisdair alisdair added the explained a Terraform Core team member has described the root cause of this issue in code label Apr 29, 2021
@geoff-reason
Copy link

geoff-reason commented Jul 26, 2021

Hi @alisdair - using another account to comment as its hitting me again.

Yeah, my problem is I dont use default providers, because I want to be specific about all resources, and I dont want regional resources being accidentally created in the wrong region (e.g. s3 buckets) as its a pain to unpick and transfer region.

So I make sure AWS_DEFAULT_REGION Is unset, and I also make sure that there is no default region in ~/.aws/config - that way, if a resource has no "provider", then it will use the implied provider, and then bomb out with this message.

Sadly though, diagnosing on which resource is the missing provider attribute is extremely hard. I have found out that it could be in any of the resources, datasources, or even in none of them, and in a statefile resource which has been deleted in local codebase where is the culprit with the lack of provider definition.

What I would really like to see is the resource which is causing the error, either in the statefile or in the local codebase.

E.g.

│ Error: Missing required argument
│ 
│ The argument "region" is required, but was not set.
| in modules.foo.modules.bar.aws_foo_bar

That last line would be sooooo helpful, without it, I have to intelligently scan all my codebase plus the remote statefile and parse it manually to see where I might be missing a provider.

I raised a separate issue I believe saying cant we abandon the default provider and be nice and specific (defaults are generally ALWAYS an antipattern if you ask me, because they represent catchalls where a human has not been specific enough, but lead to sometimes unintentional (by virtue that the human coder has not specified exactly what should happen) and very difficult to resolve situations if you do multiregion or multi-iam stuff with your providers like I do).

However I think @jbardin and @apparentlymart said on #28483 that wouldn't be removed as everyone uses it. Removing the default region is the only way I've found to force my codebase to catch the examples where I havent set an explicit provider, but unfortunately the error message makes it extremely hard to track down the resource in question.

Copy link
Contributor

github-actions bot commented Mar 2, 2024

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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code v0.15 Issues (primarily bugs) reported against v0.15 releases v1.3 Issues (primarily bugs) reported against v1.3 releases
Projects
None yet
3 participants