Thursday, September 5, 2019

AWS Lambda function for Fibonacci recursive function

Here I will show you how to run a simple recursive function in AWS lambda.

1) Login to AWS console.
2) Select Lambda from services menu.
3) Select "Create Function" button on the page.
4) Select "Author from scratch" option and enter the function details as shown in the screenshot.
 Provide a name to your function, select Python 3.7 in the runtime and choose the option to "Create a new role with basic Lambda permissions".

5) This will create the function and bring up the following screen with the ARN displaying on top right.

6) Paste the following code in the code editor. This code is to calculate a fibincacci for a passed in number.

import json

def lambda_handler(event, context):
    return getFibonacci(event.get("item"));
#return result;
def getFibonacci(n):
    if(n == 0):
        return 0
        if(n == 1):
            return n 
            if(n > 1):
                return getFibonacci(n-1) + getFibonacci(n-2)

Code is also available at

7) Testing
Configure a test event by selecting the test event from the dropdown as show below.

Create the test event as show

8) Select this new test event and test your function. You should see the following in your execution result log.

Hope this helps you with creating a simple AWS lambda function.

No comments:

Post a Comment