If you’re looking for new ways to put your code in the cloud, look no further than serverless code deployments with Azure Functions! If you need some tutorials and basic information to get started, check out the following links first, especially the Quick Login page.
Special Thanks: I’d like to give credit to my awesome colleague Joe Raio, who walked me through these steps during a hackathon project.
Useful Links
- Quick Login: https://functions.azure.com/signin
- Azure Functions: https://azure.microsoft.com/en-us/services/functions/
- Dev Guide: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference
- C# Reference: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp
- JS Reference: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node
- Best Practices: https://docs.microsoft.com/en-us/azure/azure-functions/functions-best-practices
- Portal Login: https://portal.azure.com
The rest of this blog post will focus on how you can develop Azure Functions without being tied to a web browser. You’ll learn how to write the code on your own dev machine, which will enable you to easily commit/push your code into any source code repository.
Step 0: Install Node.js if you haven’t done so already
Download Node.js from the following URL. This includes npm.
Step 1: Install the Azure-CLI npm package
Get more information at the following URL:
In a brand new folder, run the following command from a command line:
> npm i -g azure-functions-cli
The install command is now:
npm i -g azure-functions-core-tools
Step 2: Create a new Azure Function locally
> func init
> func new
- Select a language, e.g. C#
- Select a template, e.g. HttpTriggerWithParameters
- Enter a name for your new Function, e.g. “MyFunction”, which will create a subfolder using the Function’s name.
Step 3: Observe the folder structure and its files.
- readme.md is a typical “Readme” file, in markdown format, useful for Github repos.
- run.csx is contains the code for your Azure Function
- function.json defines the function bindings and other configuration settings
In the Function App’s main folder, the appsettings.js file will be shared by all the functions in this Function App. You can update this settings file by pulling down the actual settings file from your a Function App in your Azure account.
Step 4: Log in to Azure from the command line
Type the following command at the command line:
> func azure login
Follow the onscreen prompts and instructions to log in to your Azure account. If you have two-factor authentication enabled, make sure you have your verification method ready for a quick login process.
Step 5: Get a list of Function Apps in your Azure account
Type the following command at the command line:
> func azure functionapp list
Step 6: Import your App Settings from the cloud
Type the following command at the command line:
> func azure functionapp fetch-app-settings <fn-app-name>
Make sure that you replace the placeholder <fn-app-name> with your own function app’s name. If you have the settings file open locally (e.g. in VS Code), you’ll notice that local file gets updated automatically!
Step 7: Run your function locally (optional)
If you want to run your function locally, type the following command at the command line:
> func host start
This should start the function locally and display the local URL with port number. Use this URL and port number to run it in a web browser and provide parameters via the QueryString. Note that QueryString parameters start with a “?” but subsequent parameters are separated by “&” ampersand characters.
Step 8: Publish back to Azure
Each time you want to publish your Function to Azure, type the following command at the command line:
> func azure functionapp publish <fn-app-name>
Once again, make sure that you replace the placeholder <fn-app-name> with your own function app’s name.
Step 9: Enable CORS for your Function App
If your Function App is hosted in Azure, you will need to enable CORS to ensure that it can be called from JavaScript code with a different origin. For step by step instructions, follow the guide at:
Pingback: Dew Drop - May 25, 2017 (#2487) - Morning Dew