skip to Main Content

Azure appservice creation with terraform is giving error below:

A resource with the ID "/providers/Microsoft.Web/sourceControls/GitHub" already exists – to be managed via Terraform this resource needs to be imported into the State.

I ran the below command-

terraform import azurerm_source_control_token.dobble_token /providers/Microsoft.Web/sourcecontrols/GitHub

But it gives the error below:-

ID must be exactly "/providers/Microsoft.Web/sourceControls/GitHub".

I gave id in the format below:-

azurerm_source_control_token.dobble_token as "/providers/Microsoft.Web/sourceControls/GitHub". I Still face the same error.

Could someone help me resolve this?

2

Answers


  1. I tried the below terraform code to create app Service with source control from this document.

    My main.tf code referred from the document above but modified to print source_control resource id:-

    # Configure the Azure provider
    terraform {
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~> 3.0.0"
        }
      }
      required_version = ">= 0.14.9"
    }
    provider "azurerm" {
      features {}
    }
    
    # Generate a random integer to create a globally unique name
    resource "random_integer" "ri" {
      min = 10000
      max = 99999
    }
    
    # Create the resource group
    resource "azurerm_resource_group" "rg" {
      name     = "myResourceGroup-${random_integer.ri.result}"
      location = "eastus"
    }
    
    # Create the Linux App Service Plan
    resource "azurerm_service_plan" "appserviceplan" {
      name                = "webapp-asp-${random_integer.ri.result}"
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      os_type             = "Linux"
      sku_name            = "B1"
    }
    
    
    resource "azurerm_linux_web_app" "webapp" {
      name                  = "webapp-${random_integer.ri.result}"
      location              = azurerm_resource_group.rg.location
      resource_group_name   = azurerm_resource_group.rg.name
      service_plan_id       = azurerm_service_plan.appserviceplan.id
      https_only            = true
      site_config { 
        minimum_tls_version = "1.2"
      }
    }
    
    #  Deploy code from a public GitHub repo
    resource "azurerm_app_service_source_control" "sourcecontrol" {
      app_id             = azurerm_linux_web_app.webapp.id
      repo_url           = "https://github.com/Azure-Samples/nodejs-docs-hello-world"
      branch             = "master"
      use_manual_integration = true
      use_mercurial      = false
    }
    
    # Output the resource ID of azurerm_app_service_source_control
    output "source_control_resource_id" {
      value = azurerm_app_service_source_control.sourcecontrol.id
    }
    

    Print the resource id of source control by adding the code snippet below in main.tf code:-

    # Output the resource ID of azurerm_app_service_source_control
    output "source_control_resource_id" {
      value = azurerm_app_service_source_control.sourcecontrol.id
    }
    

    enter image description here

    And then import the source control latest state with the command below by using the resource id printed in the terminal with the code above.

    terraform import azurerm_app_service_source_control.sourcecontrol /subscriptions/xxxxx44fb-b5ba-2b83a074c23f/resourceGroups/myResourceGroup-56454/providers/Microsoft.Web/sites/webapp-56454
    

    enter image description here

    As my resource is already managed by terraform it gave me the warning above.

    Login or Signup to reply.
  2. @SiddheshDesai Could you help me in resolving this import statement.
    enter image description here

    It is giving the error

    2023-08-31T09:57:03.4764371Z ##[error]Error: The process ‘C:hostedtoolcachewindowsterraform1.5.6x64terraform.exe’ failed with exit code 127
    2023-08-31T09:57:03.4832459Z ##[section]Finishing: Terraform import

    2023-08-31T09:57:03.4121721Z [command]C:hostedtoolcachewindowsterraform1.5.6x64terraform.exe "terraform import azurerm_source_control_token.dobble_token1 /providers/Microsoft.Web/sourceControls/GitHub"
    2023-08-31T09:57:03.4641190Z Usage: terraform [global options] [args]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search