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
-
AWS account setup (Create a free account at https://aws.amazon.com)
-
Familiarity with Python 3.6
-
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:
-
Author from scratch
-
Function name -
python-lambda-demo
-
Runtime as Python 3.6
As shown in the screenshot below:
Python code for our lambda function
We can create a simple python function that prints the character frequency from a given input string.
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.
After that save the test event and click on Test button. We will see the results on the screen itself.
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:
$ aws lambda invoke --function-name python-lambda-demo --payload '{"text": "my name is carvia"}' output.txt
$ cat output.txt
{
"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:
Now we need to configure the necessary inputs for API Gateway, specifically:
-
Creating a new API or using existing API
-
Security: Open/Open with Key or IAM
-
API name: this will appear in API Gateway
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.
That’s all for this article. Stay tuned.
Top articles in this category:
- AWS Lambda Interview Questions for Developers
- Introduction to Python 3.6 & Jupyter Notebook
- Python coding challenges for interviews
- Flask Interview Questions
- Creating custom Keras callbacks in python
- Top 100 interview questions on Data Science & Machine Learning
- Connect to Postgresql with Python 3.x and get Pandas Dataframe