docs(node): update serverless deployment recipes

This commit is contained in:
Juri 2023-05-12 17:26:17 +02:00 committed by Juri Strumpflohner
parent 6622883dee
commit 71e8cb2776
10 changed files with 373 additions and 291 deletions

View File

@ -2648,7 +2648,7 @@
"isExternal": false,
"children": [
{
"name": "Deploying a Node.js server to Fly.io",
"name": "Deploying a Node App to Fly.io",
"path": "/recipes/deployment/node-server-fly-io",
"id": "node-server-fly-io",
"isExternal": false,
@ -2656,7 +2656,7 @@
"disableCollapsible": false
},
{
"name": "Deploying Node.js serverless functions to Netlify",
"name": "Add and Deploy Netlify Edge Functions with Node",
"path": "/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"isExternal": false,
@ -2680,9 +2680,9 @@
"disableCollapsible": false
},
{
"name": "Serverless deployment with Deno and Netlify",
"path": "/recipes/deployment/deno-netlify",
"id": "deno-netlify",
"name": "Add and Deploy Netlify Edge Functions with Deno",
"path": "/recipes/deployment/deno-netlify-functions",
"id": "deno-netlify-functions",
"isExternal": false,
"children": [],
"disableCollapsible": false
@ -2691,7 +2691,7 @@
"disableCollapsible": false
},
{
"name": "Deploying a Node.js server to Fly.io",
"name": "Deploying a Node App to Fly.io",
"path": "/recipes/deployment/node-server-fly-io",
"id": "node-server-fly-io",
"isExternal": false,
@ -2699,7 +2699,7 @@
"disableCollapsible": false
},
{
"name": "Deploying Node.js serverless functions to Netlify",
"name": "Add and Deploy Netlify Edge Functions with Node",
"path": "/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"isExternal": false,
@ -2723,9 +2723,9 @@
"disableCollapsible": false
},
{
"name": "Serverless deployment with Deno and Netlify",
"path": "/recipes/deployment/deno-netlify",
"id": "deno-netlify",
"name": "Add and Deploy Netlify Edge Functions with Deno",
"path": "/recipes/deployment/deno-netlify-functions",
"id": "deno-netlify-functions",
"isExternal": false,
"children": [],
"disableCollapsible": false

View File

@ -906,7 +906,7 @@
"itemList": [
{
"id": "node-server-fly-io",
"name": "Deploying a Node.js server to Fly.io",
"name": "Deploying a Node App to Fly.io",
"description": "",
"file": "shared/recipes/deployment/node-server-fly-io",
"itemList": [],
@ -916,7 +916,7 @@
},
{
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"name": "Add and Deploy Netlify Edge Functions with Node",
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"itemList": [],
@ -945,13 +945,13 @@
"tags": ["deployment", "deno"]
},
{
"id": "deno-netlify",
"name": "Serverless deployment with Deno and Netlify",
"id": "deno-netlify-functions",
"name": "Add and Deploy Netlify Edge Functions with Deno",
"description": "",
"file": "shared/recipes/deployment/deno-netlify",
"file": "shared/recipes/deployment/deno-netlify-edge-functions",
"itemList": [],
"isExternal": false,
"path": "/recipes/deployment/deno-netlify",
"path": "/recipes/deployment/deno-netlify-functions",
"tags": ["deployment", "deno"]
}
],
@ -961,7 +961,7 @@
},
"/recipes/deployment/node-server-fly-io": {
"id": "node-server-fly-io",
"name": "Deploying a Node.js server to Fly.io",
"name": "Deploying a Node App to Fly.io",
"description": "",
"file": "shared/recipes/deployment/node-server-fly-io",
"itemList": [],
@ -971,7 +971,7 @@
},
"/recipes/deployment/node-serverless-functions-netlify": {
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"name": "Add and Deploy Netlify Edge Functions with Node",
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"itemList": [],
@ -999,14 +999,14 @@
"path": "/recipes/deployment/deno-deploy",
"tags": ["deployment", "deno"]
},
"/recipes/deployment/deno-netlify": {
"id": "deno-netlify",
"name": "Serverless deployment with Deno and Netlify",
"/recipes/deployment/deno-netlify-functions": {
"id": "deno-netlify-functions",
"name": "Add and Deploy Netlify Edge Functions with Deno",
"description": "",
"file": "shared/recipes/deployment/deno-netlify",
"file": "shared/recipes/deployment/deno-netlify-edge-functions",
"itemList": [],
"isExternal": false,
"path": "/recipes/deployment/deno-netlify",
"path": "/recipes/deployment/deno-netlify-functions",
"tags": ["deployment", "deno"]
},
"/recipes/other": {

View File

@ -805,14 +805,14 @@
"description": "",
"file": "shared/recipes/deployment/node-server-fly-io",
"id": "node-server-fly-io",
"name": "Deploying a Node.js server to Fly.io",
"name": "Deploying a Node App to Fly.io",
"path": "/recipes/deployment/node-server-fly-io"
},
{
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"name": "Add and Deploy Netlify Edge Functions with Node",
"path": "/recipes/deployment/node-serverless-functions-netlify"
},
{
@ -831,10 +831,10 @@
},
{
"description": "",
"file": "shared/recipes/deployment/deno-netlify",
"id": "deno-netlify",
"name": "Serverless deployment with Deno and Netlify",
"path": "/recipes/deployment/deno-netlify"
"file": "shared/recipes/deployment/deno-netlify-edge-functions",
"id": "deno-netlify-functions",
"name": "Add and Deploy Netlify Edge Functions with Deno",
"path": "/recipes/deployment/deno-netlify-functions"
}
],
"node": [
@ -842,14 +842,14 @@
"description": "",
"file": "shared/recipes/deployment/node-server-fly-io",
"id": "node-server-fly-io",
"name": "Deploying a Node.js server to Fly.io",
"name": "Deploying a Node App to Fly.io",
"path": "/recipes/deployment/node-server-fly-io"
},
{
"description": "",
"file": "shared/recipes/deployment/node-serverless-functions-netlify",
"id": "node-serverless-functions-netlify",
"name": "Deploying Node.js serverless functions to Netlify",
"name": "Add and Deploy Netlify Edge Functions with Node",
"path": "/recipes/deployment/node-serverless-functions-netlify"
},
{
@ -870,10 +870,10 @@
},
{
"description": "",
"file": "shared/recipes/deployment/deno-netlify",
"id": "deno-netlify",
"name": "Serverless deployment with Deno and Netlify",
"path": "/recipes/deployment/deno-netlify"
"file": "shared/recipes/deployment/deno-netlify-edge-functions",
"id": "deno-netlify-functions",
"name": "Add and Deploy Netlify Edge Functions with Deno",
"path": "/recipes/deployment/deno-netlify-functions"
}
],
"workspace-watching": [

View File

@ -1161,13 +1161,13 @@
"description": "Deployment recipes for Node.js and Deno server applications.",
"itemList": [
{
"name": "Deploying a Node.js server to Fly.io",
"name": "Deploying a Node App to Fly.io",
"id": "node-server-fly-io",
"tags": ["deployment", "node"],
"file": "shared/recipes/deployment/node-server-fly-io"
},
{
"name": "Deploying Node.js serverless functions to Netlify",
"name": "Add and Deploy Netlify Edge Functions with Node",
"id": "node-serverless-functions-netlify",
"tags": ["deployment", "node"],
"file": "shared/recipes/deployment/node-serverless-functions-netlify"
@ -1185,10 +1185,10 @@
"file": "shared/recipes/deployment/deno-deploy"
},
{
"name": "Serverless deployment with Deno and Netlify",
"id": "deno-netlify",
"name": "Add and Deploy Netlify Edge Functions with Deno",
"id": "deno-netlify-functions",
"tags": ["deployment", "deno"],
"file": "shared/recipes/deployment/deno-netlify"
"file": "shared/recipes/deployment/deno-netlify-edge-functions"
}
]
},

