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 on Terraform Apply: CosmosDB account name already exists #3813

Closed
khaljimbo opened this issue Jul 9, 2019 · 7 comments
Closed

Error on Terraform Apply: CosmosDB account name already exists #3813

khaljimbo opened this issue Jul 9, 2019 · 7 comments

Comments

@khaljimbo
Copy link

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or "me too" comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform (and AzureRM Provider) Version

Terraform v0.12.3

  • provider.azurerm v1.31.0

Affected Resource(s)

azurerm_cosmosdb_account

Terraform Configuration Files

resource "azurerm_cosmosdb_account" "db" {
  name                = "${var.cosmosDBAccountName}"
  location            = "${var.location}"
  resource_group_name = "${var.resourceGroupName}"
  offer_type          = "Standard"
  kind                = "GlobalDocumentDB"

  enable_automatic_failover = true

  consistency_policy {
    consistency_level       = "Session"
  }

  geo_location {
    location          = "${var.failoverLocation}"
    failover_priority = 1
  }

  geo_location {
    prefix            = "${var.cosmosDBAccountName}"
    location          = "${var.location}"
    failover_priority = 0
  }
}

Expected Behavior

CosmosDB account should be created

Actual Behavior

Error: CosmosDB Account 1239website0918627public already exists, please import the resource via terraform import

on cosmosDBwithMongoDBAPI.tf line 6, in resource "azurerm_cosmosdb_account" "db":
6: resource "azurerm_cosmosdb_account" "db" {

Steps to Reproduce

Use simple terraform to create a cosmos DB account. No matter how many random characters are used to create a CosmosDB account it complains that the name already exists.

  1. terraform apply

References

I found issue #3739 which suggests this is fixed in version 1.31 of the azure provider but I am still seeing this error.

@centran
Copy link

centran commented Jul 23, 2019

I want to add that the error reported occurs the second time you run terraform apply. The first time you get an error like this:

Error: Error creating CosmosDB Account "#####" (Resource Group "#####"): Error creating/updating CosmosDB Account "#####" (Resource Group "#####"): documentdb.DatabaseAccountsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="BadRequest" Message="DatabaseAccount name '#####' already exists.\r\nActivityId: #####, Microsoft.Azure.Documents.Common/2.5.1"

Then if you run terraform apply you get the error orginally report about importing the resource. Checking the Azure portal, CLI, and API calls does not show the name anywhere.

I'm not sure this is a terraform issue. It seems that the Azure API starts to create the cosmosDB account, reserves the name, then fails. After that the name is reserved but not attached to your Azure subscription so you can't recover the name. You can't even try to use the name through the portal after it fails in terraform.

Maybe there is a bug with how the azurerm terraform provider calls the Azure API but I also feel Azure should not allow this behavior. There is probably a DoS exploit which can be carried out with this bug.

lemvik added a commit to lemvik/simple-chat-app that referenced this issue Aug 24, 2019
- Apparently Azure stuff is all-too-fragile and I had to create both
Cosmos DB and PostgreSQL twice.
- See: hashicorp/terraform-provider-azurerm#3813
lemvik added a commit to lemvik/simple-chat-app that referenced this issue Aug 29, 2019
- Apparently Azure stuff is all-too-fragile and I had to create both
Cosmos DB and PostgreSQL twice.
- See: hashicorp/terraform-provider-azurerm#3813
@dhsass
Copy link

dhsass commented Sep 23, 2019

We're getting this error on the first attempt to create the db. We even tried using a GUID as the name and got this error.

@jnunham
Copy link

jnunham commented Sep 27, 2019

@dhsass @khaljimbo Several people at my company have run in to this and discovered that if you reuse the account name for the geo_location prefix, this is what is causing the issue. 2 solutions to this:

#1. Remove the prefix altogether (this will just make the Azure API auto generate one for you)

#2. Follow the updated method on the Terraform documentation page, particularly:
prefix = "tfex-cosmos-db-${random_integer.ri.result}-customid"

Hope this fixes it for you!

@DaSenf
Copy link

DaSenf commented Apr 14, 2020

I still get the same errors all the time.
I tried all kind of "hello world"-configurations and location variations.

But whenever i only use one location, it says there is nothing available although i use one which is "recommened" in the Azure portal

when i use a failover location it says: Code="BadRequest" Message="Cannot add additional regions, since database account provision failed

@DaSenf
Copy link

DaSenf commented Apr 15, 2020

I´m stupid i used location names (like East Us) instead of location ids (like eastus) ...

@tombuildsstuff
Copy link
Contributor

👋

Taking a look through I believe @jnunham's comment above should resolve this - as such I'm going to close this issue for the moment

Thanks!

@ghost
Copy link

ghost commented Jul 4, 2020

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Jul 4, 2020
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

8 participants