hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.47k stars 9.51k forks source link

Importing azurerm_cognitive_deployment resources produces an unexpected status before attempting to import #35237

Open MohnJadden opened 4 months ago

MohnJadden commented 4 months ago

Is there an existing issue for this?

Community Note

Terraform Version

1.8.4

AzureRM Provider Version

3.104.2

Affected Resource(s)/Data Source(s)

azurerm_cognitive_deployment

Terraform Configuration Files

resource "azurerm_cognitive_deployment" "test-gpt4o-gs" {
  for_each             = var.environmentName
  name                 = "gpt-4o-gs"
  cognitive_account_id = azurerm_cognitive_account.test-cog[each.key].id
  model {
    format = "OpenAI"
    name   = "gpt-4o"
    version = "2024-05-13"
  }
  scale {
    type     = "Global-Standard"
    capacity = "100"
  }
  lifecycle {
    ignore_changes = [model,scale]
  }
}

Debug Output/Panic Output

The following text shows up in my command prompt/terminal session:
test-cog/deployments/companydeployment-gpt-4o";

Nothing changes or displays for a few seconds, then the import command runs as it normally would

Expected Behaviour

There should be some kind of status that indicates Terraform is parsing the command and/or checking the state

Actual Behaviour

The newline with the semicolon displays, with nothing changing for a few seconds, then the import command runs

Steps to Reproduce

  1. Create the resource in Azure
  2. Add the resource to the main.tf of the module
  3. Run terraform import 'module.modulename-openai-test.azurerm_cognitive_deployment.name-test-gpt4o[\"test\"]' "/subscriptions/mysubscription/resourceGroups/test-RG/providers/Microsoft.CognitiveServices/accounts/test-cog/deployments/companydeployment-gpt-4o"

Important Factoids

This is in a Windows system, hence the escape characters

References

No response

tombuildsstuff commented 4 months ago

Transferring this to hashicorp/terraform since this behaviour comes from Terraform Core

apparentlymart commented 4 months ago

Hi @MohnJadden! Sorry for this strange behavior, and thanks for reporting it.

Unfortunately since this issue relates to an object in your Azure account I think we will need more information from you to diagnose it.

It would be helpful if you would repeat the reproduction steps you described but set the environment variable TF_LOG=trace before you perform step 3, which will make Terraform show its internal logs of how it handled the import command. The results are typically too long to be readable in a GitHub issue comment, so I suggest creating a GitHub Gist containing the log text and then sharing a link to it here, which will then allow us to retrieve the full log text without any corruption.

Unfortunately I don't yet have a theory about why Terraform would be printing part of your import ID followed by a semicolon, because that doesn't match any intentional Terraform output I know of, but I'm hoping that the trace log will give some clues as to what part of Terraform is generating that message and then we'll have a better chance of figuring out what's going wrong here.

Thanks!

MohnJadden commented 4 months ago

@apparentlymart Gist is at https://gist.github.com/MohnJadden/c58cce94528bdade43277ef3aba5dd85 - LMK if I can provide anything further.

apparentlymart commented 4 months ago

Thanks for sharing that trace log, @MohnJadden!

Based on that I see that the Terraform interpreted the command line like this (using Go's conventions for printing a slice of strings):

[]string{
    "C:\\ProgramData\\chocolatey\\lib\\terraform\\tools\\terraform.exe",
    "import",
    "-no-color",
    "module.projectname-openai-test.azurerm_cognitive_deployment.projectname-test-gpt4o-gs-project2[\"test\"]",
    "/subscriptions/subscriptionguid/resourceGroups/projectnametest-test-RG/providers/Microsoft.CognitiveServices/accounts/projectnametest-test-cog/deployments/gpt-4o-gs-project2",
}

...which matches what I would've expected for this command line. (Sometimes PowerShell corrupts the command line before running an external command, but it doesn't seem to have done so in this case.)

Unfortunately I wasn't able to find the string test-cog/deployments/companydeployment-gpt-4o"; in the output. I was hoping to be able to correlate that output with some part of the log messages to narrow down what might be printing it, but I'm now wondering if I should conclude from this that the message was being printed by something else in your environment, before running Terraform.

Just to make sure I'm not making an incorrect assumption: can you verify that the errant extra text you mentioned in your original issue doesn't appear anywhere in the unredacted version of the trace log? I notice you deleted some parts before you shared it, which is fine, but I just want to make sure we didn't lose the part that's relevant to this issue as part of that redacting. :grinning:

MohnJadden commented 4 months ago

@apparentlymart I can confirm that the log has zero mention of the string companydeployment-gpt-4o"; in any part of the log. It only appears when the import is run and it makes no mention or other indication that anything is actually running.

This is a screenshot of what the actual output at issue looks like with details redacted: image

apparentlymart commented 4 months ago

Thanks @MohnJadden!

Unfortunately this leaves me quite stumped about what to try next. The fact that this message appears very early after you start running the command makes me wonder if this is being printed by something else in your environment before Terraform even begins running, but I don't know what to ask to try to prove or disprove that hypothesis. :thinking:

There are lots of messages produced by parts of Terraform's code that involve a quoted string followed by a semicolon, and so I can't feasibly visit them all, but after searching for code that might produce a message like that most of what I found were error messages and unit test assertions. Of those two only error messages would be possible to see in a released version of Terraform, but the behavior you reported doesn't involve any errors.

The fact that we only see what appears to be a fragment of the message is also baffling. Terraform CLI and Terraform Core don't know anything about the Azure provider's import ID syntax, and so would have no reason to split the string just before the test-cog token. Even if it did, the fact that there's no quote symbol at the start suggests that it's the overall message that's been truncated, rather than just the import id.

I'm afraid I've run out of ideas for this one. :confounded: