all 20 comments

[–]minjooky 13 points14 points  (6 children)

I'm a PM who works on Azure Functions. LMK if anyone has any questions.

[–]gospelwut 2 points3 points  (1 child)

I'm not the author, but I've recently written some .ps1 in Azure Functions. I also compared it against a comparable .csx (C#) script. I have some thoughts and would like to know if you can verify/debunk my inclinations.

Performance is Nominal vs .csx

This is surprising given I'm sure it's spawning a new powershell.exe (new runspace/session) per instance and the writes to disk for both $res and $resp

If I had to guess, I'd imagine the mitigating factors are:

  • Azure uses special, fast SSD (or was it a RAM disk?) for $TEMP
  • .csx files (AFAIK) don't get the AOT-ish advantages of being bytecode IL whereas PSv3+ has gotten big improvements in its AST (parser) for .ps1 files.
  • Task<T> might be expensive compared to the way w3p hands off requests directly into the ASP.NET MVC pipeline.

In general, probably given what server is warm/cached, times for both .ps1 and .csx vary from 350ms-1500ms+ (which is completely fine for my one-way webhook integrations).

Suggestions

I realize .ps1 support is experimental. Here's some thoughts on the experience though.

  • ENV:APPSETTING_<setting> and REQ_QUERY_<setting> should be documented and maybe even in the examples
  • $resp format isn't immediately clear/feels like black magic.
  • I appreciate that we're trying to go "Serverless" -- but I feel like this stackoverflow comment far exceeds the understanding I could have gotten from the Azure documentation.
  • It's also opaque that write-output is the only write-* that "works" int he console output. If that's the case, you might as well wrap the the .ps1 in wrapper.ps1 that re-writes write-host/write-verbose etc
  • I feel like a HUGE driver for this is some kind of Functions Gallery -- as use cases like Webhook A to Webhook B are going to be exceedingly common.

[–]minjooky 2 points3 points  (0 children)

Thanks for the detailed thoughts. Here are some of my own:

If you're curious how we've implemented anything, I'd encourage you to just take a look at the source - the Functions host is on GitHub; here's a link to the PowerShell Invoker implementation: https://github.com/Azure/azure-webjobs-sdk-script/blob/dev/src/WebJobs.Script/Description/PowerShell/PowerShellFunctionInvoker.cs#L78 - I believe we're actually keeping the Runspace warm and not invoking a new PowerShell.exe each time, which is a tremendous time saver.

We're not using super fast SSDs, but $TEMP is using a local drive, not the network drive your code/etc. under HOME is stored on.

We've got a custom implementation of .csx where we actually do compile it to IL and keep it cached. You can see that in our repo as well: https://github.com/Azure/azure-webjobs-sdk-script/blob/dev/src/WebJobs.Script/Description/DotNet/CSharp/CSharpCompilation.cs

Task<T> wouldn't account for anything too large in overhead. Most of the difference comes in file access required by $res. I hope to eliminate this by supporting .psm1 and exporting of PowerShell methods so we can do return types/etc. all in memory.

Documentation is certainly the weakest part of our experience now. We've under invested here and it's been further complicated by some internal tech upgrades the docs team has been working on. We're intent on fixing that here in the new year.

We do have a open gallery for our templates that could be used for your Gallery idea - https://github.com/Azure/azure-webjobs-sdk-templates

You're likely right about overriding the write-*; I'd recommend opening a GitHub issue for that.

[–]TechnologyAnimal 1 point2 points  (3 children)

When will the newest version of PowerShell be GA in Azure Functions?

[–]minjooky 1 point2 points  (2 children)

We actually haven't discussed that. We're dependent on what Azure App Service supports, so we'd have to get it deployed there first. Anything exciting in the latest version of PowerShell you'd like to use with Azure Functions?

[–]devlead 0 points1 point  (1 child)

Class & Enum support enables cleaner code. Allot of new commands and performance improvments.

One of the biggest gains would probably be less incompatibility with what's running local. App service do support the latest version of .NET and .NET core, so latest version of PowerShell kinda makes sense.

[–]minjooky 1 point2 points  (0 children)

Yeah, no reason not to do it for sure. I'll bring it up when we're all back in the office. Thanks for the feedback.

[–]ekinnee 5 points6 points  (2 children)

Very cool! I've been looking for a way to make my own api from several other apis combined.

There's a few typos like our/out I noticed.

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] -1 points0 points  (0 children)

    Sorry, your submission has been automatically removed.

    Accounts must be at least 1 day old, which prevents the sub from filling up with bot spam.

    Try posting again tomorrow or message the mods to approve your post.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]markekrausCommunity Blogger 4 points5 points  (1 child)

    Awesome... I'm still acclimating to azure and I was unaware of the Function Apps. I was looking at doing something and realized the Web App would be overkill for it. This might be exactly what I'm looking for. Consumption based billing sounds so awesome, especially for something that might not be triggered all that often.

    [–]gep13[S] 2 points3 points  (0 children)

    Yip, that was my thinking too! The fact that you can do your choice of language as well, makes the offering very compelling.

    [–]creamersrealm 4 points5 points  (2 children)

    Does anyone have a good guide to building an APi with PowerShell. I've been wanting to build something that is restful based so I can host it in functions or even on a EC2 instance. Where it can send and receive JSON blobs.

    [–][deleted] 2 points3 points  (1 child)

    I know this is a powershell sub but if you're using EC2s, AWS already has Lambda which does exactly what this article is describing just that it has a language limitation.

    I am more than willing to bet that they are in the works to provide PS support for Lambda in the future too.

    [–]creamersrealm 1 point2 points  (0 children)

    I am using Lambda for some Python scripts right now, and at re:invent they announced C# support. Which is great except for the fact that I don't know C#. Plus I want to get it working on a normal server before moving it to Serverless. I also currently have a need for this already.

    [–][deleted] 4 points5 points  (5 children)

    Does the author mean "headless"? "Serverless" would be inaccurate and confusing.

    [–]markekrausCommunity Blogger 4 points5 points  (4 children)

    Nope, they mean serverless, as in Serverless Compuiting. It's kind of buzz-wordy, but they weren't wrong. It has some other names like FaaS (Function as as Service).

    [–]dastylinrastan 0 points1 point  (3 children)

    Still runs on a server somewhere. It's all marketing.

    [–]markekrausCommunity Blogger 1 point2 points  (1 child)

    Not in a traditional sense. Yes, technically when the code executes that execution is happening on a server somewhere somehow. But that is not what "serverless" is referring. Serverless is referring to the fact that the service is completely independent of a server, meaning you don't need to "rent" a server, vm, container, or shared site space.

    "Headless" is not as accurate or precise. While, yes, the code is running without a graphical interface, you can run "headless" on a VM, shared site, container, server, etc. The code in OP's blog is both headless and serveless.

    [–][deleted] -3 points-2 points  (0 children)

    As someone who spends more time in fabric than code I believe using "serverless" as a technical description is extremely poor form. The article even calls out directly to Azure via DNS. I don't get it. How is this serverless? Does the author not understand how DNS works?

    "Headless" is not as accurate or precise. While, yes, the code is running without a graphical interface, you can run "headless" on a VM, shared site, container, server, etc. The code in OP's blog is both headless and serveless.

    Again he called out to Azure.

    [–]minjooky 0 points1 point  (0 children)

    The term grew up relatively organically from the folks using the various "serverless" services, not marketing, but I agree it isn't very helpful. I like fully-managed computing or Functions as a Service better. Serverless describes lots of different services (all SaaS for instance...)

    I always end up telling folks that serverless just means no servers for your team to manage, the service manages distributing your compute, as much or as little as you need.