DEV Community

Guido Zambarda
Guido Zambarda

Posted on • Originally published at iamguidozam.blog on

Run Azure Function locally on HTTPS

Have you ever needed to test an Azure Function locally but using HTTPS instead of the default HTTP? This post is for you! 🥳

To achieve this you will need two things:

  • a certificate
  • update the Azure Function settings to use HTTPS

Getting the certificate

Starting with the certificate, in case you don’t have one, you can create a self signed certificate using PowerShell with administrative privileges using the following code:

$cert = New-SelfSignedCertificate -Subject localhost -DnsName localhost -FriendlyName "Functions Development"
Enter fullscreen mode Exit fullscreen mode

Where the -FriendlyName can be whatever you want.

Next you need to export the certificate file, you will have to specify where to save it and the password to associate with the certificate:

Export-PfxCertificate -Cert $cert -FilePath {pfx-file-path} -Password (ConvertTo-SecureString -String {password} -Force -AsPlainText)
Enter fullscreen mode Exit fullscreen mode

The code above must be updated replacing all the placeholders and the values are:

  • {pfx-file-path} represent the path where you want to save the PFX file.
  • {password} represent the password you want to assign to the certificate.

Update the Azure Function execution settings

To start the Azure Function and load your newly created certificate you have to tell the Azure Function to use some specific argument. To do so, open the launchSettings.json file under the properties folder and in the profile you’ll need to add the —useHttps, —cors, —cert and —password parameters. For reference here is an example of how the file will look like:

{
  "profiles": {
    "MySampleProject": {
      "commandName": "Project",
      "commandLineArgs": "--port {your port} --useHttps --cors * --cert {the path to your pfx file} --password {the pfx password}"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

As you can see the commandLineArgs is where the magic happens!

Simply replace the placeholders with your informations:

  • {your port} will be the port for your project, you can use the default one already specified in the file.
  • {the path to your pfx file} will be the path to your previously created PFX file.
    • NB : the path to the PFX file must be escaped, if the path to your file is C:\temp\cert.pfx it will become c:\temp\cert.pfx.
  • {the pfx password} will be the password you specified when you exported the certificate.

Now you can run your Azure Function locally!

NB: Keep in mind that, if you created a self signed certificate, the browser will not accept it as valid because is self signed, but once you acknowledge that you can finally test your Azure Function locally!

Hope this helps!

Top comments (0)