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