Abhishek Mitra
12/27/2023, 7:39 AMNate
12/27/2023, 2:11 PMAbhishek Mitra
12/28/2023, 5:36 AMfrom prefect import task, flow,get_run_logger
import json
import botocore.config
cfg = botocore.config.Config(retries={'max_attempts': 0}, read_timeout=900, connect_timeout=900,tcp_keepalive=True)
@task(log_prints=True,timeout_seconds=3000)
def lambda_call(aws_credentials,func_name,payload_bytes,log_dict):
payload = { "body" : json.dumps(payload_bytes)}
payload_byte = bytes(json.dumps(payload), encoding='utf8')
lambdas = aws_credentials.get_boto3_session().client("lambda",config=cfg)
response = lambdas.invoke(
FunctionName=func_name,
InvocationType="RequestResponse",
Payload=payload_byte
)
lambda_payload = response['Payload']
lambda_text = lambda_payload.read()
my_json = lambda_text.decode('utf8').replace("'", '"')
data = json.loads(my_json)
if data['statusCode'] == 200:
return "Success"
else:
err = json.loads(data['body'])
raise Exception(err['error_message'])
@flow()
def main_flow():
try:
lambda_call(aws_creds, lambda_func_name, payload, log_dict)
except Exception:
raise
if __name__=='__main__':
main_flow()
Abhishek Mitra
12/28/2023, 5:39 AMNate
12/28/2023, 4:10 PMDominic Tarro
12/28/2023, 5:53 PMAbhishek Mitra
01/02/2024, 6:58 AMcfg = botocore.config.Config(retries={'max_attempts': 0}, read_timeout=900, connect_timeout=900,tcp_keepalive=True)
but it still times out, The lambda finishes as I mentioned but the response doesn't reach prefect.
I might have to go for the 2nd option.Abhishek Mitra
01/02/2024, 6:59 AMDominic Tarro
01/02/2024, 7:11 PMtcp_keepalive=False
does? I don’t know if this is what’s going on, but there may be a break in communication between the Lambda and client and the Keep-Alive is purging the connection without the client knowing, causing it to wait until the timeout. Worth a try.Nate
01/02/2024, 7:59 PMAbhishek Mitra
01/03/2024, 12:32 PMAbhishek Mitra
01/03/2024, 12:34 PMNate
01/03/2024, 7:26 PMprefect-aws
v0.4.7 has been released, here are the docs for the new `LambdaFunction` block
the new block-type will be made available in Prefect Cloud (auto registered your workspace) next time we push out changes (which happens rather often)
if you want it before then, you're free to prefect block resigter -m prefect_aws
after installing the new library
thanks again @Dominic Tarro for the contribution 💙 - let us know if anything looks offNate
01/04/2024, 11:04 PMNate
01/04/2024, 11:05 PMDominic Tarro
01/04/2024, 11:28 PMAbhishek Mitra
01/05/2024, 3:52 AMtcp_keepalive=False
Dominic Tarro
01/05/2024, 5:06 PMLambdaFunction
block
2. Try without passing a boto config
3. Try updating boto version
If those don’t work there’s not much else I can do from here