lambda ::_您无法从这里到达那里:Netlify Lambda和Firebase如何使我陷入无服务器的死胡同

lambda ::

[Update: Apparently you can get there from here! That is, if you use firebase-admin instead of @google-cloud/firestore.  I'll have more on this in the future, but the gist of it is summarized here.]

[ 更新:显然您可以从这里到达那里! 也就是说,如果您使用firebase-admin而不是@google-cloud/firestore 。 我将有更多的关于这方面的未来,但它的要点总结在这里 。]

A while back I was exploring Netlify's support for FaunaDB: a NoSQL document-oriented database with some special features to handle transactions across dispersed database servers. I decided to try it because it was a convenient choice, since there was example code I could start with. The example used lambda functions as a frontend to the database.

前一段时间,我正在探索Netlify对FaunaDB的支持 :NoSQL面向文档的数据库,具有一些特殊功能,可以处理分散的数据库服务器之间的事务 。 我决定尝试一下,因为这是一个方便的选择,因为我可以从示例代码开始。 该示例使用lambda函数作为数据库的前端。

I modified the original lambda functions to talk to the FaunaDB GraphQL API (instead of FQL). While that worked, in the end I felt Fauna's GraphQL support wasn't quite ripe yet, so I looked around for alternatives.

我修改了原始的lambda函数,以便与FaunaDB GraphQL API(而不是FQL )进行通信。 在此过程中,最后,我感到Fauna对GraphQL的支持还不太成熟,因此我四处寻找替代方案。

Eventually I settled on Cloud Firestore. I based this new project on the Fauna example, swapping out the faunadb module with apollo-server-lambda, so that I could write my own GraphQL API and resolvers.

最后,我选择了Cloud Firestore 。 我基于对动物例如这个新项目,换出与faunadb模块阿波罗-服务器-拉姆达 ,这样我就可以写我自己的GraphQL API和解析器。

One of the refinements I had to make was to push all my Netlify Function dependencies down to the /functions folder in my project (separate and at the same level as the /src folder that contains my React client). To do this, I ran npm init while inside the functions folder, moved a set of dependencies from the top-level package.json to the new /functions/package.json, added a webpack.functions.js, then ran yarn install to pull the packages into a new node_modules folder.

我必须做的一项改进是将我所有的Netlify Function依赖项下推到项目中的/ functions文件夹(与包含我的React客户端的/ src文件夹位于同一级别)。 为此,我在functions文件夹内运行了npm init ,将一组依赖项从顶级package.json移至新的/functions/package.json,添加了webpack.functions.js ,然后将yarn install运行到将软件包拉到新的node_modules文件夹中。

The result was this:

结果是这样的:

I'll talk about the subfolders later; the main thing to notice is that there's yarn files, plus package.json, a node_modules folder, a schema folder, and some .js files for testing.

稍后我将讨论子文件夹。 最要注意的是,这里有yarn文件,package.json,node_modules文件夹,schema文件夹以及一些用于测试的.js文件。

The original project used netlify_lambda to build, which uses webpack and babel. I ran into some issues, fixed them, then ran into them again later.

原始项目使用netlify_lambda进行构建,该项目使用webpack和babel。 我遇到了一些问题 ,将其修复,然后稍后再次遇到它们。

Frustrated, I decided to forego netlify-lambda and chose Netlify Dev to build and deploy from the command line. The drawback was that I didn't have the ability to launch a local server, but I could deploy candidates to Netlify and test them without first checking source into github or deploying directly to production.

沮丧的是,我决定放弃netlify-lambda,选择了Netlify Dev从命令行进行构建和部署。 缺点是我没有启动本地服务器的能力,但是我可以将候选人部署到Netlify并对其进行测试,而无需先将源检查到github或直接部署到生产中。

There were less moving parts since webpack and babel were no longer needed. When going this route, you probably set the environment variable AWS_LAMBDA_JS_RUNTIME to nodejs10.x in the Build & deploy settings for your functions.

由于不再需要webpack和babel,因此移动部件更少。 在执行此路由时,您可能在函数的“ 构建和部署”设置中将环境变量AWS_LAMBDA_JS_RUNTIME设置为nodejs10.x

事情并不总是像看起来那样 (Things are not always as they seem)

More familiar with GraphQL clients and servers than with lambda functions in the cloud, I had some naive assumptions about how things got deployed in Netlify. I thought functions were more or less copied over and build scripts run on the server, where all would be happy and my functions would be callable via URLs.

我对GraphQL客户端和服务器比对云中的lambda函数更加熟悉,我对如何在Netlify中部署事物有一些幼稚的假设。 我认为函数或多或少被复制了,并且构建脚本在服务器上运行,在那里一切都会很高兴,并且我的函数可以通过URL进行调用。

This is not at all what happens.

这根本不发生什么。

When I started with netlify_lambda, it would use webpack to create a functions_build output file. My netlify.toml configuration had that as the functions location.

当我从netlify_lambda开始时,它将使用webpack创建一个functions_build输出文件。 我的netlify.toml配置将其作为函数位置。

[build]
  functions = "functions-build"
  # This will be run the site build
  command = "yarn build"
  # This is the directory is publishing to netlify's CDN
  publish = "build"

When I switch to using Netlify Dev, I dispensed with the output folder and just deployed the "unbundled" /functions source. That's not the end of the story, though.

当我切换为使用Netlify Dev时 ,我放弃了输出文件夹,而只是部署了“未捆绑” / 功能源。 不过,这还不是故事的结局。

身份验证问题 (Authentication woes)

In the FaunaDB project, authentication was through an environment variable whose value was a simple token. A similar mechanism is used by Firebase, but instead of a token, the variable value is a path to a credentials file that you generate through the FireBase console. The lambda functions create a Firebase instance, and that instance looks for the env variable to locate the credentials file for authentication.

在FaunaDB项目中,身份验证是通过环境变量进行的,其值是一个简单的令牌。 Firebase使用类似的机制,但是变量值代替令牌,是通过FireBase控制台生成的凭证文件的路径。 lambda函数创建一个Firebase实例,该实例查找env变量以找到用于身份验证的凭据文件。

It seems like no matter where I put that credentials file or what path I used, the Firebase client would fail to find it. In the course of my research I came across a mention of Netlify's zip-it-and-ship-it utility, which other people with other problems recommended for bundling up functions in zip files.

无论我将凭据文件放在哪里或使用什么路径,Firebase客户端似乎都找不到它。 在研究过程中,我提到了Netlify的zip-it-and-ship-it实用程序,建议其他有其他问题的人将zip文件的功能捆绑在一起。

I tried it, modifying the build process to call a NodeJS script that zipped up my functions to a functions-dist folder (changing the netlify.toml config to no point to that instead of the functions source folder). Although it didn't immediately fix my issues with the credentials file, I noticed some things.

我尝试了一下,修改了构建过程,以调用一个NodeJS脚本,该脚本将我的函数压缩到functions-dist文件夹中(将netlify.toml配置更改为指向该文件夹而不是函数 source文件夹)。 尽管它不能立即解决凭据文件的问题,但我注意到了一些问题。

I began to realize that as each lambda function .js file was bundled up into a zip file, it also contained its own node_modules folder. What's more, the node_modules folder was "customized" to contain only those dependencies explicitly required by each function.

我开始意识到,由于每个lambda函数.js文件都捆绑到一个zip文件中,因此它还包含自己的node_modules文件夹。 此外,“定制” node_modules文件夹以仅包含每个函数明确要求的那些依赖项。

聪明,但不够聪明 (Clever, but not clever enough)

It took some thinking, but I decided that if I added my .json file in a local project, then made it a dependency to each lambda function, it would be pulled in the node_modules folder. At that point, I would have a path: ./creds/mycred.json. Yay!

它花了一些时间,但我决定,如果我在本地项目中添加.json文件,然后使其成为每个lambda函数的依赖项,它将被拉到node_modules文件夹中。 到那时,我将有一条路: ./creds/mycred.json 。 好极了!

It didn't quite work--when I examined the zip files, the credential files were there in each zip archive, but the Firebase client still couldn't get to them.

它不是很有效-当我检查zip文件时,每个zip存档中都存在凭证文件,但是Firebase客户端仍然无法访问它们。

I confessed my utter failure on the Netlify support forum, saying that I planned to join a commune to learn to weave hammocks.

我在Netlify支持论坛上承认自己完全失败,并说我计划加入一个公社来学习编织吊床

救命! (Help!)

I must have evoked some pity, as Dennis from Netlify soon responded and let me know that lambda functions cannot actually access the file system. What I was attempting (loading credentials via a file path) was impossible. He suggested importing the file into each lambda .js (which I had already done). It doesn't appear, though, that the Firebase client allows you to pull in credentials via an import.

我一定引起了一些同情,因为Netlify的Dennis很快做出了回应,并让我知道lambda函数实际上无法访问文件系统。 我尝试的事情(通过文件路径加载凭据)是不可能的。 他建议将文件导入每个lambda .js(我已经完成了)。 但是,似乎没有Firebase客户端允许您通过导入拉入凭据。

That aside, Dennis sort of hinted that perhaps this isn't really the approach I should take, anyway. He had a point. The only reason I went this route was because I was following one of Netlify's examples, but swapping out the faunadb package with apollo-server-lambda might just have added a lot more weight to the lambda functions; if so, it would likely have an affect on spin-up times during cold starts.

除此之外,丹尼斯有点暗示也许这不是我应该采取的方法。 他有一点。 我走这条路线的唯一原因是因为我遵循的是Netlify的示例之一,但是用apollo-server-lambda换出Animaldb程序包可能只会增加lambda函数的权重。 如果是这样,则可能会影响冷启动期间的加速时间。

抛弃Lambda函数 (Ditching lambda functions)

Lambda functions are not a solution for everything. In my case, I only wanted a simple datastore with a GraphQL frontend, without exposing the GraphQL queries in the browser console.

Lambda函数并不能解决所有问题 。 就我而言,我只想要一个带有GraphQL前端的简单数据存储,而没有在浏览器控制台中公开GraphQL查询。

I can achieve the same ends by having a Node process host both a React client and a GraphQL server. I'm (almost) certain I won't run into any file system access problems, and if so, I'll switch to another method of authentication.

我可以通过让Node进程同时托管React客户端和GraphQL服务器来达到相同的目的。 我(几乎)肯定不会遇到任何文件系统访问问题,如果是这样,我将切换到另一种身份验证方法

翻译自: https://www.freecodecamp.org/news/you-cant-get-there-from-here-how-netlify-lambda-and-firebase-led-me-to-a-serverless-dead-end/

lambda ::

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值