What is the best way to connect to database from azure webapp?

Anish Chapagai 46 Reputation points
2022-05-28T10:03:22.377+00:00

I have a django project in hosted in Azure WebApp Service. I also have a Sql Server and database within Azure. I can connect it on <databasename>.database.windows.net from my local machine. I now want my webapp to connect to that same database but I don't want to query from public url since that I think will route traffic from over the internet. I tried creating a virtual network, bounded the database's private link on that virtual network resource and replaced my host with that private link( its like XX.X.X.X) in application's environment variable. Now this will not work from my local machine obviously, but it should from within Azure network's private link, shouldn't it? But I am getting error. Here's the traceback.

Environment:


Request Method: GET
Request URL: http://<appname>.azurewebsites.net/<a_path>/

Django Version: 4.0.4
Python Version: 3.9.7
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 '<app1>',
 'whitenoise.runserver_nostatic',
 '<app2>',
 'django_crontab']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/tmp/8da4082417a153e/<a_path>/views.py", line 16, in index
    'Recents':[{'Title':i.name,
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/query.py", line 320, in __iter__
    self._fetch_all()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/query.py", line 1507, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/query.py", line 57, in __iter__
    results = compiler.execute_sql(
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1348, in execute_sql
    sql, params = self.as_sql()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/compiler.py", line 188, in as_sql
    supports_offset_clause = self.connection.sql_server_version >= 2012
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/functional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/base.py", line 445, in sql_server_version
    with self.temporary_connection() as cursor:
  File "/opt/python/3.9.7/lib/python3.9/contextlib.py", line 119, in __enter__
    return next(self.gen)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 639, in temporary_connection
    with self.cursor() as cursor:
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 284, in cursor
    return self._cursor()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/base.py", line 246, in _cursor
    conn = super()._cursor()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 260, in _cursor
    self.ensure_connection()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 244, in ensure_connection
    self.connect()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/base.py", line 324, in get_new_connection
    connstr = encode_connection_string(cstr_parts)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/base.py", line 53, in encode_connection_string
    return ';'.join(
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/base.py", line 54, in <genexpr>
    '%s=%s' % (k, encode_value(v))
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/base.py", line 78, in encode_value
    if ';' in v or v.strip(' ').startswith('{'):

Exception Type: TypeError at /<a_path>/
Exception Value: argument of type 'NoneType' is not iterable

I have migrated proper tables and it works from my local machine, so I'm assuming the problem is from my deployment side. Am I not connecting to right link or not configuring something right?

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,158 questions
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
714 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,917 questions
{count} votes

Accepted answer
  1. Udaiappa Ramachandran 726 Reputation points MVP
    2022-05-29T03:45:42.497+00:00

    Hello @Anish Chapagai
    As @brtrach-MSFT said, I would suspect you are missing some configuration, enabling the web app and SQL is very easy in azure, I have summarized the steps.

    If both Web App and Database are in the same region (same VNET), maybe multiple subnets, here are the steps,

    1. Enable vNET with one or more subnets (maybe 1 for web and another one for DB)
    2. For Web APpp, From the network tab enable VNET
      206433-image.png
    3. For SQL, From the network tab,
      3(a). from public access, either disable or "selected network" --in case you want to use withing your browser without VM in the same network
      3(b). from private access, enable the private link
      206378-image.png

    please note if they are located in the different regions then you need to peer the networks as well as create a virtual link to your database


0 additional answers

Sort by: Most helpful