How to get the current flow_run_id when implementing a custom Infrastructure Block

Hi (-:

I am in the middle of implementing our own Infrastructe Block, to run flows on Render.
I am stuck at point where inside the run() function of the Infrastructe block, I somehow need to get the current flow_run_id.

Once I’ll be able to get the current flow_run_id, I could make a REST API call to Render’s backend, telling it to start a new instance with the command:

 python -m prefect.engine {flow_run_id}

To give more context, the Infrastructure Block is:


(please don’t mind the red arrow pointing at the “identifier”, this might be a different question :slight_smile:)

I’ve tried looking at the ECSTask file which implements the same thing we are trying to achieve here, but I couldn’t find any mention of the flow_run_id in the run() or how it’s being fetched.

Any help would be much appreciated!

Hi Yaron!

The way to do this would be passing the flow run ID as an environment variable. That’s how ECSTask and other blocks do it, and it looks like the Render API’s create-service call provides an easy way to do it via the envVars parameter.

When the agent triggers a flow run via an infrastructure block, it calls prepare_for_flow_run on the base Infrastructure class, which in turn adds PREFECT__FLOW_RUN_ID to the block’s env dict.

So, if you pass the block’s environment variables on to the Render service, and then run python -m prefect.engine, Prefect should pick up the flow run ID from the environment and run the flow.

See here for a look at how ECSTask gets the environment variables, and here to see it in AzureContainerInstanceJob.

1 Like

Got it! :slight_smile:
Thanks!