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.58k stars 4.62k forks source link

azurerm_function_app_function deployment Error with State "Running" #19740

Open fuellbie opened 1 year ago

fuellbie commented 1 year ago

Is there an existing issue for this?

Community Note

Terraform Version

1.3.6

AzureRM Provider Version

3.36.0

Affected Resource(s)/Data Source(s)

azurerm_function_app_function

Terraform Configuration Files

data "azurerm_subscription" "current" {}

# Resource Group

resource "azurerm_resource_group" "resource_group" {
  name     = "resource-group-error-test"
  location = "Germany West Central"
}

# Storage account

resource "random_id" "storage" {
  byte_length = 8
}

resource "azurerm_storage_account" "storage_account" {
  name     = "test${random_id.storage.hex}"
  resource_group_name = azurerm_resource_group.resource_group.name
  location = "Germany West Central"
  account_tier = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_container" "storage_container" {
  name                 = "function-releases"
  storage_account_name = azurerm_storage_account.storage_account.name

}

resource "azurerm_role_assignment" "role_assignment" {
  scope                            = azurerm_storage_account.storage_account.id
  role_definition_name             = "Storage Blob Data Contributor"
  principal_id                     = azurerm_user_assigned_identity.function_identity.principal_id

}

# Function app

resource "azurerm_application_insights" "application_insights" {
  name                = "application-insights"
  location            = "Germany West Central"
  resource_group_name = azurerm_resource_group.resource_group.name
  application_type    = "other"
}

resource "azurerm_service_plan" "app_service_plan" {
  name                = "app-service-plan"
  resource_group_name = azurerm_resource_group.resource_group.name
  location            = "Germany West Central"
  os_type             = "Linux"
  sku_name            = "Y1"
}

resource "azurerm_linux_function_app" "get_email" {
  name                       = "getEmail${random_id.storage.hex}"
  resource_group_name        = azurerm_resource_group.resource_group.name
  location                   = "Germany West Central"

    identity {
        type  = "UserAssigned"
    identity_ids = [azurerm_user_assigned_identity.function_identity.id]
    }

  storage_account_name       = azurerm_storage_account.storage_account.name
  storage_uses_managed_identity = true
  service_plan_id            = azurerm_service_plan.app_service_plan.id

  site_config {
    application_stack {
      python_version = "3.9"
    }  
  }
  app_settings = {
    WEBSITE_RUN_FROM_PACKAGE = azurerm_storage_blob.storage_blob.url,
    WEBSITES_ENABLE_APP_SERVICE_STORAGE = false
    WEBSITES_MOUNT_ENABLED = 1
    FUNCTIONS_WORKER_RUNTIME = "python"
    AzureWebJobsStorage = "DefaultEndpointsProtocol=https;AccountName=${azurerm_storage_account.storage_account.name};AccountKey=${azurerm_storage_account.storage_account.primary_access_key}"
  }
}

resource "azurerm_function_app_function" "get_email" {
  timeouts {
    create = "5m"
    update = "5m"
  }
  name            = "GetResourceGroupOwnerEmail"
  function_app_id = azurerm_linux_function_app.get_email.id
  language        = "Python"
  test_data = jsonencode({
    "name" = "Azure"
  })

  config_json = jsonencode({
    "bindings" : [
      {
        "authLevel" : "function",
        "type" : "httpTrigger",
        "direction" : "in",
        "name" : "Request",
        "methods" : [
          "get",
          "post"
        ]
      },
      {
        "type" : "http",
        "direction" : "out",
        "name" : "$return"
      }
    ]
  })
}

resource "azurerm_user_assigned_identity" "function_identity" {
  location            = "Germany West Central"
  name                = "function-app-identity"
  resource_group_name = azurerm_resource_group.resource_group.name
}

# Upload function code

data "archive_file" "function" {
  type        = "zip"
  source_dir  = "${var.function_code}"
  output_path = "function.zip"

  depends_on = [null_resource.pip]
}

resource "null_resource" "pip" {
  triggers = {
      requirements_md5 = "${filemd5("${var.function_code}/requirements.txt")}"
  }
  provisioner "local-exec" {    
      command = "pip install --target='.python_packages/lib/site-packages' -r requirements.txt"
      working_dir = "${var.function_code}"
  }
}

resource "azurerm_storage_blob" "storage_blob" {
  name                   = "functions-${substr(data.archive_file.function.output_md5, 0, 6)}.zip"
  storage_account_name   = azurerm_storage_account.storage_account.name
  storage_container_name = azurerm_storage_container.storage_container.name
  type                   = "Block"
  content_md5            = data.archive_file.function.output_md5
  source                 = "function.zip"
}

Debug Output/Panic Output

