Enable SSL for your Azure Functions
You might remember me writing a post on how you can set up your site with SSL while using Let’s Encrypt and Azure App Services.
Well, as it goes, the same post applies for Azure Functions. You just have to do some extra work for it, but it’s not very hard.
Simon Pedersen, the author of the Azure Let’s Encrypt site extension, has done some work in explaining the steps on his GitHub wiki page. This page is based on some old screenshots, but it still applies.
The first thing you need to do is create a new function which will be able to do the ACME challenge. This function will look something like this.
public static class LetsEncrypt
{
[FunctionName("letsencrypt")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "letsencrypt/{code}")]
HttpRequestMessage req,
string code,
TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. {code}");
var content = File.ReadAllText(@"D:\home\site\wwwroot\.well-known\acme-challenge\" + code);
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(content, System.Text.Encoding.UTF8, "text/plain");
return resp;
}
}
As you can see, this function will read the ACME challenge file from the disk of the App Service it is running on and return the content of it. Because Azure Functions run in an App Service (even the functions in a Consumption plan), this is very possible. The Principal (created in the earlier post) can create these type of files, so everything will work just perfectly.
This isn’t all we have to do, because the url of this function is not the url which the ACME challenge will use to retrieve the appropriate response. In order for you to actually use this site extension you need to add a new proxy to your Function App. Proxies are still in preview, but very usable! The proxy you have to create will have to redirect the url /.well-known/acme-challenge/[someCode]
to your Azure Function. The end result will look something like the following proxy.
"acmechallenge": {
"matchCondition": {
"methods": [ "GET", "POST" ],
"route": "/.well-known/acme-challenge/{rest}"
},
"backendUri": "https://%WEBSITE_HOSTNAME%/api/letsencrypt/{rest}"
}
Publish your new function and proxy to the Function App and you are good to go!
If you haven’t done this before, be sure to follow all of the steps mentioned in the earlier post! Providing the appropriate application settings should be easy now and if you just follow each step of the wizard you’ll see a green bar when the certificate is successfully requested and installed!
This makes my minifier service even more awesome, because now I can finally use HTTPS, without getting messages the certificate isn’t valid.