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.53k stars 4.61k forks source link

ResourceNotFound when create azurerm_storage_account #10872

Closed puteulanus closed 1 year ago

puteulanus commented 3 years ago

Community Note

Terraform (and AzureRM Provider) Version

Terraform v0.14.7 azurerm v2.50.0

Affected Resource(s)

Terraform Configuration Files

resource "azurerm_storage_account" "dev" {
  name                     = "somenamedevqastorageaccount"
  resource_group_name      = azurerm_resource_group.dev.name
  location                 = azurerm_resource_group.dev.location
  account_tier             = "Standard"
  account_replication_type = "RAGRS"
  account_kind             = "StorageV2"

  static_website {
    index_document = "index.html"
  }

  tags = {
    environment = "staging"
  }
}

Debug Output

Error: Error updating Azure Storage Account `static_website` "somenamedevqastorageaccount": accounts.Client#SetServiceProperties: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceNotFound" Message="The specified resource does not exist.\nRequestId:06b940c6-701e-001d-7ef2-13c245000000\nTime:2021-03-08T08:13:43.9389228Z"

blob endpoint:

<Error>
<Code>ResourceNotFound</Code>
<Message>The specified resource does not exist. RequestId:f3e735fb-701e-0022-13ed-130ae6000000 Time:2021-03-08T07:35:20.9311641Z</Message>
</Error>

Expected Behaviour

Create a storage account can be used normally.

Actual Behaviour

  1. Storage account create failed when add static_website
  2. Remove static_website, create success, but get error in static website tab and cors tab in web portal
  3. The resource is listed in web portal, but its blob endpoint return ResourceNotFound
  4. Not 100% reproducible, small probability of create success in step 1

Steps to Reproduce

  1. terraform apply

Important Factoids

image

image

References

favoretti commented 3 years ago

Hey, thank you for reporting this. Sounds like Azure API bug to be honest.

puteulanus commented 3 years ago

Update: It may cause by repeated creation and destruction of resources with a fixed name.

Solution: Add random string to resource name.

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.50.0"
    }

    random = {
      source = "hashicorp/random"
      version = "2.3.0"
    }
  }
}

resource "random_string" "resource_code" {
  length  = 5
  special = false
  upper   = false
}

resource "azurerm_storage_account" "dev" {
  name                     = "somenamestorage${random_string.resource_code.result}"
  resource_group_name      = azurerm_resource_group.dev.name
  location                 = azurerm_resource_group.dev.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  account_kind             = "StorageV2"

  static_website {
    index_document = "index.html"
  }

  tags = {
    environment = "staging"
  }
}

It fixed the problem in my case.

smits23 commented 3 years ago

Is there a work around or stable TF version and Azure arm version that I can use as a workaround for this bug?

While creating container after storage account I am getting error 404 resource not found where as TF apply state Storage account created.

smits23 commented 3 years ago

Error on my TF apply stage: module.storage.azurerm_storage_account.storage: Still creating... [10s elapsed] module.storage.azurerm_storage_account.storage: Still creating... [20s elapsed] module.storage.azurerm_storage_account.storage: Creation complete after 26s [id=xxxxxxxxx] module.storage.azurerm_storage_container.container: Creating...

Error: failed creating container: failed creating container: containers.Client#Create: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceNotFound" Message="The specified resource does not exist.\nRequestId:81bb6095-a01e-0049-0512-419855000000\nTime:2021-05-04T18:20:43.7789311Z"

therodfather commented 3 years ago

I'm having the same error on TF apply:

ā”‚ Error: Error retrieving Azure Storage Account "livepushlpsdev1": storage.AccountsClient#GetProperties: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="StorageAccountNotFound" Message="The storage account livepushlpsdev1 was not found." ā”‚ ā”‚ with azurerm_storage_account.to_monitor, ā”‚ on storage.tf line 1, in resource "azurerm_storage_account" "to_monitor": ā”‚ 1: resource "azurerm_storage_account" "to_monitor" { ā”‚

I've tried the suggestions listed here but no luck. Also my config is as follows:

resource "azurerm_storage_account" "to_monitor" { name = "${module.resource_info.lower_short_name}1" resource_group_name = azurerm_resource_group.group1.name location = azurerm_resource_group.group1.location account_tier = "Standard" account_replication_type = "LRS" enable_https_traffic_only = true account_kind = "StorageV2"

timeouts { create = "60m" }

identity { type = "SystemAssigned" }

network_rules { default_action = "Allow" ip_rules = [] bypass = ["AzureServices"] virtual_network_subnet_ids = [azurerm_subnet.example.id] }

tags = { environment = var.environment } }

therodfather commented 3 years ago

Error on my TF apply stage: module.storage.azurerm_storage_account.storage: Still creating... [10s elapsed] module.storage.azurerm_storage_account.storage: Still creating... [20s elapsed] module.storage.azurerm_storage_account.storage: Creation complete after 26s [id=xxxxxxxxx] module.storage.azurerm_storage_container.container: Creating...

Error: failed creating container: failed creating container: containers.Client#Create: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceNotFound" Message="The specified resource does not exist.\nRequestId:81bb6095-a01e-0049-0512-419855000000\nTime:2021-05-04T18:20:43.7789311Z"

If you have network rules on the storage account, try adding a service_endpoint to the subnet resource, it fixed my issue:

