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 ::

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391464.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode 264. 丑数 II(堆)

给你一个整数 n ,请你找出并返回第 n 个 丑数 。 丑数 就是只包含质因数 2、3 和/或 5 的正整数。 示例 1: 输入:n 10 输出:12 解释:[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] 是由前 10 个丑数组成的序列。 解题思路 维…

奇迹网站可视化排行榜]_外观可视化奇迹

奇迹网站可视化排行榜]When reading a visualization is what we see really what we get?阅读可视化内容时,我们真正看到的是什么? This post summarizes and accompanies our paper “Surfacing Visualization Mirages” that was presented at CHI …

Oracle自动性能统计

Oracle自动性能统计 高效诊断性能问题,需要提供完整可用的统计信息,好比医生给病人看病的望闻问切,才能够正确的确诊,然后再开出相应的药方。Oracle数据库为系统、会话以及单独的sql语句生成多种类型的累积统计信息。本文主要描述…

numpy2

1、通用函数,是一种在ndarray数据中进行逐元素操作的函数。某些函数接受一个或多个标量数值,并产生一个或多个标量结果,通用函数就是对这些函数的封装。 1、常用的一元通用函数有:abs\fabs  sqrt   square  exp  log\log2…

Apache Prefork、Worker和Event三种MPM简单分析

(1) Prefork MPM (优点) :使用多个子进程,每个子进程只有一个线程来处理一个 http 连接,不用担心线程安全问题缺点:内存消耗大,不擅长处理高并发环境,使用keep-alive长连接时要等到超…

grasshopper_如何使用Google的Grasshopper编码应用程序来学习手机上的编码基础知识...

grasshopper什么是蚱hopper? (What is Grasshopper?) Grasshopper is an interactive education app for learning about coding. It began at Google as an experimental project created by a group called Area 120. Grasshopper是一个用于学习编码的交互式教育…

机器学习 量子_量子机器学习:神经网络学习

机器学习 量子My last articles tackled Bayes nets on quantum computers (read it here!), and k-means clustering, our first steps into the weird and wonderful world of quantum machine learning.我的最后一篇文章讨论了量子计算机上的贝叶斯网络( 在这里阅读&#xf…

leetcode 179. 最大数(排序)

给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。 注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。 示例 1: 输入:nums [10,2] 输出&a…

test3

test3 转载于:https://www.cnblogs.com/Forever77/p/11441068.html

linux渗透测试_渗透测试:选择正确的(Linux)工具栈来修复损坏的IT安全性

linux渗透测试Got IT infrastructure? Do you know how secure it is? The answer will probably hurt, but this is the kind of bad news you’re better off getting sooner rather than later.有IT基础架构吗? 你知道它有多安全吗? 答案可能会很痛…

BZOJ 1176: [Balkan2007]Mokia

一道CDQ分治的模板题,然而我De了一上午Bug...... 按时间分成左右两半,按x坐标排序然后把y坐标丢到树状数组里,扫一遍遇到左边的就add,遇到右边的query 几个弱智出了bug的点, 一是先分了左右两半再排序,保证的是这次的左…

深入理解InnoDB(1)—行的存储结构

1.InnoDB页的简介 页(Page)是 Innodb 存储引擎用于管理数据的最小磁盘单位。常见的页类型有数据页、Undo 页、系统页、事务数据页等 2.InnoDB行的存储格式 我们插入MySQL的记录在InnoDB中可能以4中行格式存储,分别是Compact、Redundant、D…

做嵌入式的必须学Android吗

做嵌入式的必须学Android吗Android方向适合哪些人呢?适合那些已经在自己领域有了一定的工作经验的人,适合作为自己的拓展,适合提升自己的能力,譬如说已经做三年Linux驱动,就可以尝试拓展去做Android驱动首先从技术角度…

test4

test4 转载于:https://www.cnblogs.com/Forever77/p/11441980.html

boltzmann_推荐系统系列第7部分:用于协同过滤的Boltzmann机器的3个变体

boltzmannRecSys系列 (RecSys Series) Update: This article is part of a series where I explore recommendation systems in academia and industry. Check out the full series: Part 1, Part 2, Part 3, Part 4, Part 5, Part 6, and Part 7.更新: 本文是我探索…

.net 初学者_在此初学者课程中学习使用TensorFlow 2.0开发神经网络

.net 初学者Learn how to use TensorFlow 2.0 in this full video course from Tech with Tim. This course will show you how to create neural networks with Python and TensorFlow 2.0.在Tech与Tim的完整视频课程中,学习如何使用TensorFlow 2.0。 本课程将向您…

AndroidStudio怎样导入library项目开源库 - 转

https://jingyan.baidu.com/article/1974b2898917aff4b1f77415.html转载于:https://www.cnblogs.com/EasyLive2006/p/7477719.html

深入理解InnoDB(2)—页的存储结构

1. 记录头信息 上一篇博客说到每行记录都会有记录头信息,用来记录每一行的一些属性 Compact行记录的记录头信息为例 1.1 delete_mask 这个属性标记着当前记录是否被删除,占用1个二进制位,值为0的时候代表记录并没有被删除,为1的…

PHP中的命名空间

1. PHP中的命名空间是什么? 官方解释在此: 命名空间概述 命名空间用一句话说,就是:把 类、函数、变量 等放到逻辑子文件夹中去,以避免命名冲突。 注:命名空间跟实际代码文件在文件系统中的路径没有任何关系…

pandas 入门

pandas简介:pandas包含的数据结构和数据处理工具的设计使得利用进行数据清洗和数据分析非常快捷;与numpy的区别,pandas用来处理表格型或异质型数据的,而numpy更适合处理同质型的数值类数据。 1、Series简介 1、Series是一种一维的…