question

AnishChapagai-2147 avatar image
0 Votes"
AnishChapagai-2147 asked AnishChapagai-2147 commented

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

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-webappsazure-virtual-networkazure-database-mysql
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AnishChapagai-2147 Thank you for your question regarding connecting your web app to Azure Web App to your Azure SQL DB privately.

Based on your current verbatim it's tough to understand if you've completed 100% of the necessary configuration.

We have an architecture document that covers this scenario. Can you please review it and then let us know if there are any items that you missed while configuring or if you are still receiving errors after verifying proper configuration?

We look forward to your reply.

1 Vote 1 ·

1 Answer

nhcloud avatar image
0 Votes"
nhcloud answered AnishChapagai-2147 commented

Hello @AnishChapagai-2147
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


image.png (88.6 KiB)
image.png (58.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much. Turns out I didn't have much knowledge on Virtual Networks and Subnets as a whole. After reading some docs and YouTube videos I found out I had to create subnets for App and Database Server under "same" resource + "same" region + same Virtual Network and then substitute the database's private link.

0 Votes 0 ·