Friday, September 8, 2023

Get quickly an Azure API (PowerShell-function)

For a lot of reasons, you need sometimes to have an API for testing. I had this need recently for testing MSAL with Anguler.
So, here is a first way to quickly get an Hello World API working in Azure. It is based on Azure function, PowerShell stack.

Prerequisites

  • Access as contributor to an Azure subscription
  • Visual Studio Code installed within your device
  • Git installed within your device

Overview

We are going to use local Azure Function Core Tools to emulate a function locally and deploy it to azure.
For doing that, we need to:

Install .Net 6

Go to the .Net 6 download page
Install .Net 6

Install Azure Function Core Tools

Click this link to download the installation package

Clone the Hello World PowerShell function code from Github

Open a command prompt, locate your DEV directory and execute :
git clone https://github.com/dfinke/powershell-azure-function-helloworld.git

Let VS code install the Azure extension

You can then open it in Visual Studio code.

Visual studio will ask you to install the extension "Azure Function". Agree, and the extension will be installed and also the other extensions "Azure Resources" and "Azure Account", that allow you to:
  • sign in to Azure from Visual Studio Code
  • create new resources into Azure from Visual Studio Code
  • deploy code to Azure, like this one of the Azure Function PowerShell Hello World


  • Create an empty function into Azure

    Then, sign in to Azure using the Azure Account extension.


    The Azure Resources extension, then, will show you the subscriptions of your tenant.
    You can click on the "+" to create a new resource

    However, we are rather going to use the portal to create the function, because an Azure function needs to have an unqiue name and the portal can validate if our name is unique.

    So, go to the portal, display the rezsources of one of your subscription and start creating a function by clicking the "+" sign.
    Then search for function app.

    Choose a name that doesn't exist, select powershell for the stack, then accept all the following tabs.
    When your function creation is complete, click "go to the resource"
    You will find the Url of your Function App. And if you click it, it will respond while there is not working API yet.

    execute the function locally


    Go back to your local environment, and, from the command prompt navigate to the code directory
    cd "powershell-azure-function-helloworld"
    
    launch the function
    func start
    
    If you paste into a browser `http://localhost:7071/api/powershell-azure-function-helloworldHttpTrigger` you will see the function API working locally:

    Deploy the function code into Azure

    Now that we have an empty function within Azure, we can use VS code to deploy our local code into it.
    Go back to VS code and right click the subfolder of the code of the PowerShell function : powershell-azure-function-helloworldHttpTrigger
    Then, at the bottom of the context menu: "deploy to function App"
    You can then, choose the subscription where the Azure function you want to deploy to, is:
    Then, we locate the helloworldfunction1123 we have previously created in Azure:
    We accept to deploy:
    And, finally, we can see the deployment in action:
    And follow it in the Output pane of VS code untill it is complete:
    BTW: I had to deploy twice to get it complete. It failled the first time
    Now, if we go back to Azure portal, we can see our PowerShell function avaolable:
    If we click on the Name Link of our function, we are redirected to a view where we can get the function Url to call it:
    and if we paste the link in a browser, our function is responding and sends us the content:
    We have now an API in Azure based on PowerShell function that we can use for testing purpose, like authentication, MSAL, etc.
    Well done!

    No comments: