Firebase Functions Deployment

Deploying Cloud Functions pushes your local function code to Google's servers where it runs live. Firebase provides CLI commands that package your code, upload it, and make it available at a public URL or trigger point. Understanding the deployment process helps you ship updates safely and efficiently.

Prerequisites

Before deploying, confirm these are ready:

  • Firebase CLI installed: npm install -g firebase-tools
  • Logged in: firebase login
  • Project initialized: firebase init functions
  • Project on Blaze plan (required for Cloud Functions)

Deploying All Functions

cd my-app
firebase deploy --only functions

Firebase compiles your code, packages it into a zip, uploads it to Google Cloud, and updates each function. The console shows the status of each function as it deploys.

Deploying a Single Function

Deploy one function at a time to avoid waiting for all functions to update:

firebase deploy --only functions:myFunctionName

This is faster during development when you are iterating on one function.

Deploying Multiple Specific Functions

firebase deploy --only functions:createPost,functions:deletePost

Viewing Deployed Functions

After deployment, Firebase shows the URL of each HTTP function in the terminal output. You can also see all deployed functions in the Firebase Console under Functions:

Function Name   | Trigger        | URL / Trigger Path
----------------|----------------|-----------------------------
api             | HTTP           | https://api-abc.run.app
onPostCreated   | Firestore      | posts/{postId} (create)
dailyCleanup    | Schedule       | every 24 hours
onUserCreated   | Auth           | user().onCreate

Checking Function Logs

firebase functions:log

This streams the most recent logs from all deployed functions. Filter by a specific function:

firebase functions:log --only myFunctionName

Environment Variables and Secrets

Functions often need secret values — third-party API keys, database passwords, SMTP credentials. Never hardcode these in your function code. Firebase provides two options:

Firebase Secret Manager (recommended)

const { defineSecret } = require("firebase-functions/params");

// Define the secret
const stripeKey = defineSecret("STRIPE_SECRET_KEY");

// Use in a function — the secret is injected at runtime
exports.chargeCard = onRequest({ secrets: [stripeKey] }, async (req, res) => {
  const key = stripeKey.value();
  // Use key to call Stripe API
});

Set the secret value in the Firebase Console under Functions > Parameters and secrets, or with the CLI:

firebase functions:secrets:set STRIPE_SECRET_KEY

Environment Variables (non-secret config)

// .env file (not committed to git)
API_URL=https://api.example.com
MAX_BATCH_SIZE=100
// Access in function code
const apiUrl = process.env.API_URL;

Deleting Functions

Remove a function you no longer need:

firebase functions:delete myFunctionName

Firebase asks for confirmation. Alternatively, delete directly in the Firebase Console under Functions by selecting the function and clicking Delete.

Function Versioning and Rollback

Each deployment creates a new version of your function. Firebase keeps recent versions. If a deployment breaks something, you can redeploy a previous version by checking out the old code from your version control system (like Git) and running firebase deploy again.

git checkout previous-stable-commit
firebase deploy --only functions:brokenFunction

Cold Starts

When a function has not been called recently, its runtime container is shut down to save resources. The next call starts a fresh container — called a cold start — which adds a few hundred milliseconds to the response time. Minimize cold starts by:

  • Keeping your function's dependencies minimal
  • Moving admin.initializeApp() outside the function handler
  • Using minimum instances (sets a number of always-warm containers, available on paid plans)
exports.fastFunction = onRequest(
  { minInstances: 1 },  // keep 1 instance always warm
  (req, res) => {
    res.json({ fast: true });
  }
);

Key Takeaway

Deploy functions with firebase deploy --only functions. Deploy individual functions by name during development to speed up iteration. Store secrets in Firebase Secret Manager, never in code. Monitor deployed functions through the Firebase Console and CLI logs. Use minimum instances for latency-sensitive functions that cannot afford cold start delays.

Leave a Comment

Your email address will not be published. Required fields are marked *