最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

azure - Terraform import hostname binding (with count) - Stack Overflow

programmeradmin4浏览0评论

In Azure, I'm trying to import a custom hostname binding for my staging evironment using Terraform. Since my dev and test don't need staging, my staging slot has a count variable which is 1 for acc and prod.

Our anization is in the process of figuring out what rights to give to our pipeline to enable us to make hostname bindings ourselves. Until that time, the bindings are made by another team and imported.

This is part of our code:

variable "web_app_slot_count" {
  type        = number
  description = "Number of slots (0 or 1)"
  default     = 0
  # tfvars contains web_app_slot_count = 1
}

resource "azurerm_windows_web_app_slot" "staging" {
  count          = var.web_app_slot_count
  ...
}

resource "azurerm_app_service_custom_hostname_binding" "staging_abc_nl" {
  count               = var.web_app_slot_count
  hostname            = "${var.environment}-staging.rdc.nl"
  app_service_name    = "${module.appservice_abc.name}/staging"
  resource_group_name = data.azurerm_resource_group.app.name
  ssl_state           = "SniEnabled"
  thumbprint          = data.azurerm_key_vault_certificate.ssl_certificate_staging[0].thumbprint
}

import {
  id = "/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl"
  to = azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]
}

Within our pipeline, this code produces an error:

Providers required by state:

    provider[registry.terraform.io/hashicorp/azurerm]

/agent/_work/_tool/terraform/1.11.2/x64/terraform apply -auto-approve -input=false /agent/_work/2/Acceptance.tfplan
azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]: Importing... [id=/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl]
azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]: Import complete [id=/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl]
azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]: Destroying... [id=/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl]
azurerm_app_service_custom_hostname_binding._staging_abc_nl[0]: Destruction complete after 9s
azurerm_app_service_custom_hostname_binding._staging_abc_nl[0]: Creating...
╷
│ Error: creating/updating Custom Hostname Binding "acc-staging.abc.nl" (App Service "abc-acc-abc-we-app-001/staging" / Resource Group "abc-acc-we-app-rg-001"): web.AppsClient#CreateOrUpdateHostNameBinding: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: error response cannot be parsed: {"" '\x00' '\x00'} error: EOF
│ 
│   with azurerm_app_service_custom_hostname_binding.staging_abc_nl[0],
│   on compute.tf line 98, in resource "azurerm_app_service_custom_hostname_binding" "staging_abc_nl":
│   98: resource "azurerm_app_service_custom_hostname_binding" "staging_abc_nl" {
│ 
╵

##[error]Error: The process '/agent/_work/_tool/terraform/1.11.2/x64/terraform' failed with exit code 1
Finishing: Terraform Apply

What might be the cause of this error? How do I prevent it?

In Azure, I'm trying to import a custom hostname binding for my staging evironment using Terraform. Since my dev and test don't need staging, my staging slot has a count variable which is 1 for acc and prod.

Our anization is in the process of figuring out what rights to give to our pipeline to enable us to make hostname bindings ourselves. Until that time, the bindings are made by another team and imported.

This is part of our code:

variable "web_app_slot_count" {
  type        = number
  description = "Number of slots (0 or 1)"
  default     = 0
  # tfvars contains web_app_slot_count = 1
}

resource "azurerm_windows_web_app_slot" "staging" {
  count          = var.web_app_slot_count
  ...
}

resource "azurerm_app_service_custom_hostname_binding" "staging_abc_nl" {
  count               = var.web_app_slot_count
  hostname            = "${var.environment}-staging.rdc.nl"
  app_service_name    = "${module.appservice_abc.name}/staging"
  resource_group_name = data.azurerm_resource_group.app.name
  ssl_state           = "SniEnabled"
  thumbprint          = data.azurerm_key_vault_certificate.ssl_certificate_staging[0].thumbprint
}

import {
  id = "/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl"
  to = azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]
}

Within our pipeline, this code produces an error:

Providers required by state:

    provider[registry.terraform.io/hashicorp/azurerm]

