简体   繁体   中英

Azure private endpoints and Terraform

I'm trying to create a storage account with a private endpoint in an Azure subnet.

I ran into an issue like this after terraform apply :

Error creating Private Endpoint "dev-pe" (Resource Group "privateendpoint-rg"): network.PrivateEndpointsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="PrivateEndpointCannotBeCreatedInSubnetThatHasNetworkPoliciesEnabled" Message="Private endpoint /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/privateendpoint-rg/providers/Microsoft.Network/privateEndpoints/dev-pe cannot be created in a subnet /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/privateendpoint-rg/providers/Microsoft.Network/virtualNetworks/dev-vnet/subnets/dev-storage-subnet since it has private endpoint network policies enabled." Details=[]

As you can see below, I've set enforce_private_link_endpoint_network_policies = false and played around with azurem_private_link_service too.

Here's my code:

resource "azurerm_resource_group" "example" {
  name     = "privateendpoint-rg"
  location = var.location
  tags     = local.common_tags
}

resource "azurerm_virtual_network" "example" {
  name                = "${var.environment}-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  tags                = local.common_tags
}

resource "azurerm_subnet" "storage" {
  name                                           = "${var.environment}-storage-subnet"
  resource_group_name                            = azurerm_resource_group.example.name
  virtual_network_name                           = azurerm_virtual_network.example.name
  address_prefix                                 = "10.0.1.0/24"
  enforce_private_link_endpoint_network_policies = false
  // enforce_private_link_service_network_policies = false
  // service_endpoints                              = ["Microsoft.Storage"]
}  

resource "random_integer" "sa_num" {
  min = 10000
  max = 99999
}

resource "azurerm_storage_account" "example" {
  name                     = "${var.adoit_number}${lower(var.environment)}${random_integer.sa_num.result}"
  resource_group_name       = azurerm_resource_group.example.name
  location                  = azurerm_resource_group.example.location
  account_tier              = "Standard"
  account_replication_type  = "LRS"
  enable_https_traffic_only = true
  tags                      = local.common_tags
}

resource "azurerm_storage_container" "example" {
  name                  = "acctestcont"
  storage_account_name  = azurerm_storage_account.example.name
  container_access_type = "private"
}

resource "azurerm_private_endpoint" "example" {
  name                = "${var.environment}-pe"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  subnet_id           = azurerm_subnet.storage.id

  private_service_connection {
    name                           = "${var.environment}-psc"
    is_manual_connection           = false
    private_connection_resource_id = azurerm_storage_account.example.id
    subresource_names              = ["blob"]
  }

}

If I change enforce_private_link_endpoint_network_policies = true I receive the following error:

Error creating Private Endpoint "dev-pe" (Resource Group "privateendpoint-rg"): network.PrivateEndpointsClient#CreateOrUpdate: Failure sending request: StatusCode=400 -- Original Error: Code="OperationNotAllowedOnKind" Message="The operation is not allowed on account kind Storage" Details=[]

OK, found it. If you want to connect a storage account to a private endpoint, the storage account has to of kind StorageV2 which looks in the Terraform code as follows:

resource "azurerm_storage_account" "example" {
  name                     = "${var.adoit_number}${lower(var.environment)}${random_integer.sa_num.result}"
  resource_group_name       = azurerm_resource_group.example.name
  location                  = azurerm_resource_group.example.location
  account_kind              = "StorageV2"
  account_tier              = "Standard"
  account_replication_type  = "LRS"
  enable_https_traffic_only = true
  tags                      = local.common_tags
}

You have set enforce_private_link_endpoint_network_policies = false which enables the policy. This is why you are getting this error.

Setting enforce_private_link_endpoint_network_policies to true will Disable the policy, and setting it to false will Enable the policy. See https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM