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.6k stars 4.64k forks source link

azurerm_redis_cache - not creating instances in paralllel #14023

Open tonythomasm opened 3 years ago

tonythomasm commented 3 years ago

Community Note

Description

We are using azurerm_redis_cache to create azure redis instances. We need 4 instances to be created in paralllel by passing list of resource names to for_each meta-argument (Terraform template is given below). But the instances are being created sequentially. Its taking 13 mins on an avg to create one instance, thus in total ~40 mins to create 4.

The instances are being created in parallel while trying manually from portal and by using azurerm_resource_group_template_deployment terraform resource. That means deployments will take care of creating the instances in parallel and all instances are ready to use in 12 mins.

So is there any way to get the instances created in parallel using azurerm_redis_cache ?

Terraform (and AzureRM Provider) Version

azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.79.1"
    }

Affected Resource(s)

Terraform Configuration Files

resource "azurerm_redis_cache" "example" {
  for_each                      = { for instance in var.redis : instance.app_name => instance }
  name                = "${var.environment_name}-${each.value.app_name}-redis"
  location            = azurerm_resource_group.mcmp_rg.location
  resource_group_name = azurerm_resource_group.mcmp_rg.name
  capacity            = each.value.premium_capacity
  family              = "P"
  sku_name            = "Premium"
  minimum_tls_version = "1.2"
  # shard_count = 2
  public_network_access_enabled = false
  redis_version = each.value.engine_version
  subnet_id  = azurerm_subnet.private[0].id

  redis_configuration {
    aof_backup_enabled              = false
    rdb_backup_enabled = false
  }

}

Debug Output

module.mcmp-azure.azurerm_redis_cache.example["actions"]: Still creating... [13m0s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [13m0s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["actions"]: Still creating... [13m10s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [13m10s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["actions"]: Still creating... [13m20s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [13m20s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["actions"]: Still creating... [13m30s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [13m30s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [13m40s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["actions"]: Still creating... [13m40s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [13m50s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["actions"]: Still creating... [13m50s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [14m0s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["actions"]: Still creating... [14m0s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["actions"]: Creation complete after 14m4s [id=/subscriptions/47a855d0-c185-48a8-9b2d-ac12a1069ede/resourceGroups/test-cache/providers/Microsoft.Cache/Redis/test-cache-actions-redis]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [14m10s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [14m20s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [14m30s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [14m40s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [14m50s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [15m0s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [15m10s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [15m20s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [15m30s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [15m40s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [15m50s elapsed]
...
...
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [25m50s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [26m0s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [26m10s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [26m20s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [26m30s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [26m40s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [26m50s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [27m0s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Still creating... [27m10s elapsed]
module.mcmp-azure.azurerm_redis_cache.example["consume"]: Creation complete after 27m18s [id=/subscriptions/47a855d0-c185-48a8-9b2d-ac12a1069ede/resourceGroups/test-cache/providers/Microsoft.Cache/Redis/test-cache-consume-redis]

Expected Behaviour

Redis instances need to be created in parallel

Actual Behaviour

Terrafrom logs shows instances are being created in parallel. But its getting created in sequence.

owenfarrell commented 2 years ago

@tonythomasm - I took a quick look, and what you're experiencing is definitely explainable.

I see that you included subnet_id as part of the configuration you shared above. By including subnet_id, Terraform locks both the specified subnet and the virtual network associated with the subnet prior to creation.

https://github.com/hashicorp/terraform-provider-azurerm/blob/main/internal/services/redis/redis_cache_resource.go#L414-L427

That's a pattern that is pretty common throughout Terraform given that Azure doesn't allow concurrent changes to the networking stack. See #13564 as an example.

In looking at the Azure API, it doesn't look like there's a dedicated function to connect a cache to a subnet. So I don't see a way to safely convert this to a parallel provisioning process.