Creating AWS Lambda using python 3.6

Upasana | August 31, 2019 | 3 min read | 147 views | AWS Tutorials


In this article we will setup a simple AWS lambda function using python 3.6 language and invoke it from aws cli.

Perquisites

  1. AWS account setup (Create a free account at https://aws.amazon.com)

  2. Familiarity with Python 3.6

  3. No IDE required as we can directly work on AWS Lambda console for writing python code

Create a lambda function

Goto lambda section of AWS services and click on create function button.

We need to select the following:

  1. Author from scratch

  2. Function name - python-lambda-demo

  3. Runtime as Python 3.6

As shown in the screenshot below:

create lambda function

Python code for our lambda function

We can create a simple python function that prints the character frequency from a given input string.

AWS lambda definition
import json

def lambda_handler(event, context):
    text = 'Input {}'.format(event['text'])
    return {
        'statusCode': 200,
        'input': text,
        'frequency': get_char_frequency(text)
    }


def get_char_frequency(text):
    freq = {}
    ls = [i for i in list(text.lower()) if i !=' ']
    for i in ls:
        if i in freq:
            freq[i] += 1
        else:
            freq[i] = 1
    return freq

We need to put the above code in inline editor of lambda function and hit the save button. Now we are ready to test the lambda function.

Configure Input

AWS Lambda console provides testing functionality, we need to configure the test events for that.

python configure input lambda

After that save the test event and click on Test button. We will see the results on the screen itself.

lambda python demo result

Invoking the lambda using aws cli

We can use AWS CLI to invoke the lambda provided you have aws cli setup on your system.

You will have to follow Amazon AWS official documentation to setup AWS CLI on your target platform:

Invoking the lambda function
$ aws lambda invoke --function-name python-lambda-demo --payload '{"text": "my name is carvia"}' output.txt
Check the output
$ cat output.txt
Response from lambda
{
 "statusCode": 200,
 "input": "Input my name is carvia",
 "frequency": {"i": 3, "n": 2, "p": 1, "u": 1, "t": 1, "m": 2, "y": 1, "a": 3, "e": 1, "s": 1, "c": 1, "r": 1, "v": 1}
 }

Other invocation mechanisms

We can use other mechanisms to invoke the lambda function, for example we can setup API gateway or SQS trigger that will invoke the lambda function.

We will cover API Gateway trigger in this article.

Exposing Lambda through API Gateway

From the left hand side on lambda configuration page, we need to select API Gateway as the Trigger, as shown in below image:

lambda api gateway 1

Now we need to configure the necessary inputs for API Gateway, specifically:

  1. Creating a new API or using existing API

  2. Security: Open/Open with Key or IAM

  3. API name: this will appear in API Gateway

lambda api gateway 2

After that we are all set. We will get the final invocation URL after we save the changes. For demo purpose, we will keep this API open and hit it from the Postman.

lambda postman

That’s all for this article. Stay tuned.


Top articles in this category:
  1. AWS Lambda Interview Questions for Developers
  2. Introduction to Python 3.6 & Jupyter Notebook
  3. Python coding challenges for interviews
  4. Flask Interview Questions
  5. Creating custom Keras callbacks in python
  6. Top 100 interview questions on Data Science & Machine Learning
  7. Connect to Postgresql with Python 3.x and get Pandas Dataframe

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.