You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+80-25Lines changed: 80 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -1,41 +1,55 @@
1
-
## Netlify Lambda CLI
1
+
## Netlify Lambda
2
2
3
-
This is a small CLI tool that helps with building or serving lambdas built with a simple webpack/babel setup.
3
+
This is an optional tool that helps with building or locally developing [Netlify Functions](https://www.netlify.com/docs/functions/)with a simple webpack/babel build step.
4
4
5
-
The goal is to make it easy to work with Lambda's with modern ES6 without being dependent on having the most state of the art node runtime available in the final deployment environment and with a build that can compile all modules into a single lambda file.
6
-
7
-
Since v1.0.0 the dependencies were upgraded to Webpack 4 and Babel 7.
5
+
The goal is to make it easy to write Lambda's with transpiled JS/Typescipt features and imported modules.
8
6
9
7
## Installation
10
8
11
-
**We recommend installing locally** rather than globally: `yarn add -D netlify-lambda`. This will ensure your build scripts don't assume a global install which is better for your CI/CD (for example with Netlify's buildbot).
9
+
**We recommend installing locally** rather than globally:
10
+
11
+
```bash
12
+
yarn add -D netlify-lambda
13
+
```
14
+
15
+
This will ensure your build scripts don't assume a global install which is better for your CI/CD (for example with Netlify's buildbot).
16
+
17
+
If you don't have a [`netlify.toml`](https://www.netlify.com/docs/netlify-toml-reference/) file, you'll need one ([example](https://github.com/netlify/create-react-app-lambda/blob/master/netlify.toml)). Define the `functions` field where the functions will be built to and served from, e.g.
18
+
19
+
```toml
20
+
# example netlify.toml
21
+
[build]
22
+
command = "yarn build"
23
+
functions = "lambda"# netlify-lambda reads this
24
+
publish = "build"
25
+
```
12
26
13
27
## Usage
14
28
15
-
Netlify lambda installs two commands:
29
+
We expose two commands:
16
30
17
31
```
18
32
netlify-lambda serve <folder>
19
33
netlify-lambda build <folder>
20
34
```
21
35
22
-
**IMPORTANT**: Both commands depend on a `netlify.toml` file being present in your project and configuring functions for deployment.
36
+
At a high level, `netlify-lambda` takes a source folder (e.g. `src/lambda`, specified in your command) and outputs it to a built folder, (e.g. `built-lambda`, specified in your `netlify.toml` file).
23
37
24
-
The `serve` function will start a dev server and a file watcher for the specified folder and route requests to the relevant function at:
38
+
The `build` function will run a single build of the functions in the folder.
39
+
40
+
The `serve` function will start a dev server for the source folder and route requests with a `.netlify/functions/` prefix, with a default port of `9000`:
25
41
26
42
```
27
-
http://localhost:9000/hello -> folder/hello.js (must export a handler(event, context callback) function)
The `build` function will run a single build of the functions in the folder.
46
+
It also watches your files and restarts the dev server on change. Note: if you add a new file you should kill and restart the process to pick up the new file.
31
47
32
-
There are additional options, introduced later:
33
-
```bash
34
-
-h --help
35
-
-c --config
36
-
-p --port
37
-
-s --static
38
-
```
48
+
**IMPORTANT**:
49
+
50
+
- You need a [`netlify.toml`](https://www.netlify.com/docs/netlify-toml-reference/) file with a `functions` field.
51
+
- Every function needs to be a top-level js/ts/mjs file. You can have subfolders inside the `netlify-lambda` folder, but those are only for supporting files to be imported by your top level function.
52
+
- Function signatures follow the [AWS event handler](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html) syntax but must be named `handler`. [We use Node v8](https://www.netlify.com/blog/2018/04/03/node.js-8.10-now-available-in-netlify-functions/) so `async` functions **are** supported ([beware common mistakes](https://serverless.com/blog/common-node8-mistakes-in-lambda/)!). Read [Netlify Functions docs](https://www.netlify.com/docs/functions/#javascript-lambda-functions) for more info.
39
53
40
54
## Using with `create-react-app`, Gatsby, and other development servers
41
55
@@ -53,9 +67,10 @@ When your function is deployed on Netlify, it will be available at `/.netlify/fu
53
67
54
68
Say you are running `webpack-serve` on port 8080 and `netlify-lambda serve` on port 9000. Mounting `localhost:9000` to `/.netlify/functions/` on your `webpack-serve` server (`localhost:8080/.netlify/functions/`) will closely replicate what the final production environment will look like during development, and will allow you to assume the same function url path in development and in production.
55
69
56
-
- If you are using with `create-react-app`, see [netlify/create-react-app-lambda](https://github.com/netlify/create-react-app-lambda/blob/f0e94f1d5a42992a2b894bfeae5b8c039a177dd9/src/setupProxy.js) for an example of how to do this with `create-react-app`. [setupProxy is partially documented in the CRA docs](https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually).
57
-
- If you are using Gatsby, see [their Advanced Proxying docs](https://www.gatsbyjs.org/docs/api-proxy/#advanced-proxying). This is implemented in the [JAMstack Hackathon Starter](https://github.com/sw-yx/jamstack-hackathon-starter).
70
+
- If you are using with `create-react-app`, see [netlify/create-react-app-lambda](https://github.com/netlify/create-react-app-lambda/blob/f0e94f1d5a42992a2b894bfeae5b8c039a177dd9/src/setupProxy.js) for an example of how to do this with `create-react-app`. [setupProxy is partially documented in the CRA docs](https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually). You can also learn how to do this from scratch in a video: https://www.youtube.com/watch?v=3ldSM98nCHI
71
+
- If you are using Gatsby, see [their Advanced Proxying docs](https://www.gatsbyjs.org/docs/api-proxy/#advanced-proxying). This is implemented in the [JAMstack Hackathon Starter](https://github.com/sw-yx/jamstack-hackathon-starter), and here is an accompanying blogpost: [Turning the Static Dynamic: Gatsby + Netlify Functions + Netlify Identity](https://www.gatsbyjs.org/blog/2018-12-17-turning-the-static-dynamic/).
58
72
- If you are using Next.js, see [this issue for how to proxy](https://github.com/netlify/netlify-lambda/pull/28#issuecomment-439675503).
73
+
- If you are using Vue CLI, you may just use https://github.com/netlify/vue-cli-plugin-netlify-lambda/.
59
74
- If you are using with Angular CLI, see the instructions below.
The serving port can be changed with the `-p`/`--port` option.
78
-
79
92
<details>
80
93
<summary>
81
94
**Using with `Angular CLI`**
@@ -137,7 +150,9 @@ The additional webpack config will be merged into the default config via [webpac
137
150
138
151
The default webpack configuration uses `babel-loader` with a [few basic settings](https://github.com/netlify/netlify-lambda/blob/master/lib/build.js#L19-L33).
139
152
140
-
However, if any `.babelrc` is found in the directory `netlify-lambda` is run from, it will be used instead of the default one. If you need to run different babel versions for your lambda and for your app, [check this issue](https://github.com/netlify/netlify-lambda/issues/34) to override your webpack babel-loader.
153
+
However, if any `.babelrc` is found in the directory `netlify-lambda` is run from, or [folders above it](https://github.com/netlify/netlify-lambda/pull/92) (useful for monorepos), it will be used instead of the default one.
154
+
155
+
If you need to run different babel versions for your lambda and for your app, [check this issue](https://github.com/netlify/netlify-lambda/issues/34) to override your webpack babel-loader.
141
156
142
157
### Use with TypeScript
143
158
@@ -176,6 +191,23 @@ You may also want to add `typescript @types/node @types/aws-lambda`.
176
191
177
192
3. (Optional) if you have `@types/aws-lambda` installed, your lambda functions can use the community typings for `Handler, Context, Callback`. See the typescript instructions in [create-react-app-lambda](https://github.com/netlify/create-react-app-lambda/blob/master/README.md#typescript) for an example.
178
193
194
+
Check https://github.com/sw-yx/create-react-app-lambda-typescript for a CRA + Lambda full Typescript experience.
195
+
196
+
## CLI flags/options
197
+
198
+
There are additional CLI options:
199
+
200
+
```bash
201
+
-h --help
202
+
-c --config
203
+
-p --port
204
+
-s --static
205
+
```
206
+
207
+
### --port option
208
+
209
+
The serving port can be changed with the `-p`/`--port` option.
210
+
179
211
### --static option
180
212
181
213
If you need an escape hatch and are building your lambda in some way that is incompatible with our build process, you can skip the build with the `-s` or `--static` flag. [More info here](https://github.com/netlify/netlify-lambda/pull/62).
@@ -193,15 +225,38 @@ Don't forget to search our issues in case someone has run into a similar problem
193
225
194
226
## Netlify Identity
195
227
196
-
Netlify Identity is [not supported at the moment](https://github.com/netlify/netlify-lambda/issues/51) inside `netlify-lambda` function emulation, but for now you can [read the docs](https://www.netlify.com/docs/functions/#identity-and-functions) on how they should work.
228
+
Make sure to [read the docs](https://www.netlify.com/docs/functions/#identity-and-functions) on how Netlify Functions and Netlify Identity work together. Basically you have to make your request with an `authorization` header and a `Bearer` token with your Netlify Identity JWT supplied. You can get this JWT from any of our Identity solutions from [gotrue-js](https://github.com/netlify/gotrue-js) to [netlify-identity-widget](https://github.com/netlify/netlify-identity-widget).
229
+
230
+
Since for practical purposes we cannot fully emulate Netlify Identity locally, we provide [simple JWT decoding inside the `context` of your function](https://github.com/netlify/netlify-lambda/pull/57). This will give you back the `user` info you need to work with.
231
+
232
+
Minor note: For the `identity` field, since we are not fully emulating Netlify Identity, we can't give you details on the Identity instance, so we give you [unambiguous strings](https://github.com/netlify/netlify-lambda/blob/master/lib/serve.js#L87) so you know not to rely on it locally: `NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL` and `NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN`. In production, of course, Netlify Functions will give you the correct `identity.url` and `identity.token` fields. We find we dont use this info often in our functions so it is not that big of a deal in our judgment.
233
+
234
+
## Example functions and Tutorials
235
+
236
+
You can do a great deal with lambda functions! Here are some examples for inspiration:
- Raymond Camden's [Adding Serverless Functions to Your Netlify Static Site](https://www.raymondcamden.com/2019/01/08/adding-serverless-functions-to-your-netlify-static-site)
244
+
- Travis Horn's [Netlify Lambda Functions from Scratch](https://travishorn.com/netlify-lambda-functions-from-scratch-1186f61c659e)
245
+
-[**Submit your blogpost here!**](https://github.com/netlify/netlify-lambda/issues/new)
246
+
247
+
These libraries pair very well for extending your functions capability:
-[Any others to suggest?](https://github.com/netlify/netlify-lambda/issues/new)
197
252
198
253
## Other community approaches
199
254
200
255
If you wish to serve the full website from lambda, [check this issue](https://github.com/netlify/netlify-lambda/issues/36).
201
256
202
257
If you wish to run this server for testing, [check this issue](https://github.com/netlify/netlify-lambda/issues/49).
203
258
204
-
If you wish to emulate more Netlify functionality locally, [check this repo](https://github.com/8eecf0d2/netlify-local).
259
+
If you wish to emulate more Netlify functionality locally, check this repo: https://github.com/8eecf0d2/netlify-local. We are considering merging the projects [here](https://github.com/netlify/netlify-lambda/issues/75).
205
260
206
261
All of the above are community maintained and not officially supported by Netlify.
0 commit comments