How to create multiple custom domain names for API Management with api_management_custom_domain resource?



variable "gateway_custom_domains" {
  description = "List of custom domains for the Gateway."
  type = list(object({
    host_name                    = string
    certificate_id               = string
    negotiate_client_certificate = optional(bool, false)
    default_ssl_binding          = optional(bool, false)
    })
  )
  default = []
}

resource "azurerm_api_management_custom_domain" "gateway" {
  for_each   = { for domain in var.gateway_custom_domains : domain.host_name => domain }
  api_management_id = azurerm_api_management.apim.id

  gateway {
    host_name                   = each.value.host_name
    key_vault_id                = each.value.certificate_id
    negotiate_client_certificate = try(each.value.negotiate_client_certificate, false)
    default_ssl_binding          = try(each.value.default_ssl_binding, false)
  }
}

module "apim_01" {
  source              = ...
  
  ...

  gateway_custom_domains = [
    {
      host_name = "gw1.example.com"
      certificate_id = azurerm_key_vault_certificate.apimgw1.versionless_secret_id
      negotiate_client_certificate    = false
      default_ssl_binding             = true
    },
    {
      host_name = "gw2.example.com"
      certificate_id = azurerm_key_vault_certificate.apimgw2.versionless_secret_id
      negotiate_client_certificate    = false
      default_ssl_binding             = false
}

I have this Terraform code to create custom domain names for API Management, but I couldn´t get this working when passing multiple objects to the module, as the first one gets created but the others fail with a conflict error:

│ Error: creating/updating Custom Domain: (Name "default" / Service Name "apim-weu-01" / Resource Group "rg-weu-01"): performing CreateOrUpdate: unexpected status 409 (409 Conflict) with error: ServiceLocked: The API Service apim-weu-01 is transitioning at this time. Please try the request again later. 
1865│ 
1866│ with module.apim_01.azurerm_api_management_custom_domain.custom_domains["gw2.example.com"], 
1867│ on .terraform/modules/apim_01/custom-domains.tf line 1, in resource "azurerm_api_management_custom_domain" "custom_domains": 
1868│ 1: resource "azurerm_api_management_custom_domain" "custom_domains" { 
1869│ 
1870╵ 
1871Error: Terraform exited with code 1. 
1872Error: Process completed with exit code 1.

Thanks.

It looks like Terraform makes the api calls in the for_each iteration before the Apim instance gets available and I believe that there is no way to control de for_each to create subsequentially. I searched in the azurerm provider issue, it is wierd that there is nobody complaining about, I am afraid that I am doing something wrong. Is there any way to prevent this?

I also tried to create a dedicated module that would just create the custom domain name and then the customer would be able to call it multiple times to create multiple custom domain names, that is its requirement, but then the first one gets created and the second fails saying that the resource already exist and needs to be imported in the state. It seems that the resource refer to the custom domain name in the state with a reference to the apim instance and not to the custom domain itself and then it generates the error. Have smeone faced something similar and could solve that? Again there is no one complaining about it and this sounds wierd.

Any help would be appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật