0

I am trying to create and run a lambda function from within Redshift.

I have scaled the lambda_handler back to something very simple

def lambda_handler(event, context):
    print("Received event:", event)

    value = 42
    print("Returning value:", value, "type:", type(value))

    result = [[value]]
    print("JSON result to return:", result)

    return result

I have also tried setting result as 42 and [42].

My create external function statement is

CREATE OR REPLACE EXTERNAL FUNCTION test_fn()
RETURNS INT
VOLATILE
LAMBDA 'arn:aws:.........'
IAM_ROLE 'arn:aws:iam::........';

However I keep getting the error:

SQL Error [XX000]: ERROR: Invalid External Function Response
  Detail: 
  -----------------------------------------------
  error:  Invalid External Function Response
  code:      8001
  context:   Cannot parse External Function response
  query:     0
  location:  exfunc_data.cpp:374
  process:   padbmaster [pid=1073939252]
  -----------------------------------------------

I am using a cluster of 2 ra3.large nodes which is patched to version 191.

I am expecting Redshift to return the value 42 when calling SELECT test_fn();

0

2 Answers 2

2

Why Redshift can’t parse your Lambda response — and the minimal fix

Redshift Lambda UDFs must return a JSON string whose top level has a field named results. results must be an array with one element per input row Redshift sends in event["arguments"]. If you return a bare value, a Python list, or anything that isn’t a JSON string with {"results": [...]}, Redshift throws “Cannot parse External Function response”.

Minimal working Python handler (for a no‑arg UDF)

import json

def lambda_handler(event, context):
    # Redshift batches rows in event["arguments"]
    rows = event["arguments"]          # e.g., [ [] ] for SELECT my_fn();
    results = [42] * len(rows)         # one output per input row
    return json.dumps({"results": results})

Checklist

  • The handler returns json.dumps({...}), not a raw Python object.

  • Top‑level key is exactly results.

  • len(return_payload["results"]) == len(event["arguments"]).

  • Your SQL UDF’s return type matches the Python values you put in results.

If you add parameters later, each “row” in event["arguments"] will be an array of those values; compute one scalar per row and place them in results.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I was missing this, and has resolved my issue.
if this answer reolved your problem then you could mark it as accepted, and few minutes later you can upvote it.
0

Error [XX000]: This error is always about a mismatch between what Lambda returns and what Redshift expects. Once you fix the return type, the error goes away.

Redshift external functions have strict requirements on return types:

  • The Lambda return type must match the Redshift declared SQL type.

  • Scalars (INT, VARCHAR, etc.) must be returned as plain values, not as lists, arrays, or dicts.

So, You need to do the following:

  1. value is a plain integer, matching the RETURNS INT type in Redshift.

  2. No brackets, no lists, no dicts — just the scalar.

Lambda Handler:

def lambda_handler(event, context):
    print("Received event:", event)
    
    value = 42
    print("Returning value:", value)
    
    return value  # must be scalar to match RETURNS INT

Redshift External Function

CREATE OR REPLACE EXTERNAL FUNCTION test_fn()
RETURNS INT
VOLATILE
LAMBDA 'arn:aws:lambda:...'
IAM_ROLE 'arn:aws:iam::...';

Calling:

SELECT test_fn();

✅ Returns:

 test_fn
---------
 42

Key Takeaways:

  1. Always match Lambda return to Redshift SQL type.

  2. Scalars: return plain values (42, "hello").

  3. Arrays: return JSON arrays if Redshift type is ARRAY<...>.

  4. Structs/Records: return JSON objects matching the declared schema.

  5. print() statements are fine for debugging — they don’t affect the returned value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.