I have an instance running on cloud run which uses an external api endpoint. This api is also serverless so it takes a couple of mins to start. Is there a way to call this api when the cloud run instance starts? I’ve seen an option with triggers (eventarc) but I don’t fully understand how it works and if it helps.
I understand that you’re looking for a way to call an external API when your Cloud Run instance starts. However, as of now, Cloud Run currently does not have a built-in mechanism to trigger external API calls at instance startup.
For Eventarc triggers, it will require your external API to emit events or connect to a compatible Google Cloud service. It might not be the ideal solution for initiating an external API call upon Cloud Run instance startup.
I suggest filing this as a feature request, so that our engineering team can look into it. Note that I won’t be able to provide the date as to when this will be implemented. More information, including specifics and timeline will be available soon. Please keep an eye on the release notes for any updates or new features related to Cloud Run.
Starting a new instance really is just starting a new instance of your container, so I’d think you can just call the API from your code. Your dockerfile contains a command to run at the end of it; that’s where you’d want to put this API call.
Out of curiosity: Is the other API an AI model?
I don’t think Eventarc is helpful here - it’s just another way for services to communicate with each other. Unless you were thinking of using it as some way to keep the other API “awake”?
What would be most helpful in this scenario is reducing the startup latency of the other API, though I understand that’s not always possible. A few other things that might help:
Set min instances on your Cloud Run service so you don’t get as many 0->1 cold starts
Experiment with Cloud Run custom health checks - if you delay a bit the time before you mark the instance as ready, you might be able to hide some of the API request latency from actual user requests. But if you increase it too high, you might get some timeouts; Cloud Run’s autoscaling works best when containers start under around ~30s or so (even lower is better).
Thank you @knet . And yes, the api is an AI model, it’s a serverless endpoint on a different platform (databricks) so it takes about a minute o so to start in cold. That’s why I was thinking about this option of triggering a call while the app is starting. Where in the docker file would you put this call?
Putting it in the dockerfile is an interesting idea, though I don’t know how much benefit it’s going to give you over putting it in the code in your main() method. It depends how fast your container starts - if you’re using something like python or node without a heavy-weight framework, you probably won’t get a lot of benefit from putting this in the dockerfile directly.