Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Latest commit

 

History

History
85 lines (61 loc) · 3.58 KB

azure_functions.md

File metadata and controls

85 lines (61 loc) · 3.58 KB

Using Tye and Azure Functions

Azure Functions is a popular serverless compute platform from Azure. Tye supports running Azure functions locally.

Getting Started: Create an Azure Function

Starting from the sample here, we are going to transform the backend from a web application to an azure function app.

To start, create an Azure Function project in a folder called backend-function. You can do this via:

Next, you must have the Azure Functions Core Tools through npm. By default, if you created an azure function through VSCode or Commandline, you will already have installed it. Otherwise, you can install the core tools by running:

npm install -g azure-functions-core-tools@3

You can also specify a path to func by specifying pathToFunc for the azure function service.

Next, create an HttpTrigger called MyHttpTrigger in your functions project. Change the contents of MyHttpTrigger to the following:

        [FunctionName("MyHttpTrigger")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var backendInfo = new BackendInfo()
            {
                IP = req.HttpContext.Connection.LocalIpAddress.ToString(),
                Hostname = System.Net.Dns.GetHostName(),
            };

            return new OkObjectResult(backendInfo);
        }

        class BackendInfo
        {
            public string IP { get; set; } = default!;

            public string Hostname { get; set; } = default!;
        }

Finally, change line in the frontend's Startup.cs to call the right endpoint (line 63), changing "/" to "api/MyHttpTrigger".

endpoints.MapGet("/", async context =>
{
    var bytes = await httpClient.GetByteArrayAsync("/api/MyHttpTrigger");
    var backendInfo = JsonSerializer.Deserialize<BackendInfo>(bytes, options);
    ...
}

Adding your Azure Function in tye.yaml

Now that we have a backend function added, you can simply modify your tye.yaml to point to the azure function instead:

# tye application configuration file
# read all about it at https://github.com/dotnet/tye
name: frontend-backend
services:
- name: backend
  azureFunction: backend-function/ # folder path to the azure function.
- name: frontend
  project: frontend/frontend.csproj

Running locally

You can now run the application locally by doing tye run.

On first run of an app that requires functions, tye will install any tools necessary to run functions apps in the future. This may take a while on first run, but will be saved afterwards.

Navigate to the tye dashboard to see both the frontend and backend running. Navigate to the frontend to see that the app still has the same behavior as before.

Deployment

Deployment of azure functions is currently not supported.