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.pswp.cn/news/391464.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391464.shtml
英文地址,請注明出處:http://en.pswp.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是一種一維的…