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

path.module and path.root still return absolute paths in terraform console #21438

Closed
rjullman opened this issue May 24, 2019 · 6 comments
Closed

Comments

@rjullman
Copy link

Terraform Version

Terraform v0.12.0
+ provider.aws v2.12.0

Terraform Configuration Files

provider "aws" {
  region  = "us-east-1"
  version = "~> 2.12.0"
}

resource "aws_db_instance" "default" {
  identifier     = "${basename(path.module)}-db"
  instance_class = "db.t3.micro"
}

Crash Output

N/A

Expected Behavior

Passes validation.

Actual Behavior

Error: only lowercase alphanumeric characters and hyphens allowed in "identifier"

  on main.tf line 6, in resource "aws_db_instance" "default":
   6: resource "aws_db_instance" "default" {

Error: first character of "identifier" must be a letter

  on main.tf line 6, in resource "aws_db_instance" "default":
   6: resource "aws_db_instance" "default" {

Steps to Reproduce

  1. terraform init
  2. terraform apply

Additional Context

Replacing the basename function with variables or locals works fine (as long as they do not in turn use basename). I imagine the issue is related to basename not being evaluated before the validation logic applies.

@apparentlymart
Copy link
Contributor

Hi @rjullman! Thanks for reporting this.

The error messages here are coming from the AWS provider itself, but as you said this might be because the basename function is producing an unacceptable result.

In your case I think the situation is that path.module is, as of v0.12.0, a path relative to the current working directory, rather than an absolute path as before. If the module in question is the root module for your configuration and you're running it from its own directory then path.module would be . (current working directory), and thus basename(.) would also be ., which would create:

  identifier = ".-db"

Using the name of the containing directory is not a pattern we expected but we've seen a similar usage (for a different purpose) in #21400. While using the name of the containing directory feels a little "magical" for my tastes, I can see that you were using it to avoid writing the module name again in here, and so I'd like to find a different way to meet that use-case. It's likely that a new abspath function, like proposed in #21400, so you'd be able to write this instead:

identifier     = "${basename(abspath(path.module))}-db"

@rjullman
Copy link
Author

Thanks for the help!

I did not realize that the path.module had changed. In terraform console it still prints the absolute path to the module. Does the console work differently than the final interpreter?

I agree that using the directory name in this way is (as you put it) magical. I've been eliminating this construct where possible (I removed all dependencies in the toy example above) and should be able to unblock myself here.

@apparentlymart
Copy link
Contributor

Indeed, I think terraform console is behaving a little differently here, and that's a bug to be fixed. Because terraform console works in a different way than most other Terraform commands (it's evaluating directly against the state, with no plan in progress, etc) some of its behaviors can potentially be different, and I expect we missed updating something in there while we were switching over to using relative paths for these.

@apparentlymart
Copy link
Contributor

Since we already have #21400 open, I think I'm going to repurpose this one to represent the fact that terraform console is behaving differently, since it seems like that was a key cause of you not being able to diagnose the problem here. We'll use #21400 to represent the need for abspath, since it sounds like you've taken a different approach that won't need it for this particular situation.

@apparentlymart apparentlymart changed the title TF 0.12 - validation fails on identifiers using built-in functions path.module and path.root still return absolute paths in terraform console May 24, 2019
@jbardin
Copy link
Member

jbardin commented Mar 31, 2021

Confirmed this is now working as expected in current releases.

Thanks!

@jbardin jbardin closed this as completed Mar 31, 2021
@ghost
Copy link

ghost commented May 1, 2021

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.

@ghost ghost locked as resolved and limited conversation to collaborators May 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants