Cloud Function Deployment Error on Flutter Web

Cloud function deployment failed

I’m working on a Flutter web app that needs to send SMS and emails through Twilio and SendGrid. To handle emails, I created a Google Cloud Function that triggers every time a new document is created in a Firestore collection called apiusers. The function is supposed to send an email using SendGrid whenever a new user is added to this collection.

Could not create or update Cloud Run service sendusernotification, Container Healthcheck failed. Revision 'sendusernotification-00001-wof' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The healtcheck timeout can be extended. Logs for this revision might contain more information.`

`i  functions: cleaning up build files...`

`⚠  functions: Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at` https://console.cloud.google.com/gcr/images/**/eu/gcf

error: https://imgur.com/a/kfNE5NA

There must be some compile time or runtime error that prevents the function to start.

You can check the functions logs in GCP, maybe it throws some obvious error.

You may want to check if 'npm run build ’ works. Without the source code and error in the code it’s impossible to say what’s wrong.

1 Like

The following is my index.js file

const { onDocumentCreated } = require('firebase-functions/v2/firestore');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const sgMail = require('@sendgrid/mail');
const twilio = require('twilio');

admin.initializeApp();

const SENDGRID_API_KEY =functions.config().sendgrid.key;
const TWILIO_SID = functions.config().twilio.sid;
const TWILIO_TOKEN = functions.config().twilio.token;

sgMail.setApiKey(SENDGRID_API_KEY);


const twilioClient = twilio(TWILIO_SID, TWILIO_TOKEN);

exports.sendUserNotification = onDocumentCreated(
    {
        document: '<Firestore-Collection>/{userId}',
        region: 'europe-west2',
    },
    async (event) => {
        const snapshot = event.data;
        const userData = snapshot.data();

      
        const { Email, FirstName, LastName, Password } = userData;

     
        const emailMessage = {
            to: <receiver-email-address>,
            from: <sender-email-address>
            subject: 'New User Account Created',
            text: `An Email ${Email} has been created with account name ${FirstName} ${LastName}. Here are the login credentials: username - ${Email}, password - ${Password}`,
        };

        const smsMsg = {
            body: `Account created for ${FirstName} ${LastName}. Username: ${Email}, password: ${Password}`,
            from: <sender-contact number>
            to: <receiver-contact-number>,
        };

        try {
            await sgMail.send(emailMessage);
            console.log('Email sent successfully');
            await twilioClient.messages.create(smsMsg);
            console.log('SMS sent successfully');
        } catch (error) {
            console.error('Error sending notification:', error);
        }
    }
);

This is my package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "node index.js",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "18"
  },
  "main": "index.js",
  "dependencies": {
    "@sendgrid/mail": "^8.1.4",
    "cors": "^2.8.5",
    "firebase-admin": "^12.7.0",
    "firebase-functions": "^6.0.1",
    "moment-timezone": "^0.5.46",
    "twilio": "^5.3.5"
  },
  "devDependencies": {
    "firebase-functions-test": "^3.1.0"
  },
  "private": true
}

You are missing values for:

  • document in the function definition - I replaced it with name-of-the-collection
  • to and from in the emailMessage
  • as well as from and to in smsMsg
exports.sendUserNotification = onDocumentCreated(
    {
        document: 'name-of-the-collection/{userId}',
        region: 'europe-west2',
    },
    async (event) => {
        const snapshot = event.data;
        const userData = snapshot.data();


        const { Email, FirstName, LastName, Password } = userData;


        const emailMessage = {
            to: Email,
            from: "my-email-address",
            subject: 'New User Account Created',
            text: `An Email ${Email} has been created with account name ${FirstName} ${LastName}. Here are the login credentials: username - ${Email}, password - ${Password}`,
        };

        const smsMsg = {
            body: `Account created for ${FirstName} ${LastName}. Username: ${Email}, password: ${Password}`,
            from: "my-contact-number",
            to: Email,
        };

        try {
            await sgMail.send(emailMessage);
            console.log('Email sent successfully');
            await twilioClient.messages.create(smsMsg);
            console.log('SMS sent successfully');
        } catch (error) {
            console.error('Error sending notification:', error);
        }
    }
);

Btw I recommend using TypeScript for writing functions, if you use VS Code it will detect these type problems immediately.