Azure Function unable to connect to Azure SQL Database

ChrisU 231 Reputation points
2022-06-23T09:06:04.32+00:00

Hi All,

I am having an issue connecting to an Azure database from an Azure Function. Please see screen grab below of the function (I will be updating it to use AD soon!).

214301-image.png

The weird thing is, is that it has worked in the past. This is to save data from an IoT hub into the database. It worked when I was using LORIOT as the LoRaWAN provider, however I have switched to The Things Network (TTN) and so have had to edit the Azure function as TTN provides payload formatter, where as LORIOT did not. So I could remove the decoder from the Azure function. As you can see from the screen grab below, the data from the IoT-Hub is being received and logged. I honestly did not expect saving to the DB to be an issue, but as you can see I am getting the error shown below which shows that it is failing when trying to connect:

214302-image.png

I have checked the credentials (as mentioned - they are unchanged from when it was working) and I have checked that the DB has status = Online and have checked that my client IPv4 address has been added to the DB firewall.

Does anyway have any idea as to the issue?

Azure SQL Database
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Alberto Morillo 34,671 Reputation points MVP Volunteer Moderator
    2022-06-23T10:49:52.733+00:00

    Could you please add the following to the connection string?

    ;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;'  
    

    Make sure you allow Azure Services to access your Azure SQL Database. Make sure you have below setting enabled on Azure SQL firewall.

    214406-image.png

    0 comments No comments

  2. ChrisU 231 Reputation points
    2022-06-23T13:02:23.81+00:00

    Thank you for your response Alberto. I had already added the client IP address to the firewall rules.

    I have added to the connection string that which you suggested, but done it like this:
    214300-image.png
    214310-image.png

    However I believe I am getting the same error:
    ![214401-image.png]3


  3. ChrisU 231 Reputation points
    2022-06-24T07:35:50.277+00:00

    Hi @Alberto Morillo I have had this option enabled since the start of setting this up. As mentioned, this DB has worked with the existing Azure Function and the save function within the init.py file was unchanged.

    Going to be looking into this more today.

    0 comments No comments

  4. ChrisU 231 Reputation points
    2022-06-24T10:32:09.43+00:00

    So the infrastructure team got involved and added my client IP address to the firewall and now it seems to work. The error that I was seeing when deployed to Azure was an error on the code that was quickly sorted. This is weird as the Infrastructure team swears blind that my client IP was not added previously so we do not know why it worked in the passed. Either way, it is all working as expected now so all good. @Alberto Morillo ; thanks for your time on this!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.