/agent/_work/_tool/terraform/1.11.2/x64/terraform apply -auto-approve -input=false /agent/_work/2/Acceptance.tfplan
azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]: Importing... [id=/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl]
azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]: Import complete [id=/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl]
azurerm_app_service_custom_hostname_binding.staging_abc_nl[0]: Destroying... [id=/subscriptions/12341234-1234-1234-1234-123412341234/resourceGroups/abc-acc-we-app-rg-001/providers/Microsoft.Web/sites/abc-acc-abc-we-app-001/hostNameBindings/acc-staging.abc.nl]
azurerm_app_service_custom_hostname_binding._staging_abc_nl[0]: Destruction complete after 9s
azurerm_app_service_custom_hostname_binding._staging_abc_nl[0]: Creating...
╷
│ Error: creating/updating Custom Hostname Binding "acc-staging.abc.nl" (App Service "abc-acc-abc-we-app-001/staging" / Resource Group "abc-acc-we-app-rg-001"): web.AppsClient#CreateOrUpdateHostNameBinding: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: error response cannot be parsed: {"" '\x00' '\x00'} error: EOF
│ 
│   with azurerm_app_service_custom_hostname_binding.staging_abc_nl[0],
│   on compute.tf line 98, in resource "azurerm_app_service_custom_hostname_binding" "staging_abc_nl":
│   98: resource "azurerm_app_service_custom_hostname_binding" "staging_abc_nl" {
│ 
╵

##[error]Error: The process '/agent/_work/_tool/terraform/1.11.2/x64/terraform' failed with exit code 1
Finishing: Terraform Apply

What might be the cause of this error? How do I prevent it?

Share Improve this question edited Mar 17 at 10:49 realbart asked Mar 17 at 9:36 realbartrealbart 4,0042 gold badges33 silver badges41 bronze badges 4
  • 2 Can you also add the web_app_slot_count variable definition to the question? – Marko E Commented Mar 17 at 9:52
  • 1 ...because the most likely explanation is that its value is 0. – Matthew Schuchard Commented Mar 17 at 10:18
  • Marko, Matthew, you're right. I copied an error I got while trying various things to fix the actual error. – realbart Commented Mar 17 at 10:50
  • Are you still facing the issue! – Jahnavi Commented Mar 21 at 13:16
Add a comment  | 

1 Answer 1

Reset to default 1

Firstly, make sure that the resource has been already existed before importing it into the current configuration. And modify the app service name under azurerm_app_service_custom_hostname_binding resource provider because the app service name might be throwing a conflict here and it is not to be in the format of ${module.appservice_abc.name}/staging instead refer it directly as ${module.appservice_abc.name} or azurerm_app_service.example.name.

Refer app service custom binding template from Terraform registry.

In order to make the count work, try to assign count value equals to 1 instead of default = 0. There might be an issue when this has been to set to 0 if there is no web app staging slot existed.

Modified terraform code:

resource "azurerm_key_vault_certificate" "example" {
  name         = "sample-cert"
  key_vault_id = azurerm_key_vault.example.id

  certificate {
    contents = filebase64("xxxx.pfx")
  }
}
resource "azurerm_windows_web_app_slot" "staging" {
  count          = 1
  name           = "stag-slot"
  app_service_id = data.azurerm_windows_web_app.example.id
  site_config {}
}
resource "azurerm_app_service_custom_hostname_binding" "example" {
  count               = var.web_app_slot_count
  hostname            = "stag-staging.rdc.nl"
  app_service_name    = data.azurerm_app_service.example.name
  resource_group_name = data.azurerm_resource_group.app.name
  ssl_state           = "SniEnabled"
  thumbprint          = azurerm_key_vault_certificate.example[0].thumbprint

  depends_on = [azurerm_windows_web_app_slot.example]
}

Output:

Note: You can try the terraform code with modified changes in your pipeline by setting up the pipeline configuration and clone the repos in the below way and it should work as expected.

YML Pipeline set up script:

trigger: none

pool:
  vmImage: ubuntu-latest

variables:
  bkstrg: 'caronew'
  bkstore: 'newstrgc'
  bkcont: 'newc'
  bkkey: 'devops.terraform.tfstate'

stages:
  - stage: tfvalidate
    jobs:
      - job: validate
        continueOnError: false
        steps:
          - task: TerraformInstaller@1
            displayName: tfinstall
            inputs:
               terraformVersion: 'latest'
          - task: TerraformTaskV4@4
            displayName: 'init'
            inputs:
               provider: 'azurerm'
               command: 'init'
               backendServiceArm: 'Myfirstconn'
               backendAzureRmResourceGroupName: 'caronew'
               backendAzureRmStorageAccountName: 'newstrgc'
               backendAzureRmContainerName: 'newc'
               backendAzureRmKey: 'devops.terraform.tfstate'
          - task: TerraformTaskV4@4
            displayName: 'validate'
            inputs:
              provider: 'azurerm'
              command: 'validate'
  - stage: tfdeploy
    condition: succeeded('tfvalidate')
    dependsOn: tfvalidate
    jobs:
      - job: apply
        steps:
          - task: TerraformInstaller@1
            displayName: tfinstall
            inputs:
               terraformVersion: 'latest'
          - task: TerraformTaskV4@4
            displayName: 'init'
            inputs:
               provider: 'azurerm'
               command: 'init'
               backendServiceArm: 'Myfirstconn'
               backendAzureRmResourceGroupName: 'caronew'
               backendAzureRmStorageAccountName: 'newstrgc'
               backendAzureRmContainerName: 'newc'
               backendAzureRmKey: 'devops.terraform.tfstate'
          
          - task: TerraformTaskV4@4
            displayName: 'plan'
            inputs:
              provider: 'azurerm'
              command: 'plan'
              environmentServiceNameAzureRM: 'Myfirstconn'
          - task: TerraformTaskV4@4
            displayName: 'apply'
            inputs:
              provider: 'azurerm'
              command: 'apply'
              environmentServiceNameAzureRM: 'Myfirstconn'
发布评论

评论列表(0)

  1. 暂无评论