javascriptgoogle-cloud-platformgoogle-cloud-storageserverlessnetlify-function

Netlify Scheduled Functions not saving file to GCP in prod


I have a simple handler that fetches data from an api, and saves the JSON content in a GCP bucket every 10 minutes.

export const handler: Handler = async () => {
  const credentials = require("./project-name-digits.json");
  const storage = new Storage({
    projectId: credentials.project_id,
    credentials,
  });
  
  const bucket = storage.bucket("my-bucket");
  const result = await axios("example.com/api");

  const file = bucket.file("my-filename");
  const contents = JSON.stringify(result.data);

  file.save(contents);

  return {
    statusCode: 200,
  };
};

My netlify.toml file specifies to include json file

[functions]
    node_bundler = "esbuild"

[functions."scheduled-lending"]
    schedule = "*/10 * * * *"
    included_files = ["project-name-digits.json"]

I'm able to run the fn locally and it writes to the GCP bucket without errors.

I can log the credentials and it does show up correctly when the handler runs in prod, however it doesn't actually write to the GCP bucket.

I have the service account set as Storage Object Adminrole for the bucket.

What am I missing here?


Solution

  • I'm not an expert on Netlify functions, but if they behave anything at all like Google Cloud Functions, you are obliged to wait for any and all promises to resolve before sending a response to the caller. Failure to do so means that any in-flight asynchronous work might get shut down by the runtime in order to save on compute costs.

    This means that you should await result of file.save() in order to ensure that it completes.