Page 10 - MSDN Magazine, June 2018
P. 10

Data Points JULIE LERMAN Replacing a Bulky API with Azure Functions
Back in 2015 and 2016, I wrote a few columns about a Node.js Web API that interacted with Azure DocumentDB. There was a lot involved. Even though I took advantage of the Node.js SDK designed for DocumentDB, I still had to write a lot of logic. There was logic for building up objects that represented the DocumentDB account, the database and the collections. There was code to create queries and to execute them—layers of asynchronous calls. But I was able to write a nice little API that allowed my application to create, query and update data in the database.
In more recent columns, I’ve worked with Azure Functions, Microsoft’s serverless APIs that live in Azure and integrate seam- lessly with other Azure technologies. One of those integrations is with Azure Cosmos DB, the data storage into which Azure DocumentDB evolved.
Having written a number of Azure Functions, integrating them with Cosmos DB to read and write data, I looked back at my older Node.js APIs and realized I could eliminate about 98 percent of the code by converting them into Azure Functions. That’s because most of the code I had written was to interact with the database, but now Azure Functions take care of all of that work. With nothing more than a configuration to identify the connection string—whether the data is coming from the database or going into it—and the relevant query, the built-in features of the function will take care of the rest.
Using Visual Studio Code and Extensions
In the earlier Azure Functions columns, I worked directly in the Azure portal. But it’s also possible to use the Azure Functions Core Tools to develop on your computer and then deploy to Azure. The first version of the tools (version 1) runs only on Windows, with .NET 4.7.1 or higher, and there’s a Visual Studio 2017 extension for working with those tools.
In contrast, version 2 of the Azure Functions Core Tools runs on .NET Core and is cross-platform, and a Visual Studio Code extension lets you work with it handily (bit.ly/2H7VmxH). That’s the version I’ll be working with in this article. Note that both the tools and the extension are in preview.
Working with this extension has a few setup requirements, which you’ll find in the Read Me for the Visual Studio Code extension. And
it assumes you’ve already installed Visual Studio Code and its prereq- uisites on your system, which could be Windows, macOS or Linux. The extension helps you easily create new Azure Function App folders to contain Azure Functions, create the functions (with a variety of templates to choose from), run and debug them locally, and deploy them to Azure. I’ve been really impressed because it takes away the hard work and all you have to do is focus on your
code—which is the promise of serverless computing.
In addition to the Azure Functions extension, I also installed the Azure Cosmos DB extension for Visual Studio Code (bit.ly/2HkPfDE). I’m using that to look at the collections and documents of my existing databases, but you can also use the extension to create new Cosmos DB accounts, databases and collections, as well as retrieve and update documents and create new documents. I’ve put in a suggestion to be able to import documents given pre-existing
JSON files and hope to see that in there someday.
I’ve been really impressed because it takes away all the hard work and all you have to do is focus on your code—which is the promise of serverless computing.
Critical to using both of these extensions is the ability to sign into your Azure account from Visual Studio Code, so I’ve also installed the Azure Account extension (bit.ly/2k1phdp).
Each of these three extensions have handy walk-throughs on the pages I’ve linked to, which will help you get started using them. I’ll do a light walk-through here for the purpose of showing you how much easier it is to create the Azure Function counterparts of my Node.js Web API than it was to build all of the code and depen- dencies I relied on for the Node.js solution. I will, however, still use JavaScript as the language for my functions. JavaScript and Node.js support is built into Visual Studio Code.
Creating the Azure Functions Project
After installing the three extensions and their dependencies, it was time to recreate my API. I started by creating a new folder on my
This article relies on technologies that are in preview. All information is subject to change.
Code download available at msdn.com/magazine/0618magcode.
6 msdn magazine


































































































   8   9   10   11   12