Search code examples
twilio

Twilio serverless update asset without redeploy


I have a Twilio Service where the function loads a private text asset and returns a value based on the contents of that file.

// Read memo_assets from assets
const memo_assets_path = Runtime.getAssets()[MEMO_ASSETS_PATH].path;
const buffer = fs.readFileSync(memo_assets_path);
const memo_assets = buffer.toString().split("\n");

This works as desired, however when I update the contents of MEMO_ASSETS_PATH, I need to redeploy in order to pick up the changed file.

Is there a way to avoid the redeploy? I tried making the MEMO_ASSETS_PATH protected but that created errors due to not being able to find the file.


Solution

  • The solution you're looking for may not be possible in the exact way you've described. Once you deploy a Twilio function with a particular asset, the function will not perceive changes in the content of the asset without a redeployment. The function essentially gets a static snapshot of the content at the time of deployment.

    However, an alternative could be to decouple the function from the actual data. Instead of having the function directly read from the file, you can place the file behind a URL and make the function fetch the content from that URL. This way, when you update the file, the function would always fetch the latest content when it's called, without needing a redeployment.

    Regarding your second query, remember that protected assets in Twilio Functions are accessed in a different way. They are not directly accessible via the getAssets() and instead you need their publicly facing urls:

    getAssets() only returns private Assets. Public and protected assets can be accessed via their publicly facing urls without the need for calling getAssets(). Refer to the visibility guide for more context!

    Source