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 Functionsextension 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.txtin my other function folder to local. -
Copy the
__init__.pyfrom 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
cdto the directory and then./.venv/Scripts/activateand 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,
cdto 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/httpreplacehttpwith 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
.envfiles:- In the local project folder, in your
requirements.txtaddpython-dotenv==0.15.0 - Then in
helpers.pyaddfrom dotenv import load_dotenvfollowed byload_dotenv()somewhere - Now create your
.envfile with environmental vars - Lastly, make sure
.envis in your.gitignoreor 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