╷
│ Error: creating Function App Function: (Function Name "GetResourceGroupOwnerEmail" / Site Name "getEmail83a4403a22ad44c5" / Resource Group "resource-group-error-test") - State: "Running" / InProgressOperationID: <nil>
│
│   with azurerm_function_app_function.get_email,
│   on main.tf line 83, in resource "azurerm_function_app_function" "get_email":
│   83: resource "azurerm_function_app_function" "get_email" {
│
│ creating Function App Function: (Function Name "GetResourceGroupOwnerEmail" / Site Name "getEmail83a4403a22ad44c5" / Resource Group "resource-group-error-test") - State: "Running" /
│ InProgressOperationID: <nil>
╵

Expected Behaviour

The Function App Function should be deployed successfully without an Error. If something is wrong with the terraform code or the Azure environment, I expect a helpful error message.

Actual Behaviour

Error message just says State: "Running", which is not a helpful information to debug the problem.

Steps to Reproduce

No response

Important Factoids

No response

References

No response

xiaxyi commented 1 year ago

Thanks @fuellbie for raising this issue, the error message was thrown by the api, not by terraform provider. In order to narrow down the issue, can you help to try using rest api/ azure portal to create the function to see if the function can be created successfully?

KernelPryanic commented 1 year ago

I'm getting the same error using Terraform and the function is getting created correctly using the func cli.

babaMar commented 1 year ago

Just stumbled upon this very same error with the function not being created. It seem that State: "Running" is not a vital part of the information, as stopping the Function App and retrying gives State: "Stopped". So that's just the current state of the Function App.

What seems suspicious to me is that InProgressOperationID: <nil>

IfthikharAhamed commented 1 year ago

I am also in similar state , i am getting the same error while publishing the code with more than few files/using the Zip deployment..

Shruti-git-007 commented 1 year ago

Hello All,

Is there any update on the error above. I am creating a function_app_function with event hub trigger but landing up on the same error. image

randomnote1 commented 1 year ago

Hey all, I hit this issue today, but was able to resolve it by setting the app setting WEBSITE_RUN_FROM_PACKAGE to 0.

I do believe a better error message is needed for this though.

YoavbhGit commented 1 year ago

Hello, Anyone got a solution to that problem? I do need to use this: WEBSITE_RUN_FROM_PACKAGE to use a zip file in my function which includes requirements and the .py itself.

IfthikharAhamed commented 1 year ago

I completely scrapped the azurerm_function_app_function resource block and made use of azurerm_function_app to deploy the code as well . There 2 options you can choose from

  1. Local build: install the dependencies along with the code into zip file ( make sure you use the same version to install the dependencies as version in function app)
  2. Remote build : add below parameter to app setting ENABLE_ORYX_BUILD=true SCM_DO_BUILD_DURING_DEPLOYMENT=true

to troubleshoot do a ssh to the node and see the /var/log for errors..

YoavbhGit commented 1 year ago

This happens to me when I try to upload python file using the azurerm_linux_function_app. It works fine without the file block. In comparison, I use the same setup but with azurerm_windows_function_app and a powershell script, and Terraform creates the resources and uploads the code just fine. So... maybe something up with the azurerm_linux_function_app?

What im trying to do basiclly is to create a function app inside it a function that i can develop in portal (Its just a small script) and i need the 'requests' package from python. Now Iv'e even tried creating a requirements.txt manually in the wwwroot folder and enabled PYTHON_ENABLE_WORKER_EXTENSIONS=1 and I still get the error No module named requests. There is microsoft documentation of how to do it with VScode but since i need it to be in terrafrom that doesn't help at all. Any ideas about that?

akingscote commented 11 months ago

FWIW this works for me:

...

resource "azurerm_service_plan" "example" {
  name                = "example"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  os_type             = "Linux"
  sku_name            = "Y1"
}

resource "azurerm_linux_function_app" "example" {
  lifecycle {
    ignore_changes = [
      name
    ]
  }

  name                = "xxxxxxxxxxxx"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location

  storage_account_name       = azurerm_storage_account.example.name
  storage_account_access_key = azurerm_storage_account.example.primary_access_key
  service_plan_id            = azurerm_service_plan.example.id

  site_config {

    application_stack {
      python_version = "3.9"
    }
  }

    identity {
    type = "SystemAssigned"
  }

  app_settings = {
    AzureWebJobsDisableHomepage    = true
    SCM_DO_BUILD_DURING_DEPLOYMENT = true
    ENABLE_ORYX_BUILD              = true
    FUNCTIONS_WORKER_RUNTIME       = "python"
  }
}

resource "azurerm_function_app_function" "example" {
  name            = "myfunc"
  function_app_id = azurerm_linux_function_app.example.id
  language        = "Python"

  file {
    name    = "mycode.py"
    content = file("mycode.py")
  }

  config_json = jsonencode({
    "bindings" = [
      {
        "authLevel" = "anonymous"
        "direction" = "in"
        "methods" = [
          "get",
          "post",
        ]
        "name" = "req"
        "type" = "httpTrigger"
      },
      {
        "direction" = "out"
        "name"      = "$return"
        "type"      = "http"
      },
    ]
  })
}
BeAllAround commented 7 months ago

Hi all,

I had the same issue and, simply, couldn't work out a way around it. So I had to come up with a way to do this from scratch using bash and terraform respectively. This solution is open source. I hope it helps.

https://github.com/BeAllAround/azure-function-app

Only requirements would be https://github.com/BeAllAround/azure-function-app?tab=readme-ov-file#requirements

Cheers!