Learn how to Deploy NestJS serverless apps to Azure Functions
Tags: webdev, node, nestjs, javascript
Published: 2019-08-16 18:55:38

> **Originally published on the [Trilon Blog](https://trilon.io/blog/deploy-nestjs-azure-functions) by [Kamil Mysliwiec](https://twitter.com/kammysliwiec)**

## Deploy NestJS to Production Series:

- [Part 1: Deploy NestJS to Zeit Now.sh](https://trilon.io/blog/deploying-nestjs-to-zeit-now)
- [**Part 2: Deploy NestJS to Azure Functions**](https://trilon.io/blog/deploy-nestjs-azure-functions) _(this article)_
- ... more to come ...

---

# Serverless NestJS with Microsoft Azure

In this article we'll be looking at how to deploy _serverless_ **[NestJS](https://nestjs.com)** applications to the cloud platform **[Azure Functions](https://code.visualstudio.com/tutorials/functions-extension/getting-started)** in only a few minutes!

If you're not familiar with **[NestJS](https://nestjs.com)**, it is a TypeScript Node.js framework that helps you build enterprise-grade efficient and scalable Node.js applications.

---

> **Incase you missed the announcement, the integration between NestJS & Azure was _just_ teased at the other week!**

{% twitter 1156143186508730368 %}

<br>
Before we dive further, let's take a step back to make sure we have a good understanding about Azure Functions and how they can play a role in handling our serverless NestJS applications.

## What are Azure Functions ?

Azure Functions are a serverless computing execution service that enables you to run code _on-demand_ without having to manage the allocation of machine resources, servers, etc. Write your NestJS code, deploy it - and let Azure Functions handle the rest!

Azure functions can let you create functions that execute **based on events**, read more in the official Azure Functions documentation [here](https://docs.microsoft.com/azure/azure-functions/).

---

Azure let's you deploy a myriad of different frameworks/languages, from C# to JavaScript/TypeScript.

NestJS is a TypeScript-based Node.js framework after all - so let's take a look at how we utilize the fantastic Azure platform to deploy our serverless applications!

## Getting NestJS setup

> **NOTE:** In this demonstration, we'll be showcasing a new NestJS application generated by the CLI, but if you prefer to use an existing NestJS application - feel free - and to skip ahead to the next section about Schematics.

```bash
☁ npm i -g @nestjs/cli
☁ nest new PROJECT_NAME
```

Now let's `cd` into the newly created directory and open up our IDE. At this point we have a simple generated NestJS Application.

---

# 📯 Introducing NestJS Azure Functions Schematics

At **[Trilon](https://trilon.io)**, we've been working with some amazing individuals over on the Microsoft Azure team to create a seamless integration between NestJS + Azure Functions. Allowing you to effortlessly deploy your new/existing services to the cloud!

These NestJS schematics allow you to instantly setup your application with all the scaffolding required for the integration, with one simple command. More on this later!

# Installing NestJS Azure Functions Schematics

In your terminal, make sure you're at the root directory of your project and type:


```ts
nest add @nestjs/azure-func-http
```

Example output:

```bash
✔ Installation in progress... ☕
CREATE /.funcignore (66 bytes)
CREATE /host.json (23 bytes)
CREATE /local.settings.json (116 bytes)
CREATE /proxies.json (72 bytes)
CREATE /main/function.json (294 bytes)
CREATE /main/index.ts (287 bytes)
CREATE /main/sample.dat (23 bytes)
CREATE /src/main.azure.ts (321 bytes)
UPDATE /package.json (1827 bytes)
```

**Excellent!** You now have your NestJS application completely setup for Azure Functions! What's next?

---

## Local Azure Functions development

Next, we're going to utilize the Azure-CLI to test our Azure Functions locally!

If you don't have it installed on your machine yet, make sure to:

1. Install [Azure Functions Core Tools](https://docs.microsoft.com/azure/azure-functions/functions-run-local#v2)
2. Install the [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest)

Once you have everything installed, make sure it was setup right by entering `func --version` in your terminal.

---

With the Azure-CLI all setup, let's fire up our *Local* Azure Functions environment and see everything in action!

> **IMPORTANT:** Build NestJS first. Since it's written in TypeScript we need to make sure our application compiles down to JavaScript before letting Azure do the rest!

```bash
npm run build && func host start
```

This should output something similar to:

```bash
Hosting environment: Production
Content root path: /Users/yourname/Documents/Trilon/nestjs-azure-functions
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.

Http Functions:

	main:  http://localhost:7071/api/{*segments}
```

---

#### Open up that url `http://localhost:7071/api/` in your browser you should see:

![NestJS Azure Functions local development](https://thepracticaldev.s3.amazonaws.com/i/m1equa78t9lgr7w4oi3m.png)<figcaption>NestJS & Azure functions local development</figcaption>

### It's that easy!

If you're wondering why the URL is prepended with `/api/`, take a look at your `main.azure.ts` file in the NestJS project. You could update it to whatever you wish, add API versioning (`/api/v1/`), etc!

![NestJS globalPrefix](https://thepracticaldev.s3.amazonaws.com/i/f3g0w6o8eaknaz5xvllo.png)

Eventually a real-world application would have all of the URLs all available under this globalPrefix.

```bash
# ie:
GET /api/products/
GET /api/products/123
POST /api/products
```

---

# Deploying NestJS to Azure Functions

<div class="quote">Stay tuned for future announcements as we will be introducing <code>nest deploy azure</code> and other great integrations that will simplify this process even further!</div>

> **NOTE**: To deploy Azure Functions, you'll need an [Azure Account](https://azure.microsoft.com/free/) (new accounts get 12 months of free services).

When it comes to Deploying your NestJS app to Azure Functions, there are a few different paths you could take, and it all depends on whether you prefer doing everything in the `terminal` or your **IDE** (ie: VSCode).


In this article we're going to showcase deploying via VSCode as it's a bit simpler! Read more about deploying via the `terminal` from the official Azure documentation [here](https://docs.microsoft.com/azure/azure-functions/functions-create-first-azure-function-azure-cli#create-a-resource-group).

## Install the Azure Functions VSCode Extension

Head over to the Extensions Marketplace in VSCode and search for `"Azure Functions"` or download it [here](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions).

> More information on the extension installation and prerequisites [here](https://code.visualstudio.com/tutorials/functions-extension/getting-started).

![VSCode Extensions - Install Azure Functions](https://thepracticaldev.s3.amazonaws.com/i/q3150mqwek6eafmq2gpc.png)

Once installed, you'll notice a new **Icon** on the _left-hand_ sidebar, select it to open up the Azure Functions options.

**1. Login or Signup (to Azure)**

![VSCode Extension - Azure Functions](https://thepracticaldev.s3.amazonaws.com/i/5894y83ebngo6lsdfux5.png)
<!-- <figcaption>Azure Functions VSCode Extension</figcaption> -->

**2. Hit the "Deploy to Function App" **Arrow** icon, and select "Create New Function App in Azure".**

![VSCode Extension - Azure Functions](https://thepracticaldev.s3.amazonaws.com/i/rbieelzu71n5oisosusl.png)

**3. Select a unique name for your Azure Function, and press Enter.**

> **NOTE**: This step may take a few moments as it will be creating Storage Groups, and your new Function App. Future deployments will be much faster, not to worry! :)

![Deploying Azure Function](https://thepracticaldev.s3.amazonaws.com/i/q0ddxsu6xr29clehb34c.png)

#### Deployed!

When everything is finished, check in the `Output` tab (shown above) that your deployment has been finished!

```bash
# --- output similar to ---
# ... etc etc ...
1:54:52 PM nestjs-azure-demo: Deployment successful.
1:55:08 PM nestjs-azure-demo: Waiting for long running command to finish...
Deployment to "nestjs-azure-demo" completed.

HTTP Trigger Urls:
  main: https://nestjs-azure-demo.azurewebsites.net/api/%7B*segments%7D
```

# Serverless NestJS deployed to the ☁ !

If you've been following along, you should be able to access that URL in your output tab, just make sure to remove the last `%7B*segments%7D` portion of the URL.

🐱 Navigate to the URL to see your serverless "Hello World" API in all its eternal glory! 🐱


![Deploy NestJS to Azure Function live demo](https://thepracticaldev.s3.amazonaws.com/i/6ckwu3xbu4j12ulk3cw9.png)
<figcaption>Magical ✨</figcaption>

> **See the "Hello World" NestJS + Azure Functions live demo:<br> [https://nestjs-azure-demo.azurewebsites.net/api](https://nestjs-azure-demo.azurewebsites.net/api)**<br><br>You can find the Github code example [here](https://github.com/TrilonIO/nestjs-azure-functions)

## There we have it!

In just a few minutes, we fired up azure functions locally, and learned how to deploy with the new VSCode Azure Functions Extension, taking our serverless NestJS application to the cloud in no time!

## Future Plans for NestJS & Azure ✨

It's our mission to make this entire process even simpler in the future!

> **Stay tuned for more articles and updates on the upcoming `nest deploy azure`, and other amazing Azure integrations! ✨** <br><br>Azure Storage, improved serverless support, and much more!

---

# In Conclusion

- Azure Functions let you deploy your NestJS applications to the cloud with ease, letting you use all of your other favorite Azure tools from within your application.
- Make sure your scripts are building your NestJS _before_ deploying.
- Deploy through either the command-line or through IDE Extensions
- More NestJS & Azure integrations coming soon!
- Enjoy the ☁ responsibly!

---

Check out the [Trilon Blog](https://trilon.io/blog) for more articles on Angular, NestJS, and much more!

---