Hi Everyone, I am trying to trigger prefect flow d...
# prefect-community
Hi Everyone, I am trying to trigger prefect flow deployed on cloud whenever file is landed to S3 bucket using AWS Lambda function by referring this documentation from Chris. Lambda function is failing with
"errorMessage": "HTTP Error 404: Not Found"
I am passing API URL
. I understand that this documentation is not latest and I also tried URL
and getting same error. Request your inputs regarding this error in Prefect version 2.0
Hi JV, The ideal way to do this looks a bit different in Prefect 2. Instead of calling the API, consider setting the PREFECT_API_URL and PREFECT_API_KEY environment variables and then calling your Prefect flow function from your Lambda. It will run and record the flow run results in Prefect Cloud. You might find this post helpful, as well as the GitHub repository that goes with it (it doesn't sound like you need the GitHub Actions part of the article, but I think it's still useful if you skip that part). This S3 event flow in particular sounds like it is close to what you need.
If you have more questions about this or anything AWS-related, I suggest posting to the #prefect-aws channel. That help ensure it gets seen ASAP 😄
Hi @Ryan Peden, I am setting those parameters as environment variables and then accessing in the code, lambda is failing with the earlier mentioned error. I have referred the posts which you have shared and I was not able to find resolution. Status code 404 indicates page not available so I wanted to check if the API URL which I am using is correct.
Hi @JV - your URL should be structured like this
Copy code
is that how yours looks?
Hi @Nate no, I have not structured URL in that format, I am just passing
Copy code
can you please let me know if I need to dynamically pass UUID to the URL you have mentioned above and where will I get that?
if you run
prefect config view
locally you can see what your URL should be (provided you've setup a profile, otherwise here are the docs) and that's what you should set as the PREFECT_API_URL in the env vars for your lambda
Hi @Nate, Thanks for this information, its working. Now my API call resulted in
Copy code
"HTTP Error 405: Method Not Allowed"
Do we need to set any permissions in prefect cloud to allow POST requests on cloud API?
can you show how you're making your api call?
Hi @Nate, I am making api call as mentioned in the post, I want to pass s3 file path which is captured as event to the flow
Copy code
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
input_file = "s3://"+bucket+"/"+key

create_mutation = """
mutation($input: create_flow_run_input!){
	create_flow_run(input: $input){
inputs = dict(flow_id=os.getenv("PREFECT__FLOW_ID"))
inputs['file'] = input_file

variables = dict(input=inputs)

data = json.dumps(
	dict(query=create_mutation, variables=json.dumps(variables))

## prepare the request
req = urllib.request.Request(os.getenv("PREFECT__CLOUD__API"), data=data)
req.add_header("Content-Type", "application/json")
	"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())
@JV because that article you linked above is for prefect 1, it shows how to interact with the graphQL API we had in prefect 1. Prefect 2 has a REST API, so you will need to hit the appropriate REST endpoint to trigger flow runs Let me find a discourse link
you can do something like this in prefect 2
Hi @Nate , Its working, Thanks for your inputs.
🦜 1