hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.59k stars 4.63k forks source link

"ParentResourceNotFound" Message="Can not perform requested operation on nested resource. Parent resource 'hr-dev-ms-sql-server' not found. #18311

Open AvtsVivek opened 2 years ago

AvtsVivek commented 2 years ago

Is there an existing issue for this?

Community Note

Terraform Version

Terraform v1.2.9

AzureRM Provider Version

~> 3.0

Affected Resource(s)/Data Source(s)

N/A

Terraform Configuration Files

resource "azurerm_mssql_server" "mssql_server" {
  name                         = "${local.resource_name_prefix}-${var.mssql_server_name}"
  resource_group_name          = azurerm_resource_group.rg.name
  location                     = azurerm_resource_group.rg.location
  version                      = "12.0"
  administrator_login          = "adm1n157r470r"
  administrator_login_password = "4-v3ry-53cr37-p455w0rd"
}

resource "azurerm_mssql_database" "mssql_database" {
  name           = "${local.resource_name_prefix}-${var.mssql_database_name}"
  server_id      = azurerm_mssql_server.mssql_server.id
  collation      = "SQL_Latin1_General_CP1_CI_AS"
  license_type   = "LicenseIncluded"
  max_size_gb    = 1
  read_scale     = false
  sku_name       = "S0"
  zone_redundant = false
  depends_on     = [azurerm_mssql_server.mssql_server]
  tags           = local.common_tags
}

The full config files are here.

So the parent resource here is the sql server(azurerm_mssql_server.mssql_server) and the child is database(azurerm_mssql_database.mssql_database) to be created on the server.

The child has an implicit dependency on the parent. The parent gets created, but still when its the time for the child, terraform says that the parent is not found.

Debug Output/Panic Output

azurerm_mssql_database.mssql_database: Creating...
╷
│ Error: creating/updating Database: (Name "hr-dev-ms-sql-db" / Server Name "hr-dev-ms-sql-server" / Resource Group "hr-dev-rg-vivrag-dtdwxd"): sql.DatabasesClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ParentResourceNotFound" Message="Can 
not perform requested operation on nested resource. Parent resource 'hr-dev-ms-sql-server' not found."
│
│   with azurerm_mssql_database.mssql_database,
│   on tf6-02-sql-server-db.tf line 20, in resource "azurerm_mssql_database" "mssql_database":
│   20: resource "azurerm_mssql_database" "mssql_database" {
│
╵

The full gist is here.

Expected Behaviour

The sql database on the ms sql server should get created without errors.

Actual Behaviour

azurerm_mssql_database.mssql_database: Creating... ╷ │ Error: creating/updating Database: (Name "hr-dev-ms-sql-db" / Server Name "hr-dev-ms-sql-server" / Resource Group "hr-dev-rg-vivrag-dtdwxd"): sql.DatabasesClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ParentResourceNotFound" Message="Can not perform requested operation on nested resource. Parent resource 'hr-dev-ms-sql-server' not found." │ │ with azurerm_mssql_database.mssql_database, │ on tf6-02-sql-server-db.tf line 20, in resource "azurerm_mssql_database" "mssql_database": │ 20: resource "azurerm_mssql_database" "mssql_database" { │ ╵

Steps to Reproduce

terraform fmt

terraform init

terraform validate

terraform plan -out main.tfplan

terraform apply main.tfplan 

The command list is below.

https://github.com/AvtsVivek/Az204WthTerraform/blob/main/src/tf-files/650500-sql-db-with-tf/commands.sh
# Terraform Block
terraform {
  required_version = "~> 1.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }

    random = {
      source  = "hashicorp/random"
      version = "~> 3.0"
    }
  }
}

# Provider Block
provider "azurerm" {
  features {}
}

Full file is here.

Important Factoids

N/A

References

Similar issue is explained here on the ms docs, but this about bicep and not terraform.

Now found a work around and seems to be working.

Based on the sleep terraform timer resource here, I added a timer to the config.

resource "time_sleep" "wait_for_some_time" {
  depends_on      = [azurerm_mssql_server.mssql_server]
  create_duration = "30s"
}

The full set of new configs are here

You can see the time_sleep resource here.

Also I found similar errors with another example.

The terraform configuration files are here.

When I plan and apply, I get the following error.

