Search code examples
azureazure-functionsazure-storage

Fixing CORS and collision errors when Function is deployed


I am completely new to Azure cloud. When I deployed my Azure Function in to Function app using VS code, in the Azure portal, I am seeing the error says that:

System.InvalidOperationException : A collision for Host ID 'functionAppName' was detected in the configured storage account.

When I try to run the function using the option Code Test/Run, the Run button is disabled with the error message:

Running your function in portal requires the app to explicitly accept requests from https://portal.azure.com. This is known as cross-origin resource sharing (CORS).

I have no idea of what these errors all about, since working with Azure is completely new to me. How can I fix these issues?

Error 1:

AZFD004
Diagnostic event
Error code
AZFD004
Level
Error
Message
A collision for Host ID 'functionAppName' was detected in the configured storage account. For more information, see URL.
Details
System.InvalidOperationException : A collision for Host ID 'functionAppName' was detected in the configured storage account. For more information, see URL.
Hit count
23
Timestamp
October 6, 2023 at 12:30:18 AM GMT+5:30
Help link

Error 2:

Running your function in portal requires the app to explicitly accept requests from https://portal.azure.com. This is known as cross-origin resource sharing (CORS). Click here to add https://portal.azure.com to the allowed origin configuration.

__init__.py:

import azure.functions as func
import sys
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_dir))

 # import 'test_run' module
from test_run import func_1, func_2, func_3

def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        func_1()  
        func_2()
        func_3()

        name = req.params.get('name')
        if not name:
            return func.HttpResponse("Please pass a 'name' parameter in the query string.", status_code=400)

        return func.HttpResponse(f"Hello {name}. The function triggered successfully.")
    except Exception as e:
        return func.HttpResponse(f"An error occurred: {str(e)}", status_code=500)

test_run.py:

def func_1():
    '''some code here'''
def func_2():
    '''some code here'''
def func_3():
    '''some code here

Solution

  • I have used your code and test it locally as well as in portal post deployment, it worked as expected for me.

    Code:

    import  sys
    import  os
    import  azure.functions  as  func 
    from .test_run  import  func_1,func_2,func_3 
    
    current_dir  =  os.path.dirname(os.path.abspath(__file__))
    sys.path.append(os.path.join(current_dir))
    
    def  main(req: func.HttpRequest) -> func.HttpResponse:
    
    try:
    func_1()
    func_2()
    func_3()  
    
    name  =  req.params.get('name')
    if  not  name:
        return  func.HttpResponse("Please pass a 'name' parameter in the query string.", status_code=400)
    return  func.HttpResponse(f"Hello {name}. The function triggered successfully.")
    
    except  Exception  as  e:
    return  func.HttpResponse(f"An error occurred: {str(e)}", status_code=500)
    

    test_run.py-

    def  func_1():
    print("Testing func_1")
    
    def  func_2():
    print("Testing func_2")
    
    def  func_3():
    print("Testing func_3")
    

    Local Test Result-

    While testing, you need pass name=<anything> in order to get the result.

    enter image description here

    enter image description here

    In Portal-

    You need to pass the name parameter like below.

    enter image description here

    enter image description here