resource "azurerm_subnet" "example" { name = "${module.resource_info.upper_short_name}-SUBNET" resource_group_name = azurerm_resource_group.group1.name virtual_network_name = azurerm_virtual_network.example.name address_prefixes = ["10.0.1.0/24"] service_endpoints = [ "Microsoft.Storage" ] }

smits23 commented 3 years ago

@therodfather I dont have service end point however I removed the entire network acl from storage block and tired, it works sometimes and it fails most of the times with 404, reruinningthe pipeline the 2nd time passes.

esource "azurerm_storage_account" "storage" {
name = var.sa_name resource_group_name = var.test_rg_name location = var.location account_tier = var.account_tier account_replication_type = var.account_replication_type min_tls_version = "TLS1_2" enable_https_traffic_only = "true" tags = var.tags }

SA Network rules

resource "azurerm_storage_account_network_rules" "acl" { resource_group_name = var.test_rg_name storage_account_name = azurerm_storage_account.storage.name default_action = "Deny" bypass = [

                              "AzureServices"
                           ]

ip_rules = var.storage_account_ip_rules

}

alexandruanghel commented 3 years ago

The same thing happens with azurerm_storage_data_lake_gen2_filesystem, the example from docs doesn't work most of the time (the account is created but it errors out when creating the containers and it works when running it again):

azurerm_storage_account.example: Still creating... [20s elapsed]
azurerm_storage_account.example: Creation complete after 21s [id=/subscriptions/.../resourceGroups/example-resources/providers/Microsoft.Storage/storageAccounts/examplestorageacc]
azurerm_storage_data_lake_gen2_filesystem.example: Creating...

Error: Error creating File System "example" in Storage Account "examplestorageacc": datalakestore.Client#Create: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceNotFound" Message="The specified resource does not exist.\nRequestId:9d361ae2-...\nTime:2021-05-11T06:08:50.6863617Z"

Adding a 2 min time_sleep didn't help.

The only thing that worked was what @puteulanus originally suggested, adding a random string to the storage account name. This seems to be an issue with repeateadly destroying and recreating the storage account using the same name.

I've also tried various versions of this provider from 2.47 to 2.58 and none worked, so sounds like an Azure API issue but any such issue should be handled by this provider.

smits23 commented 3 years ago

@alexandruanghel I can confirm this works in Tf version 1.33. But thats way too old. My workaround was to use Arm with TF deployment. And that worked just fine.

magodo commented 1 year ago

This duplicates to #13070, especially just for recreating the same named storage account as is mentioned at: https://github.com/hashicorp/terraform-provider-azurerm/issues/10872#issuecomment-793501019.

rcskosir commented 1 year ago

Thanks for opening this issue. This was a problem in the 2.x version of the provider which is no longer actively maintained. If this is still an issue with the 3.x version of the provider please do let us know by opening a new issue, thanks!

DmytroFesyk commented 1 year ago

Hi I faced with this issue Saved the plan to: main.tfplan

To perform exactly these actions, run the following command to apply: terraform apply "main.tfplan" PS C:\EPAM\AZ-cources\cloudx-java-azure-dev\petstore\terra\pet-storage-modules> terraform apply main.tfplan module.azurerm_storage_account.azurerm_storage_account.sa: Destroying... [id=/subscriptions/cc3d673b-7b49-4b4a-8034-d02ef8cf8167/resourceGroups/pet-storage/providers/Microsoft.Storage/storageAccounts/petstoragestorageaccount] module.azurerm_storage_account.azurerm_storage_account.sa: Destruction complete after 4s module.azurerm_storage_account.azurerm_storage_account.sa: Creating... module.azurerm_storage_account.azurerm_storage_account.sa: Still creating... [10s elapsed] module.azurerm_storage_account.azurerm_storage_account.sa: Still creating... [20s elapsed] ā•· ā”‚ Error: retrieving share properties for Storage Account (Subscription: "cc3d673b-7b49-4b4a-8034-d02ef8cf8167" ā”‚ Resource Group Name: "pet-storage" ā”‚ Storage Account Name: "petstoragestorageaccount"): storage.FileServicesClient#GetServiceProperties: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="ResourceNotFound" Message="The specified resource does not exist.\nRequestId:63aee3e1-601a-0072-2d73-daa9ff000000\nTime:2023-08-29T12:25:17.5687769Z" ā”‚ ā”‚ with module.azurerm_storage_account.azurerm_storage_account.sa, ā”‚ on ..\storage-account\main.tf line 1, in resource "azurerm_storage_account" "sa": ā”‚ 1: resource "azurerm_storage_account" "sa" {

My TF file

resource "azurerm_storage_account" "sa" { name = var.name resource_group_name = var.resource_group_name location = var.location account_tier = var.account_tier account_replication_type = var.account_replication_type allow_nested_items_to_be_public = false blob_properties{ versioning_enabled = true } }

rruenroeng commented 12 months ago

This thread is getting a little confused. I think that @therodfather raised a different error than what was originally brought up by @puteulanus. These have different solutions.

I think that @therodfather and @smits23 were running into the issue brought up in this thread.

I think @puteulanus and @DmytroFesyk were running into an issue related to this issue. Somewhere along the lines, Terrraform flagged the storage accounts as tainted and is trying to delete them and then immediately stand them up. It takes a bit of time to delete storage accounts, so we wind up going in circles and throwing 404 errors.

github-actions[bot] commented 4 months ago

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.