azurerm_virtual_machine_extension.iis-windows-vm-extension: Creating...
╷
│ Error: compute.VirtualMachineExtensionsClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ParentResourceNotFound" Message="Can not perform requested operation on nested resource. Parent resource 'hr-dev-winvm' not found."
│
│   with azurerm_virtual_machine_extension.iis-windows-vm-extension,
│   on tf7-05-web-windows-vm-resource.tf line 30, in resource "azurerm_virtual_machine_extension" "iis-windows-vm-extension":
│   30: resource "azurerm_virtual_machine_extension" "iis-windows-vm-extension" {
│
╵

So here the child resource azurerm_virtual_machine_extension should be created after the parent resource azurerm_windows_virtual_machine is created.

But unless I have sleep timer setup, its giving me the above error. I had to keep azurerm_virtual_machine_extension waiting for 6 minutes, and then only its successful. 30 or 60 sec wait did not work. You can see that resource in this file here.

My conclusion is, we have to wait for an indefinite amount of time after the parent is created and then execute the plan and apply commands. We can use the time_sleep resource but the time we need to specify is indefinite. It could be 30s or it can be as high as half hour. So the way I am working now is, execute plan and apply repeatedly till at one point, finally the child is successfully gets created without error.

Is it that I am missing something?

sinbai commented 2 years ago

@AvtsVivek thanks for opening this issue here. Unfortunately, I could not reproduce this issue with the blow tf configuration. It works fine on my local. Could you reproduce it with the following configuration?


terraform {
  # required_version = ">= 1.0.0"
  # The right most version upgrade is allowed by the following.
  # So for production, the following should be used with ~> 1.0.0
  # https://stackoverflow.com/q/72975317/1977871
  required_version = "~> 1.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }

    random = {
      source  = "hashicorp/random"
      version = "~> 3.0"
    }
  }
}

# Generic Input Variables
# Business Division
variable "business_divsion" {
  description = "Business Division in the large organization this Infrastructure belongs"
  type        = string
  default     = "hr"
}
# Environment Variable
variable "environment" {
  description = "Environment Variable used as a prefix"
  type        = string
  default     = "dev"
}

# Azure Resource Group Name 
variable "resource_group_name" {
  description = "Resource Group Name"
  type        = string
  default     = "rg-vivrag"
}

# Azure Resources Location
variable "resource_group_location" {
  description = "Region in which Azure Resources to be created"
  type        = string
  default     = "eastus"
}

locals {
  owners               = var.business_divsion
  environment          = var.environment
  resource_name_prefix = "${var.business_divsion}-${var.environment}"
  #name = "${local.owners}-${local.environment}"
} 

variable "mssql_server_name" {
  description = "Ms Sql server name"
  type        = string
  default     = "ms-sql-server0909"
}

variable "mssql_database_name" {
  description = "Ms Sql Database Name "
  type        = string
  default     = "ms-sql-db0909"
}

# Provider Block
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  name     = "18311"
  location = "eastus"
}

resource "azurerm_mssql_server" "mssql_server" {
  name                         = "${local.resource_name_prefix}-${var.mssql_server_name}"
  resource_group_name          = azurerm_resource_group.rg.name
  location                     = azurerm_resource_group.rg.location
  version                      = "12.0"
  administrator_login          = "adm1n157r470r"
  administrator_login_password = "4-v3ry-53cr37-p455w0rd"
}

resource "azurerm_mssql_database" "mssql_database" {
   name           = "${local.resource_name_prefix}-${var.mssql_database_name}"
  server_id      = azurerm_mssql_server.mssql_server.id
  collation      = "SQL_Latin1_General_CP1_CI_AS"
  license_type   = "LicenseIncluded"
  max_size_gb    = 1
  read_scale     = false
  sku_name       = "S0"
  zone_redundant = false
  depends_on     = [azurerm_mssql_server.mssql_server]
  tags = {
    environment = "dev",
    owners = "hr"
  }
}
AvtsVivek commented 2 years ago

Hi @sinbai

I ran the configuration, and I got the same error.

