https://prefect.io logo
h

Hugo Kitano

06/14/2021, 6:46 PM
Hi, this is a general question, but just looking some advice. I’m trying to set off an already-registered run in a python lambda function that is triggered by a s3 file upload (bucket and key of the file are parameters). Whats the most lightweight way to do this? Any example code would be great.
k

Kevin Kho

06/14/2021, 6:49 PM
Hey @Hugo Kitano, I think this assumes you have permissions to hit it, but it would be a matter of hitting the
create_flow_run
endpoint of the GraphQL API
Copy code
import requests
import json
query = """mutation {
  create_flow_run(input: { 
  flow_id: "5ac80adc-442a-4e53-bc01-d9d65bffd6aa"}) {
    id
  }
}"""
url = '<https://api.prefect.io>'
r = <http://requests.post|requests.post>(url, json={'query': query}, headers={"authorization": "Bearer insert_token"})
print(r.status_code)
print(r.text)
You may need to add the user agent to the headers like this:
Copy code
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"}
Some users find this fails due to security blocking the request. If that happens. the
Prefect Client
has a
create_flow_run
method that you can use. I gave the script above first cuz there’s no Prefect dependency
h

Hugo Kitano

06/14/2021, 6:53 PM
Thanks Kevin, I have a Prefect layer available for this lambda function so I may just use the create_flow_run method. I think it would be a little easier to show and explain to my colleagues. Can you send me a code snippet for that? I really appreciate it
k

Kevin Kho

06/14/2021, 6:55 PM
If authenticated, I think it’s
Copy code
from prefect.client.client import Client
Client=Client()
Client.create_flow_run(flow_id=id, run_name="test")
👍 1
h

Hugo Kitano

06/14/2021, 6:55 PM
How do I make sure I’m authenticated?
k

Kevin Kho

06/14/2021, 7:01 PM
The Client will throw an error if it can’t retrieve a configured API Key. You can also pass it the server and API key though when you instantiate it doc .
If you have the token and configured with
prefect auth login
on the CLI, then the Client would be able to fetch it
h

Hugo Kitano

06/14/2021, 7:59 PM
Thanks, Kevin. What would the “api_server” parameter of the Client() method be? Would it need to be specified?
k

Kevin Kho

06/14/2021, 8:02 PM
Are you on Prefect Server or Cloud? Cuz this is the Server channel so I’m assuming Server?
h

Hugo Kitano

06/14/2021, 8:11 PM
Oh! Oops, wrong channel. I want to be on Cloud
k

Kevin Kho

06/14/2021, 8:12 PM
Oh! Then the default should work for you
h

Hugo Kitano

06/14/2021, 8:18 PM
Is there a difference between an “api key” and an “api token”?
k

Kevin Kho

06/14/2021, 8:19 PM
Let me get someone who can answer that better than me
Actually, still not sure about which of the two cases is which
m

Mariia Kerimova

06/14/2021, 8:36 PM
Hello Hugo! Prefect is transitioning from api tokens to service account api keys. You can provide environment variable 
PREFECT__CLOUD__AUTH_TOKEN
 for authenticating your client.
h

Hugo Kitano

06/14/2021, 8:37 PM
Hi Mariia, thanks for the advice. What is the difference between a User API key and a Service Account API key?
m

Mariia Kerimova

06/14/2021, 8:56 PM
User API keys represent your identity, and they are cross tenant with user's personal permissions. You would use those keys locally, but for CI or agents you would use service account keys, which have only permissions restricted to the tenant where this service account was created.
h

Hugo Kitano

06/14/2021, 8:59 PM
I see. So when I set
PREFECT_CLOUD_AUTH_TOKEN
, I should use the ID token returned after I do
prefect auth list-tokens
, right?
m

Mariia Kerimova

06/14/2021, 9:12 PM
I would recommend to create a service account key. You can do it through UI: click Team -> Service Accounts -> Add service account. Or use interactive API:
Copy code
mutation {
  create_api_key(input: { user_id: <user_id>, name: "my-api-key" }) {
    token
  }
}
🙌 1
As you can see in this PR, we are going to deprecate
prefect auth list-tokens
2 Views