Now we are ahead for the next challenge, where we would like get familiar with Azure Functions and how to use them to build serverless applications. We will start by creating a new Azure Functions project, test and debug the a simple application make sure we have full understanding how to develop and enhance them, and then we will use Bicep to deploy the function app to Azure. Once we have it up and running, we will create a project solution file to organize our different projects.
Azure Functions Core Tools (func): is a command-line tool that lets you develop, manage, and deploy Azure Functions projects from your local computer. You can use Core Tools to create, add, and bind functions to your project, and start the Functions host to run or debug them. You can also use Core Tools to deploy your code to Azure and work with application settings.
- Developing Azure Functions applications.
- Organizing projects using a Project Solution (.sln).
- Create your first containerized Azure Functions project.
- Deploy Azure Functions application to Azure using Bicep.
- (Optional) Organize projects using a Project Solution
-
Initialize new dotnet Azure Functions project using Azure Functions Core Tools CLI (func).
func init ./src/backend --worker-runtime dotnet --docker
Note: The --docker flag is a command-line argument that generates a Dockerfile for a container. The Dockerfile uses a base image that matches the selected worker runtime. This is useful if you want to run your functionapp inside a container.
-
Rename backend.csproj to Todo.Backend.csproj for naming consistency.
mv ./src/backend/backend.csproj ./src/backend/Todo.Backend.csproj
-
Review your new Todo.Backend project:
-
Change to the backend project root directory
pushd src/backend
-
Create an HttpTrigger function:
-
Review different C# available functions templates:
func templates list --language c#
-
Create a new HttpTrigger function
func new --name "Healthz" --template "HttpTrigger" --authlevel "anonymous"
This command will create a new Http Trigger function named
Healthz
withanonymous
authentication level, which means that the function will be accessible without any authentication, and a default implementation that returns Http Status Code 200.
-
-
Launch the functions runtime host with your function app locally:
func start
Once the build succedded the function app will start locally and you will be able to click on Open in browser button in the toast message or open manually
http://localhost:7071
and see function home page is up and test your new HttpTrigger via the corresponded api endpointhttp://localhost:7071/api/Healthz
-
Stop the functions runtime host by pressing
Ctrl
+C
in the terminal. -
Change back to root directory
popd
-
Run and debug your function locally:
- Add .vscode tasks and launch configurations for functions.
- Add the following tasks to .vscode/tasks.json under
tasks
array:
{ "label": "Start Backend", "type": "dotenv", "targetTasks": "Backend Functions Run", "file": "${input:dotEnvFilePath}" }, { "label": "Clean Backend", "command": "dotnet", "args": [ "clean", "${workspaceFolder}/src/backend/Todo.Backend.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "options": { "cwd": "${workspaceFolder}/src/backend/" }, "type": "process", "problemMatcher": "$msCompile" }, { "label": "Build Backend", "command": "dotnet", "args": [ "build", "${workspaceFolder}/src/backend/Todo.Backend.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "type": "process", "dependsOn": "Clean Backend", "group": { "kind": "build", "isDefault": true }, "problemMatcher": "$msCompile" }, { "label": "Backend Functions Run", "detail": "Helper task--use 'Start Backend' task to ensure environment is set up correctly", "type": "func", "dependsOn": "Build Backend", "command": "host start", "options": { "cwd": "${workspaceFolder}/src/backend/" }, "presentation": { "panel": "dedicated", }, "problemMatcher": [ "$func-dotnet-watch" ] }
- Add the following tasks to .vscode/tasks.json under
- In order to attach to a running azure functions process, add the following launch configuration, to .vscode/launch.json under configurations array:
{ "name": "Attach to .NET Functions", "type": "coreclr", "request": "attach", "processId": "${command:azureFunctions.pickProcess}" }
- Start the function locally using command pallete
Ctrl
+Shift
+P
and type Tasks: Run Task and select Start Backend task. - Place a breakpoint in your Healthz function.
- Open Run and Debug panel using
Ctrl
+Shift
+D
, select Attach to .NET Functions from the dropdown and then pressing F5 to start debugging. - Trigger your Healthz function and validate the breakpoint is hit.
- Stop debugging by pressing
Shift
+F5
and stop the function app by pressingCtrl
+C
in the terminal.
- Add .vscode tasks and launch configurations for functions.
We could use different compute services like a Azure Container Instances (ACI) or Azure Kubernetes Service to host our function app, but for this workshop we will use an App Service Plan service.
-
Create backend.bicep using infra/core/host/functions.bicep module.
- Understand functions.bicep - app service plan
- backend storage best practices and connection string for multicloud
-
Add new backend service to main.bicep.
-
Add new backend service to azure.yaml for azd.
-
Deploy changes to your environment using
azd up
. -
Locate your service backend endpoint and test your function app
azd show | grep "backend" | awk '{print $2 "api/healthz"}'
A Project Solution (.sln) file is a solution file that is used to group one or more projects together in Visual Studio and now supported also in VSCode. It is a text-based file that stores metadata about your solution, including the projects that are associated with the solution, the items that are not associated with a particular project, and the build configurations that determine which project configurations to apply in each type of build.
There are several reasons why you might want to use a .sln file for your projects. Here are some of them:
- Different projects can be developed by different teams, and the solution file can help manage the dependencies between them.
- The solution file can be used to manage the configuration and deployment settings for all of the projects in the solution.
- Different projects can have different dependencies, either dependencies to other projects in your solution or dependencies to third-party DLLs, pre/post-build steps and more.
Organize the solution using a Project Solution (.sln):
-
Open new terminal: Menu -> Terminal -> New Terminal (or
Ctrl
+Shift
+~
). -
Create new .sln in the root directory:
dotnet new sln -n Todo
-
Add existing .NET projects to sln:
dotnet sln Todo.sln add src/api/Todo.Api.csproj
-
Close the opened solution in VSCode Solution Explorer, using one of the following options:
-
Open the new Todo solution in VSCode Solution Explorer:
- Open the Command Palette with
Ctrl
+Shift
+P
. - Type Open Solution and select .NET: Open Solution from the list.
- Pick Todo.sln.
- Open the Command Palette with
-
Add backend project to the sln:
dotnet sln Todo.sln add src/backend/Todo.Backend.csproj
-
Review the new solution in VSCode Solution Explorer:
Name | Description |
---|---|
Understanding serverless cold start | APIM - Consumption tier cold start |