Larry Profx
05/13/2024, 8:29 PMNate
05/13/2024, 8:30 PMLarry Profx
05/13/2024, 8:32 PMNate
05/13/2024, 8:33 PMexport PREFECT_API_KEY
and PREFECT_API_URL
wherever you're trying interact with your workspaceLarry Profx
05/13/2024, 8:34 PMLarry Profx
05/13/2024, 8:53 PMMarvin
05/13/2024, 8:54 PMprefect cloud login
from your CLI.
- Choose to authenticate by pasting the API key when prompted.
Note: Ensure that you keep your API key secure and do not share it unnecessarily. For more details and instructions, you can visit the Manage Prefect Cloud API Keys documentation.
Here's how you would create an async function to authenticate with an API key:
python
import httpx
from prefect import get_client
async def authenticate_with_api_key(api_key: str):
# Assuming you have the API key, you can either set it in your client config
# or use it directly when making requests. Prefect Cloud expects
# the API key as a bearer token in the Authorization header.
headers = {
'Authorization': f'Bearer {api_key}'
}
# Use httpx AsyncClient or any other async HTTP client to make your requests
async with httpx.AsyncClient(headers=headers) as client:
# Example request to authenticate and retrieve your Prefect account details
response = await client.get('<https://api.prefect.io/graphql>')
# Handle your response
if response.status_code == 200:
print("Authenticated successfully.")
return response.json()
else:
print("Failed to authenticate.")
return None
# Ensure you replace 'YOUR_API_KEY' with your actual API key
api_key = 'YOUR_API_KEY'
client = get_client()
client.set_api_key(api_key) # This sets the API key for your Prefect Client instance
# Now you can use 'client' within your flows and tasks.
Please replace YOUR_API_KEY
with your actual API key. This function uses the httpx
library to make asynchronous HTTP requests, and sets the API key in the headers for authentication. The example shown is a basic example for setting up an async function; depending on the Prefect Cloud API you're interacting with, you may need to adjust the endpoint and parameters accordingly.Nate
05/13/2024, 9:14 PMLarry Profx
05/13/2024, 9:23 PMNate
05/13/2024, 9:40 PMLarry Profx
05/13/2024, 10:08 PMNate
05/13/2024, 10:09 PMLarry Profx
05/13/2024, 10:11 PMNate
05/13/2024, 10:13 PMPREFECT_API_URL
corresponds to a specific workspace, which you must have if you're using cloud, and API_KEY
corresponds to an actor authenticated in that workspace (which could be a user or a service account)
here's an example thats for GHA, but it should be roughly the same for jenkins
• set the env vars
• use the CLI as you wish to create the deploymentLarry Profx
05/13/2024, 10:20 PMNate
05/13/2024, 10:25 PMwe don't have workspacesyou do have a workspace if you're using prefect cloud. you may only have a personal workspace, but that is still a workspace. your
PREFECT_API_URL
will look something like this
PREFECT_API_URL='<https://api.prefect.cloud/api/accounts/xxx/workspaces/xxx>'
if prefect cloud login -k <>
isnt working then there may be a problem with your key or you may be using a version of prefect with a bug in that CLI commandNate
05/13/2024, 10:27 PMprefect cloud login -k
should not even be necessary in CI, just setting the two env vars I mentioned should be all you need to use the CLI against your workspaceLarry Profx
05/13/2024, 10:32 PMNate
05/13/2024, 10:34 PMIf there's a way to pass the api_url and apikey through the prefect.yaml, that would be greatthere is not,
prefect.yaml
is for defining deployments
Ours is just https://prefect2-server.${ENV}.innovation.io/apiare you sure you use prefect cloud? it seems like you're hosting open source prefect here
Larry Profx
05/13/2024, 10:37 PMNate
05/13/2024, 10:37 PMthere is no auth implemented for oss prefect server, api keys are for authenticating with prefect cloud
Larry Profx
05/13/2024, 10:39 PMNate
05/13/2024, 10:40 PMLarry Profx
05/13/2024, 10:47 PMNate
05/13/2024, 10:50 PM