Run Azure Functions Locally
Description:
Here are the steps I did to test Azure functions locally following the quickstart.
To Resolve:
-
Install the Azure Core Tools
-
Install the
Azure Functions
extension in VSCode -
In Azure Functions blade in vscode, create a new project called ‘my-http-test’ inside a new folder in my workspace called ‘local’.
-
Copy the module names from
requirements.txt
in my other function folder to local. -
Copy the
__init__.py
from my other function to local. Press F5 to run in debug. This generates an error:1
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command .venv\Scripts\python -m pip install -r requirements.txt" terminated with exit code: 1.
- The fix was to
cd
to the directory and then./.venv/Scripts/activate
and thenpip install -r path/to/requirements.txt
- The fix was to
-
I tried the previous steps a few times and kept running into errors. What eventually fixed it was:
- Using azure functions extension, create a new project under a new folder
- Then,
cd
to the root directory in vscode and runfunc host start
- Fix all errors until you get just a screen that says
http: [GET,POST] http://localhost:7071/api/http
replacehttp
with your function name.
-
From here, you should be able to use Postman to send requests to your example function, except… environmental vars! Uggh… In Azure, these are setup per function app and contain secrets used to connect to Azure Key Vault. Instead, we will use
.env
files:- In the local project folder, in your
requirements.txt
addpython-dotenv==0.15.0
- Then in
helpers.py
addfrom dotenv import load_dotenv
followed byload_dotenv()
somewhere - Now create your
.env
file with environmental vars - Lastly, make sure
.env
is in your.gitignore
or you risk exposing credentials! I also delete this file every day when I’m done testing just to be safe.
- In the local project folder, in your
Comments