azurerm_mssql_database.mssql_database: Creating...
╷
│ Error: creating/updating Database: (Name "admin-staging-ms-sql-db0909" / Server Name "admin-staging-ms-sql-server0909" / Resource Group "18311"): sql.DatabasesClient#CreateOrUpdatrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ParentResourceNotFound" Message="Can not perform requested operation on nested resource. Parent resouc in-srce 'admin-staging-ms-sql-server0909' not found."
│
│   with azurerm_mssql_database.mssql_database,
│   on tfconfig.tf line 87, in resource "azurerm_mssql_database" "mssql_database":
│   87: resource "azurerm_mssql_database" "mssql_database" {
│
╵

You can see in the screen shot below. You can see the current version as well.

image

The server gets created successfully, I can see in the portal. But the database is the problem.

image

Is there any problem with AzureRm Version mismatch? Also please note, I am on Windows 10 machine.

Terraform v1.2.9
on windows_amd64
+ provider registry.terraform.io/hashicorp/azurerm v3.22.0
+ provider registry.terraform.io/hashicorp/random v3.4.3

Do let me know if you want me to try anything else.

And here are the files I used.

zucler commented 2 years ago

We've been experiencing similar issues when deploying azurerm_virtual_machine_extension resource.

╷
│ Error: compute.VirtualMachineExtensionsClient#CreateOrUpdate: Failure sending request: StatusCode=404 -- Original Error: Code="ParentResourceNotFound" Message="Can not perform requested operation on nested resource. Parent resource 'Source-0' not found."
│ 
│   with azurerm_virtual_machine_extension.winboot-ext[0],
│   on main.tf line 143, in resource "azurerm_virtual_machine_extension" "winboot-ext":
│  143: resource "azurerm_virtual_machine_extension" "winboot-ext" {
│ 
╵

however, about 1 minute before, the log states:

azurerm_windows_virtual_machine.sourcevm[0]: Creation complete after 57s [id=/subscriptions/************************************/resourceGroups/NubevaRR_ci5251/providers/Microsoft.Compute/virtualMachines/Source-0]

It has been working fine for ages before, something has changed around 10 days ago.

mdurell commented 2 years ago

I too am experiencing intermittent ParentResourceNotFound errors when deploying azurerm_virtual_machine_extension resources. I will try and add the wait resource hack listed above. I agree with the timeframe indicated as well, this started for me a few weeks ago.

Upanshu11 commented 2 years ago

Experiencing the same issue with azurerm_virtual_machine_extension. Any resolution?

RavinderReddyF5 commented 2 years ago

Experiencing same issue with .azurerm_virtual_machine_extension.

stbenjam commented 2 years ago

We are experiencing the same issue, but for a different resource (private dns zone) https://github.com/hashicorp/terraform-provider-azurerm/issues/18350

We also dated the problem to begin around the same time (sometime during the day on 9/1).

    date    | azure_infrastructure_failure_percentage 
------------+----------------------------------------
 2022-08-15 |  5.61
 2022-08-16 |  8.24
 2022-08-17 |  7.53
 2022-08-18 |  4.59
 2022-08-19 |  6.35
 2022-08-20 |  7.51
 2022-08-21 |  3.92
 2022-08-22 |  6.49
 2022-08-23 |  4.38
 2022-08-24 |  7.81
 2022-08-25 |  4.86
 2022-08-26 |  3.55
 2022-08-27 |  6.09
 2022-08-28 | 10.80
 2022-08-29 |  7.75
 2022-08-30 |  3.09
 2022-08-31 |  4.63
 2022-09-01 | 15.88
 2022-09-02 | 26.43
 2022-09-03 | 24.82
 2022-09-04 | 22.50
 2022-09-05 | 31.56
 2022-09-06 | 25.00
 2022-09-07 | 26.02
 2022-09-08 | 32.62
 2022-09-09 | 30.62
 2022-09-10 | 39.23
 2022-09-11 | 44.44
 2022-09-12 | 29.48
 2022-09-13 | 27.09
 2022-09-14 | 25.15
 2022-09-15 | 26.89
 2022-09-16 | 13.94
 2022-09-17 | 15.92
 2022-09-18 | 16.05
 2022-09-19 | 14.65
 2022-09-20 | 19.27
 2022-09-21 | 19.83
 2022-09-22 | 19.84
 2022-09-23 | 22.87
 2022-09-24 | 22.09
 2022-09-25 | 26.14
 2022-09-26 | 20.08
learnprofile commented 2 years ago

we are also facing this issue and spoke to Microsoft engineering ...waiting for their feedback,