View File

@ -1,30 +1,22 @@
# Serverless deployment with Deno Deploy
# Serverless Deployment with Deno Deploy
In this guide, we'll show you how to deploy serverless functions using [Deno Deploy](https://deno.com/deploy).
## Creating the project
## Prerequisite: A Deno Project
You can create a new Deno project with a single command.
{% callout type="warning" title="Have Deno already installed?" %}
Make sure you have Deno installed on your machine. Consult the [Deno docs for more details](https://deno.com/manual/getting_started/installation)
{% /callout %}
If you don't have a Nx Deno project yet, you can easily create a new one with the following command:
```shell
npx create-nx-workspace@latest denoapp --preset=@nx/deno
```
Once the command is finished, you can `cd` into the workspace.
This creates a single Deno application.
```shell
cd denoapp
```
To run the server, use `nx serve` and the server will start on `http://localhost:8000`. You can also run lint and tests with `nx lint` and `nx test` respectively.
For existing projects, see the next section, otherwise you can skip to [deployment](#deno-deploy).
### Configure existing projects
**Skip this step if you are not configuring an existing project.**
For existing workspaces, you will need to install the `@nx/deno` package.
You can also add a new Deno application to an existing Nx monorepo workspace. Make sure you have the `@nx/deno` package installed:
{% tabs %}
{% tab label="npm" %}
@ -50,23 +42,15 @@ pnpm add -D @nx/deno
{% /tab %}
{% /tabs %}
Now, you can generate a Deno project.
Then generate a new Deno app with the following command:
```shell
nx g @nx/deno:app denoapp
```
You are now ready to deploy the project.
## Configuring Deno Deploy
## Deno Deploy
We will use [Deno Deploy](https://deno.com/deploy) to host our serverless functions. First, you'll need to run the generator to set up your project.
```shell
nx g @nx/deno:setup-serverless --platform=deno-deploy
```
A new `nx deploy` target will be added to your project, but before you can run it there are a few set up steps.
First configure your Deno Deploy project:
1. Push your repository to [GitHub](https://github.com/).
2. Go to [Deno dashboard](https://dash.deno.com/) and set up your Deno project. You need to authorize GitHub to allow access to your repositories, then you need to specify the main file (e.g. `src/main.ts`), and the production branch (e.g. `main`).
@ -74,7 +58,42 @@ A new `nx deploy` target will be added to your project, but before you can run i
4. Add an entry to the project's `.env` file: `DENO_DEPLOY_TOKEN=<token-from-previous-step>` (create this file if needed, and add it to `.gitignore` so you don't commit it)
5. Install the [`deployctl`](https://deno.com/deploy/docs/deployctl) CLI tool.
Once you are done the above steps, you can deploy and view your Deno app!
`deployctl` is a CLI that allows us to deploy our Deno project. We can embed that into our Nx project by creating a `run-command`. The `@nx/deno` plugin already comes with a `setup-deploy` generator that helps with that. Just run:
```shell
nx g @nx/deno:setup-deploy --platform=deno-deploy
```
This adds a new target to your `project.json`
```json {% fileName="project.json"}
{
"name": "denoapp",
...
"targets": {
...
"deploy": {
"executor": "nx:run-commands",
"options": {
"command": "deployctl deploy --project=<Your-Deno-Deploy-Project-Name> --import-map=import_map.json --exclude=node_modules src/main.ts --dry-run"
},
"configurations": {
"preview": {
"command": "deployctl deploy --project=<Your-Deno-Deploy-Project-Name> --import-map=import_map.json --exclude=node_modules src/main.ts"
},
"production": {
"command": "deployctl deploy --project=<Your-Deno-Deploy-Project-Name> --import-map=import_map.json --exclude=node_modules --prod src/main.ts"
}
}
}
}
}
```
## Deploy
Once you are done the above steps, you can deploy and view your Deno app using the following command:
```shell
nx deploy

View File

@ -0,0 +1,136 @@
# Add and Deploy Netlify Edge Functions with Deno
## Prerequisite: A Deno Project
{% callout type="warning" title="Have Deno already installed?" %}
Make sure you have Deno installed on your machine. Consult the [Deno docs for more details](https://deno.com/manual/getting_started/installation)
{% /callout %}
If you don't have a Nx Deno project yet, you can easily create a new one with the following command:
```shell
npx create-nx-workspace@latest denoapp --preset=@nx/deno
```
This creates a single Deno application.
You can also add a new Deno application to an existing Nx monorepo workspace. Make sure you have the `@nx/deno` package installed:
{% tabs %}
{% tab label="npm" %}
```shell
npm i -D @nx/deno
```
{% /tab %}
{% tab label="yarn" %}
```shell
yarn add -D @nx/deno
```
{% /tab %}
{% tab label="pnpm" %}
```shell
pnpm add -D @nx/deno
```
{% /tab %}
{% /tabs %}
Then generate a new Deno app with the following command:
```shell
nx g @nx/deno:app denoapp
```
## Adding a Netlify Serverless Function
To add serverless support to the Deno project, run the `setup-serverless` generator. Pass `--netlify` to the `platform` argument to set it up for Netlify deployment.
```shell
nx g @nx/deno:setup-serverless --platform=netlify
```
This will add a `netlify.toml` file and install the `netlify-cli` package. In addition it also creates a `functions` directory:
```
└─ denoapp
├─ ...
├─ functions
│ └─ hello-geo.ts
├─ src
│ ├─ ...
├─ ...
├─ netlify.toml
├─ nx.json
├─ project.json
└─ package.json
```
The generator updates the `project.json` of your Deno project and adds a new `serve-functions` target that delegates the local serving of the function to the Netlify CLI:
```json {% fileName="project.json" %}
{
"targets": {
...
"serve-functions": {
"command": "npx netlify dev"
}
}
}
```
Just run `nx serve-functions` to start the local server.
## Configure Your Netlify Deploy Settings
Make sure you have a site configured on Netlify (skip if you have already). You have mostly two options:
- either go to [app.netlify.com](https://app.netlify.com) and create a new site
- use the Netlify CLI and run `npx netlify deploy` which will walk you through the process
If you run `npx netlify deploy` in the workspace, the site ID will be automatically saved in the `.netlify/state.json` file. Alternatively adjust the `deploy-functions` in your `project.json` to include the `--site` flag:
```json {% fileName="project.json" %}
{
"targets": {
...
"deploy-functions": {
"executor": "nx:run-commands",
"options": {
"command": "npx netlify deploy"
},
"configurations": {
"production": {
"command": "npx netlify deploy --prod"
}
}
},
}
}
```
## Deploying to Netlify
To deploy them to Netlify, run:
```shell
nx deploy-functions
```
This creates a "draft deployment" to a temporary URL. If you want to do a production deployment, pass the `--prod` flag:
```shell
nx deploy-functions --prod
```
This invokes the "production" configuration of the `deploy-functions` target and passes the `--prod` flag to the Netlify CLI.
{% callout type="info" title="Configure your CI for automated deployments" %}
Note that for a more stable and automated setup you might want to configure your CI to automatically deploy your functions.
{% /callout %}

View File

@ -1,75 +0,0 @@
# Serverless deployment with Deno and Netlify
In this guide, we'll show you how to deploy Deno serverless functions using [Netlify](https://netlify.com/).
## Creating the project
You can create a new Deno project with a single command.
```shell
npx create-nx-workspace@latest denoapp --preset=@nx/deno
```
Once the command is finished, you can `cd` into the workspace.
```shell
cd denoapp
```
To serve the functions locally, run `nx serve-functions` and the server will start on `http://localhost:8888`. The Hello function is available on `http://localhost:8888/api/geo`.
For existing projects, see the next section, otherwise you can skip to [deployment](#deploying-to-netlify).
### Configure existing projects
**Skip this step if you are not configuring an existing project.**
For existing workspaces, you will need to install the `@nx/deno` package.
{% tabs %}
{% tab label="npm" %}
```shell
npm i -D @nx/deno
```
{% /tab %}
{% tab label="yarn" %}
```shell
yarn add -D @nx/deno
```
{% /tab %}
{% tab label="pnpm" %}
```shell
pnpm add -D @nx/deno
```
{% /tab %}
{% /tabs %}
Now, you can generate a Deno project.
```shell
nx g @nx/deno:app denoapp
```
You are now ready to deploy the project.
## Deploying to Netlify
First, you'll need to run the generator to set up your project.
```shell
nx g @nx/deno:setup-serverless --platform=netlify
```
This will add a `netlify.toml` file and install the `netlify-cli` package. You can deploy your app with the following command.
```shell
npx netlify deploy
```
You will be prompted to set up the project when deploying for the first time. Once deployed, the production URL will be logged out, and you can see the Hello function by visiting `https://<deploy-id>.netlify.app/api/geo`.

View File

@ -1,21 +1,25 @@
# Deploying AWS lambda functions in Node.js
# Deploying AWS Lambda Functions in Node.js
Deploying AWS lambda functions in Node.js takes a few steps:
This recipe guides you through setting up AWS Lambda functions with Nx.
## Creating the project
## Getting set up
For new workspaces you can create a Nx workspace with AWS lambda functions with one command:
Depending on your current situation, you can either
- create a new project with the goal of primarily developing and publishing AWS Lambda functions
- add AWS Lambda functions to an existing Node.js project in an Nx workspace
### Starting a New Project
To create a new project, run
```shell
npx create-nx-workspace@latest my-functions \
--preset=@nx/aws-lambda \
npx create-nx-workspace@latest my-functions --preset=@nx/aws-lambda
```
## Configuring existing projects
### Configure Existing Projects
**Skip this step if you are not configuring an existing project.**
You will need to install `@nx/aws-lambda` if you haven't already.
First, make sure you have `@nx/aws-lambda` installed.
{% tabs %}
{% tab label="npm" %}
@ -41,25 +45,39 @@ pnpm add -D @nx/aws-lambda
{% /tab %}
{% /tabs %}
- Add AWS lambda configuration by running the following command:
Next, use the corresponding Nx generator to add the AWS Lambda configuration to an existing project:
```shell
nx generate @nx/aws-lambda:setup-serverless
nx g @nx/aws-lambda:setup-serverless
```
- Create a new aws-lambda project with:
This will setup your project to use AWS Lambda functions:
```shell
nx generate @nx/aws-lambda:serverless
1. Creates a new AWS lambda function in directory `functions/hello-world`.
2. Adds `samconfig.toml` and `template.yaml` in the root of the project.
3. Updates your `project.json` to have 2 new targets `serve-functions` & `deploy-functions`.
## Serve and Develop Your Functions Locally
The `project.json` should have a new target `serve-functions`:
```json {% fileName="project.json" %}
{
"name": "my-functions",
...
"targets": {
...
"serve-functions": {
"command": "sam build && sam local start-api"
},
...
}
}
```
This will do a few things:
This allows to just run `nx serve-functions` to start a local server that serves your functions. As you can see it leverages the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-command-reference.html) underneath.
1. Create a new AWS lambda function in directory `src/hello-world`.
2. Add `samconfig.toml` & `template.yaml` in the root of the project.
3. Update your `project.json` to have 2 new targets `serve` & `deploy`.
## Configure your AWS lambda deploy settings
## Configure Your AWS Lambda Deploy Settings
{% callout type="note" title="Prerequiste" %}
You need to configure your AWS credentials inside AWS before attempting to deploy.
@ -68,29 +86,32 @@ You need to configure your AWS credentials inside AWS before attempting to deplo
## Deployment
Before running your deployments you must have these:
The following requirements need to be met in order to run the AWS Lambda function deployment:
- [SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html#install-sam-cli-instructions) installed on your machine
- [Esbuild](https://esbuild.github.io/getting-started/) available in your PATH
- [esbuild](https://esbuild.github.io/getting-started/) available in your PATH (SAM need this). Example: `npm install -g esbuild`.
```shell
npm install -g esbuild
Your `samconfig.toml` stores default parameters for the SAM CLI. On the other hand, if you want to configure your lambda function settings such as the AWS region, runtime, and handler function, update your `template.yaml`.
The Nx `project.json` already contains a `deploy-functions` target we can invoke to trigger the deployment:
```json {% fileName="project.json" %}
{
"name": "my-functions",
...
"targets": {
...
"deploy-functions": {
"command": "sam build && sam deploy --guided"
}
}
}
```
To view your functions locally you run:
Just run:
```shell
nx serve
nx deploy-functions
```
Your `samconfig.toml` stores default parameters for SAM CLI.
If you want configure your lambda function settings such as the AWS region, runtime, and handler function. You can update your `template.yaml` file located at the root of your project.
To deploy them to AWS you would run:
```shell
nx deploy
```
That's it! Your AWS Lambda function should now be deployed using Nx. You can test it using the endpoint URL provided by Nx, and monitor its execution using the AWS Lambda console or other monitoring tools. Note that you may need to configure additional settings, such as event sources or permissions, depending on your specific use case.
That's it! For monitoring or further permission settings, please refer to the AWS Lambda console.

View File

@ -1,83 +1,34 @@
# Deploying a Node.js server to Fly.io
# Deploying a Node App to Fly.io
In this guide, we'll show you how to go from zero to production using Nx and Node.js server support. We'll use [Fastify](https://www.fastify.io/) as the frameworks of choice, but you can also use [Express](https://expressjs.com/) and [Koa](https://koajs.com/) by selecting those frameworks during project creation. We'll also cover how to configure the same setup for existing Node.js projects.
This recipe guides you through deploying your Nx based Node backend application to [Fly.io](https://fly.io/).
**Prefer a video version? Watch this short video to learn how to start a Node.js server project with Nx.**
{% youtube
src="https://www.youtube.com/embed/K4f-fMuAoRY"
title="Build Node backends - The Easy Way!"
width="100%" /%}
## Creating the project
You can create a new server project with a single command.
If you don't have an Nx powered Node project already, you can create a new one
```bash
npx create-nx-workspace@latest my-api \
--preset=node-standalone \ # create a Node.js project
--framework=fastify \ # other options are express and koa
--docker # generates a Dockerfile (we'll need this)
--preset=node-standalone \
--framework=fastify \
--docker
```
Once the command is finished, you can `cd` into the workspace.
This example uses [Fastify](https://www.fastify.io/) but you can equally use [Express](https://expressjs.com/), [Koa](https://koajs.com/) or [NestJS](https://nestjs.com/) by selecting those frameworks during project creation.
```bash
cd my-api
You can also install the `@nx/node` package into an existing Nx monorepo and generate a new Node application.
{% cards cols="1" %}
{% card title="Build Node backends - The Easy Way!" description="Starting with Nx 15.7 we now have first-class support for building Node backend applications" type="video" url="https://youtu.be/K4f-fMuAoRY" /%}
{% card title="Easy, Modular Node Applications!" description="In this video, we're going to explore how to modularize a Node backend application" type="video" url="https://youtu.be/LHLW0b4fr2w" /%}
{% /cards %}
## Setup Docker
If you don't have a Docker setup already, you can leverage the `setup-docker` generator from the `@nx/node` package to create one:
```shell
npx nx g @nx/node:setup-docker
```
To run the server, use `nx serve` and the server should start on `http://localhost:3333`. You can also run the e2e tests against the running server by running `nx e2e e2e` in a separate tab. Note that the `e2e` project is separate from the server project, thus the need to specify it in the `nx e2e <project>` command.
For existing projects, see the next section, otherwise you can skip to [deployment](#deploying-the-server-to-fly.io).
### Configure existing projects
**Skip this step if you are not configuring an existing project.**
If you have an existing Node.js server project, you can add the same deployment capabilities as we've just covered. Firstly, if the project is not an Nx project you can initialize it as such by running the `npx nx init` command in your project. Next, we can add the `build` and `docker-build` targets by invoking a couple of generators.
You will need to install `@nx/node` and `@nx/esbuild` if you haven't already.
{% tabs %}
{% tab label="npm" %}
```bash
npm i -D @nx/node @nx/esbuild
```
{% /tab %}
{% tab label="yarn" %}
```bash
yarn add -D @nx/node @nx/esbuild
```
{% /tab %}
{% tab label="pnpm" %}
```bash
pnpm add -D @nx/node @nx/esbuild
```
{% /tab %}
{% /tabs %}
Now, set up the build and Docker targets with these commands. You will be prompted to select the project to configure.
```bash
# Add build target
# You can skip this step if your project already has a build target.
nx g @nx/esbuild:esbuild-project --skipValidation
# Add Dockerfile
nx g @nx/node:setup-docker
```
You are now ready to deploy the project.
## Deploying the server to Fly.io
Recall that a `Dockerfile` has been created for our project. If you missed the Docker setup, you can always run the `nx g @nx/node:setup-docker` command to generate the `Dockerfile`.
## Deploying the Server to Fly.io
Now, all we need to do is set up Fly.io and deploy! If you haven't used Fly.io before, you need to install the CLI and create an account. It'll only take a couple of minutes.
@ -92,9 +43,15 @@ Once you have authenticated using `fly`, we are ready to launch our project.
fly launch --generate-name --no-deploy
```
Follow the prompts and a `fly.toml` file will be generated, which contains the Fly.io configuration. We need to update this file with the correct port used by our image.
{% callout type="warning" title="Dockerignore" %}
```
The setup process of Fly asks about creating a `.dockerignore` file based on the current `.gitignore`. If you confirm that step, make sure to remove `**/dist` from the `.dockerignore` file, otherwise the build will fail because the `Dockerfile` copies the build output from the `dist/` folder.
{% /callout %}
Once the setup completes, update the `fly.toml` file and make sure it uses the correct port:
```toml {% fileName="fly.toml" %}
[[services]]
http_checks = []
internal_port = 3000 # Make sure this matches the port in Dockerfile
@ -111,10 +68,13 @@ Fly.io will log out the monitoring link when the server is successfully deployed
That's is! Our server is now deployed for the world to use.
{% callout type="note" title="Adding deploy command" %}
You can add a `deploy` target to the project by add this to the `project.json` file (under `"targets"`).
## Optional: Adding a Deploy Target
```json
You can also automate the deployment by adding a target to your project. In addition, that allows us to leverage the Nx [task pipeline](/concepts/task-pipeline-configuration) to make sure we first run the `build` and then the `deploy`.
By using [Nx run-commands](/recipes/executors/run-commands-executor), you can add a `deploy` target to the project. Go to the project's `project.json` file (under `"targets"`) and add the following:
```json {% fileName="project.json" %}
"deploy": {
"dependsOn": [
"build"
@ -123,6 +83,4 @@ You can add a `deploy` target to the project by add this to the `project.json` f
}
```
Then you can run `nx deploy`, which will run the build (if necessary) before deploying.
{% /callout %}
Then you can run `nx deploy`, which will run the build (if necessary) before deploying. If the build ran before, **it would be cached**.

View File

@ -1,20 +1,25 @@
# Deploying Node.js serverless functions to Netlify
# Add and Deploy Netlify Edge Functions with Node
Deploying Node.js serverless functions to Netlify involes a few steps:
Deploying Node.js serverless functions to Netlify involves a few steps.
## Creating the project
## Getting set up
Depending on your current situation, you can either
- create a brand new project with the intention of solely hosting and deploying Netlify functions
- adding Netlify functions to an existing project
Let's walk through both scenarios.
### Starting a New Project
For new workspaces you can create a Nx workspace with serverless function with one command:
```shell
npx create-nx-workspace@latest my-functions \
--preset=@nx/netlify \
--site=my-site \ # Site ID or name to deploy the functions
npx create-nx-workspace@latest my-functions --preset=@nx/netlify --site=my-site
```
## Configuring existing projects
**Skip this step if you are not configuring an existing project.**
### Configure Existing Projects
You will need to install `@nx/netlify` if you haven't already.
@ -42,52 +47,70 @@ pnpm add -D @nx/netlify
{% /tab %}
{% /tabs %}
- Add serverless configuration by running the following command:
Next add the Netlify serverless configuration by running the following command:
```shell
nx generate @nx/netlify:setup-serverless
```
- Create a new netlify serverless project with:
```shell
nx generate @nx/netlfiy:serverless
nx g @nx/netlify:setup-serverless
```
This will do a few things:
1. Create a new serverless function in `src/functions`.
2. Add the `netlify.toml` in the root of the project
3. Update your `project.json` to have 2 new targets `serve` & `deploy`.
3. Update your `project.json` to have 2 new targets `serve-functions` & `deploy-functions`.
## Configure your Netlify deploy settings
## Serve Your Functions Locally
If you **do not** have a _site_ setup within your workspace, go inside the Netlify dashboard, and create/use an existing site where your serverless functions will be deployed.
To serve your functions locally, run:
In your `project.json` you can update your deploy site by going to the `deploy` target adding `--site=my-site-name-or-id` replace **my-site-name-or-id** to what you have in your Netlify dashboard.
```shell
nx serve-functions
```
It should look similar to:
## Configure Your Netlify Deploy Settings
```json
"deploy": {
Make sure you have a site configured on Netlify (skip if you have already). You have mostly two options:
- either go to [app.netlify.com](https://app.netlify.com) and create a new site
- use the Netlify CLI and run `npx netlify deploy` which will walk you through the process
If you run `npx netlify deploy` in the workspace, the site ID will be automatically saved in the `.netlify/state.json` file. Alternatively adjust the `deploy-functions` in your `project.json` to include the `--site` flag:
```json {% fileName="project.json" %}
{
"targets": {
...
"deploy-functions": {
"dependsOn": ["lint"],
"command": "npx netlify deploy --prod-if-unlocked --site=my-site"
"command": "npx netlify deploy --site=YOUR_SITE_ID",
"configurations": {
"production": {
"command": "npx netlify deploy --site=YOUR_SITE_ID --prod"
}
}
}
}
}
```
## Deployment
To view your functions locally you run:
To deploy them to Netlify, run:
```shell
nx serve
nx deploy-functions
```
Inside your `netlify.toml` your functions _path_ should be already configured.
To deploy them to Netlify you would run:
This creates a "draft deployment" to a temporary URL. If you want to do a production deployment, pass the `--prod` flag:
```shell
nx deploy
nx deploy-functions --prod
```
The netlify CLI will output the link where you functions can be accessed. You can either click that link or open your browser and navigate to it!
This invokes the "production" configuration of the `deploy-functions` target and passes the `--prod` flag to the Netlify CLI.
{% callout type="info" title="Configure your CI for automated deployments" %}
Note that for a more stable and automated setup you might want to configure your CI to automatically deploy your functions.
{% /callout %}