Python function in AWS Lambda

1

I need to run the following function below on AWS Lambda :

def subset_sum(numbers, target, partial=[]):
    s = sum(partial)

# check if the partial sum is equals to target
if s == target:
    print("sum(%s)=%s" % (partial, target))
if s >= target:
    return  # if we reach the number why bother to continue

for i in range(len(numbers)):
    n = numbers[i]
    remaining = numbers[i + 1:]
    subset_sum(remaining, target, partial + [n])

if __name__ == "__main__":
    subset_sum([1,2,3], 5)

However this appears and I can not resolve:

{
  "errorMessage": "'>=' not supported between instances of 'int' and 'LambdaContext'",
  "errorType": "TypeError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      7,
      "subset_sum",
      "if s >= target:"
    ]
  ]
}

The original function has more values that take a long time because of this I'm trying to run in Lambda . The function returns the combination of numbers that result in the sum of the set value (in case 5)

On the computer using py subset_sum.py it works normally and returns 2 and 3 (which combined equals 5).

UPDATE

I made the modification according to Tom's response. It now reports another error.

def lambda_handler(event, context):

    s = sum(event['partial'])

    if s == 5:
        print("sum(%s)=%s" % (event['partial'], 5))
        exit()
    if s >= 5:
        return

    for i in range(len(event['numbers'])):
        n = event['numbers'][i]
        remaining = event['numbers'][i + 1:]
        dict = {'numbers' : remaining, 'partial' : event['partial'] + [n]}
        lambda_handler(dict)

if __name__ == "__main__":
    dict = {'numbers' : [1,2,3], 'partial' : []}
    lambda_handler(dict)

Lambda output

{
  "errorMessage": "'partial'",
  "errorType": "KeyError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      5,
      "lambda_handler",
      "s = sum(event['partial'])"
    ]
  ]
}

Output print (event) at the beginning of the handler

START RequestId: f3a45f36-b1fc-11e7-8f6d-d5a3a428a4fd Version: $LATEST
{'key3': 'value3', 'key2': 'value2', 'key1': 'value1'}
'partial': KeyError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 7, in lambda_handler
    s = sum(event['partial'])
KeyError: 'partial'
END RequestId: f3a45f36-b1fc-11e7-8f6d-d5a3a428a4fd
    
asked by anonymous 15.10.2017 / 05:58

1 answer

0

If you have defined the subset_sum function as the handler of your Lambda Function, the parameters that are passed are different from your subset_sum ([1,2,3], 5) .

The invoke of the handler receives the parameters:

  • event - Event responsible for running Lambda Function (API Gateway, CloudWatch and etc);
  • context - runtime info.

The error occurs by the comparison you are making between an int and a LambdaContext object.

Parameters must be accessed through the event, eg:

def my_handler(event, context):
    partial = event['partial']
    target = event['target']

    print(partial)
    print(target)

Documentation: link

EDIT

To set test event values through the console:

Lambda - > (Select your Lambda Function) - > Select a test event - > Configure test events

    
15.10.2017 / 17:44