How to use Cloud Firestore on AWS Lambda
This guide will teach you how to properly deploy an AWS Lambda Function that uses the Node.js Firebase Admin SDK to call Cloud Firestore.
Before you read any further, it’s worth mentioning that the simplest way to write serverless code is using Cloud Functions for Firebase which works with Cloud Firestore out of the box. This guide is only for those who are determined to use AWS Lambda.
So why does this even need a guide? Well the Node.js SDK for Cloud Firestore uses gRPC to communicate. gRPC is built on native code, and by default AWS Lambda functions can’t run native code. If you just do
npm install and then upload your code, you may get an error message like this when you try to run your lambda function:
Fear not! There’s a fairly simple fix if you read on.
Before we go any further, we need to make sure you’re in the right place. This is not a guide about why you might want to use AWS Lambda Functions nor is this a guide about how to use Cloud Firestore. In addition to knowing about these two services, you’ll need to make sure the following dependencies are installed on your machine:
You must have docker installed on your machine. You can check if you have docker by running
docker run hello-world. If that doesn't work for you, consult the docs for how to install docker.
AWS CLI (Optional)
In this guide we’ll use the AWS CLI to deploy Lambda functions, although you can deploy your functions any way you want. To install the AWS CLI, follow the instructions in the Github README.
We’ll only need a few files to complete this guide. Create a new directory with the following files:
package.json can be extremely simple, all you need is a dependency on
The body of your function should be in in
index.js. Any function that writes to Cloud Firestore will work, here is an extremely simple function that adds a new document on each execution:
The final file,
serviceAccount.json is a JSON key for a service account that is authorized to modify Cloud Firestore data in your Firebase project. This step is required to use
firebase-admin on Lambda and is not specific to using Cloud Firestore. To get a new service account key, go to the Service Accounts page in the Firebase console and select "Node.js" and then click "Generate New Private Key":
If you’re using the code above, rename the resulting JSON file to
serviceAccount.json and move it to the current directory.
Install and Deploy
Now that you have your development environment set up, you are ready to deploy your function. The first step, and the most important step in this guide, is to use docker to install your dependencies:
When this command completes you should have a
node_modules directory that contains a
firebase_admin folder. Using docker to install the dependencies does so in a way that will make the compatible with the runtime environment in your AWS Lambda function.
Now it’s time to deploy your function. First zip up your current directory. This will create a fairly large ZIP file since the whole
node_modules directory is included:
Next use the AWS CLI to deploy your function. You can use any name, region, and role that you want to. The only important arguments here are
Finally, execute your function. If it works you should see something like this:
And in the Firebase Console you should see a new document in the
That’s it! You have successfully written data to Cloud Firestore from an AWS Lambda function.