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 buildcommand = "yarn build"# This is the directory is publishing to netlify's CDNpublish = "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 ::
lambda ::_您无法从这里到达那里:Netlify Lambda和Firebase如何使我陷入无服务器的死胡同相关推荐
- lambda :: -_无需再忙了:Lambda-S3缩略图,由SLAppForge Sigma钉牢!
lambda :: -> 如果你还没有注意到了,我最近被唠叨试图开始使用时,我遭遇了陷阱AWSλ-S3正式例子 . 虽然大多数这些愚蠢的错误的指责是对我自己的懒惰,过度自尊和缺乏对细节的关注,我 ...
- 七牛服务器入门教程_教程:使用无服务器,StepFunction和StackStorm构建社区的入门应用程序…...
七牛服务器入门教程 by Dmitri Zimine 由Dmitri Zimine 使用无服务器,StepFunction和StackStorm Exchange构建社区注册应用 (Building ...
- 云服务器面临的问题_无服务器安全面临的多方面威胁以及我们应如何应对
云服务器面临的问题 by Yan Cui 崔燕 无服务器安全面临的多方面威胁以及我们应如何应对 (The many-faced threats to Serverless security, and ...
- aws lambda使用_使用AWS Lambda,无服务器框架和Go的按需WebSockets
aws lambda使用 Lambda functions and WebSockets can be seen as concepts difficult to reconcile. Lambdas ...
- aws lambda使用_如何使用AWS Lambda和S3构建无服务器URL缩短器
aws lambda使用 by Daniel Ireson 丹尼尔·埃里森(Daniel Ireson) 如何使用AWS Lambda和S3构建无服务器URL缩短器 (How to build a S ...
- lambda 对象去重_最火的java8新特性:Lambda 表达式
主要内容 1. Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 其他新特性 Java 8新特性简介 速度更快 代码更少(增加了新的语法 Lambd ...
- java lambda::_基准测试:Java 8 Lambda和流如何使您的代码慢5倍
java lambda:: 与长期的实现相比,Java 8 lambda和流的性能如何? Lambda表达式和流在Java 8中受到了热烈的欢迎.这些是迄今为止很激动人心的功能,很长一段时间以来,它们 ...
- lambda表达式java项目常用_一文带你彻底搞懂Lambda表达式
1. 为什么使用Lambda表达式 Lambda是一个匿名函数,我们可以把Lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递).可以写出更简洁.更灵活的代码.作为一种更紧凑的代码风 ...
- sam服务器是什么_使用SAM CLI将机器学习模型部署到无服务器后端
sam服务器是什么 介绍 (Introduction) Over the last year at CMRA, we have been incorporating more machine lear ...
最新文章
- 使用eclipse搭建嵌入式开发环境
- VM:如何向vmware虚拟机中传输文件(或者共享文件夹)之详细攻略(图文教程)
- 【行业进展】国内自动驾驶发展的怎么样了?
- [转载]JDBC/Spring/MyBatis性能比较
- C# 禁止修改已装箱了的值类型的字段值,但是可以通过接口的方式实现
- mybatis mysql 模糊查询语句_mybatis+Spring mysql的模糊查询问题
- hadoop无法启动DataNode问题
- linux 按序号创建文件夹,在Linux终端中创建M3U播放列表的方法
- SpringMVC简单映射请求参数介绍
- php获取客户端IP
- linux tar命令将压缩包解压到指定位置,用tar命令把目标压缩包解压到指定位置
- 读书笔记—中国跑步指南
- 批量同时创建邮箱和AD账户
- android n换行格式,Android 写文件生成器的时候换行请用\r\n
- AMCL中odom的数据处理
- 直播继续搅局双11?
- 七夕快到了,你怎么还不向我表白?
- 设计一个高精度的ADC采样电路思路
- 基于simulink的PN码相关峰同步仿真
- Zcash中的description