Hello all, we are currently using lambda to trigger our flows. we are also currently setting up our ...
v
Hello all, we are currently using lambda to trigger our flows. we are also currently setting up our CI for each of these flow projects. we are using the flow id to trigger our flows through lambda. but the problem is everytime we register a new version of the flow the flow id keeps changing. we noticed the flow group id remains the same. is there a way possible that we can always trigger the latest version of our flow without having to worry about changing flow id’s.
a
yes, you’re right, that’s exactly what the flow group ID is for - it’s an ID that remains the same for all versions of the flow:
upvote 1
v
but how do we use this flow group id to trigger our latest flow version.
k
You can also query for the flow id by doing
archived=False
n
Copy code
# kick-off flow run with prefect.Client instance
def trigger_flow_run(params: Dict[str, Any]) -> dict:
    prefect_client = prefect.Client(api_key=PREFECT_AUTH_TOKEN)
    return prefect_client.create_flow_run(
        parameters=params,
        version_group_id=os.getenv('PREFECT_VERSION_GROUP_ID'),
    )
here's a piece of a lambda I'm currently using to trigger flow runs, which does trigger the most recent flow version
upvote 2
v
Thank you
btw this would mean we would have to install prefect along with lambda in order to use the prefect.Client function.
k
If you want to avoid that, you can use the Python requests library like this
v
our current implementation however uses an api request to trigger the flow.
Copy code
## prep the request
    req = urllib.request.Request(os.getenv("PREFECT__CLOUD__API"), data=data)
    req.add_header("Content-Type", "application/json")
    req.add_header(
        "Authorization", "Bearer {}".format(os.getenv("PREFECT__CLOUD__API_KEY"))
    )
    ## send the request and return the response
    resp = urllib.request.urlopen(req)
    return json.loads(resp.read().decode())
k
Yeah you can just use that. The Client just hits the GraphQL API endpoint under the hood.
v
so i need not make any changes and it will automatically trigger the latest flow ?
k
Wait ok I think there was a bunch in this thread. To summarize, you need to use the
version_group_id
instead of the
flow_id
because that is not modified during registration. The
create_flow_run
Mutation in the GraphQL API can take in the
version_group_id
directly. You can do a request like you are doing to this API endpoint.
v
Thanks so only change is to use the
version_group_id
instead of the
flow_id
. I will give it a try.
a
@Vamsi Reddy
Copy code
import requests

query = """
 mutation {
  create_flow_run(input: { version_group_id: "fb919cc4-7f74-4ed7-9f3c-71bdd9be69e8" }) {
    id
  }
}
"""

url = "<https://api.prefect.io>"
response = <http://requests.post|requests.post>(
    url, json={"query": query}, headers={"authorization": "Bearer XXX"}
)
print(response.status_code)
print(response.text)
👍 1
upvote 1