I’m upgrading firebase functions, I’m setting up secret manager env vars, however they are only available INSIDE the export function, this means that third party module will need to be initialized every time they are called, is this right?
Normally I would do something like this
var myModule = require("myModule");
myModule.initiliaze({
KEY:process.env.SECRET_KEY
})
exports.myFunction = onCall((request) => {
return myModule.process();
});
But now with secrets not being available outside exports it would have to look like this
var myModule = require("myModule");
exports.myFunction = onCall((request) => {
myModule.initiliaze({
KEY:process.env.SECRET_KEY
})
return myModule.process();
});
If I get a hundred calls an hour, do I really want to initialize 100 separate times?
2
Answers
For some reason my secret vars are unavailable when I first start the emulator, but then become available upon the first call, I still don't want to have initialization code in every single functions, so I ended up with this at the top of the file outside all the exported functions
I’m not sure why your secrets aren’t available in the first code snippet, but if you want to prevent initializing the SDK on each call – consider using a variable to track whether it’s already been initialized in this Functions container.
Something like: