You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python-lambda is a toolset for developing and deploying *serverless* Python code in AWS Lambda.
15
19
@@ -18,10 +22,6 @@ Important
18
22
This is a FORK of the original Python-lambda package by Nick Ficano.
19
23
It will NOT be updated regularly and is frozen per our projects needs.
20
24
21
-
A call for contributors
22
-
=======================
23
-
With python-lambda and `pytube <https://github.com/nficano/pytube/>`_ both continuing to gain momentum, I'm calling for contributors to help build out new features, review pull requests, fix bugs, and maintain overall code quality. If you're interested, please email me at nficano[at]gmail.com.
24
-
25
25
Description
26
26
===========
27
27
@@ -34,125 +34,55 @@ The *Python-Lambda* library takes away the guess work of developing your Python-
34
34
Requirements
35
35
============
36
36
37
-
* Python 2.7 & 3.6 (At the time of writing this, AWS Lambda only supports Python 2.7/3.6).
38
-
* Pip (~8.1.1)
39
-
* Virtualenv (~15.0.0)
40
-
* Virtualenvwrapper (~4.7.1)
37
+
* Python 3.6
38
+
* Pip (Any should work)
39
+
* Virtualenv (>=15.0.0)
40
+
* Virtualenvwrapper (>=4.7.1)
41
41
42
42
Getting Started
43
43
===============
44
44
45
-
First, you must create an IAM Role on your AWS account called `lambda_basic_execution` with the `LambdaBasicExecution` policy attached.
46
-
47
-
On your computer, create a new virtualenv and project folder.
48
-
49
-
.. code:: bash
50
-
51
-
$ mkvirtualenv pylambda
52
-
(pylambda) $ mkdir pylambda
53
-
54
-
Next, download *Python-Lambda* using pip via pypi.
55
-
56
-
.. code:: bash
57
-
58
-
(pylambda) $ pip install python-lambda
59
-
60
-
From your ``pylambda`` directory, run the following to bootstrap your project.
61
-
62
-
.. code:: bash
63
-
64
-
(pylambda) $ lambda init
65
-
66
-
This will create the following files: ``event.json``, ``__init__.py``, ``service.py``, and ``config.yaml``.
67
-
68
-
Let's begin by opening ``config.yaml`` in the text editor of your choice. For the purpose of this tutorial, the only required information is ``aws_access_key_id`` and ``aws_secret_access_key``. You can find these by logging into the AWS management console.
69
-
70
-
Next let's open ``service.py``, in here you'll find the following function:
45
+
Using this library is intended to be as straightforward as possible. Code for a very simple lambda used in the tests is reproduced below.
71
46
72
47
.. code:: python
73
48
74
-
defhandler(event, context):
75
-
# Your code goes here!
76
-
e = event.get('e')
77
-
pi = event.get('pi')
78
-
return e + pi
79
-
80
-
81
-
This is the handler function; this is the function AWS Lambda will invoke in response to an event. You will notice that in the sample code ``e`` and ``pi`` are values in a ``dict``. AWS Lambda uses the ``event`` parameter to pass in event data to the handler.
82
-
83
-
So if, for example, your function is responding to an http request, ``event`` will be the ``POST`` JSON data and if your function returns something, the contents will be in your http response payload.
49
+
config = {
50
+
'function_name': 'my_test_function',
51
+
'function_module': 'service',
52
+
'function_handler': 'handler',
53
+
'handler': 'service.handler',
54
+
'region': 'us-east-1',
55
+
'runtime': 'python3.6',
56
+
'role': 'helloworld',
57
+
'description': 'Test lambda'
58
+
}
84
59
85
-
Next let's open the ``event.json`` file:
86
60
87
-
.. code:: json
61
+
defhandler(event, context):
62
+
return'Hello! My input event is %s'% event
88
63
89
-
{
90
-
"pi": 3.14,
91
-
"e": 2.718
92
-
}
64
+
This code illustrates the two things required to create a lambda. The first is ``config``, which specifies metadata for AWS. One important thing to note in here is the ``role`` field. This must be a IAM role with Lambda permissions - the one in this example is ours. The second is the ``handler`` function. This is the actual code that is executed.
93
65
94
-
Here you'll find the values of ``e`` and ``pi`` that are being referenced in the sample code.
66
+
Given this code in ``example_function.py`` you would deploy this function like so:
95
67
96
-
If you now try and run:
97
-
98
-
.. code:: bash
99
-
100
-
(pylambda) $ lambda invoke -v
101
-
102
-
You will get:
103
-
104
-
.. code:: bash
105
-
106
-
# 5.858
107
-
108
-
# execution time: 0.00000310s
109
-
# function execution timeout: 15s
110
-
111
-
As you probably put together, the ``lambda invoke`` command grabs the values stored in the ``event.json`` file and passes them to your function.
112
-
113
-
The ``event.json`` file should help you develop your Lambda service locally. You can specify an alternate ``event.json`` file by passing the ``--event-file=<filename>.json`` argument to ``lambda invoke``.
114
-
115
-
When you're ready to deploy your code to Lambda simply run:
116
-
117
-
.. code:: bash
118
-
119
-
(pylambda) $ lambda deploy
120
-
121
-
The deploy script will evaluate your virtualenv and identify your project dependencies. It will package these up along with your handler function to a zip file that it then uploads to AWS Lambda.
122
-
123
-
You can now log into the `AWS Lambda management console <https://console.aws.amazon.com/lambda/>`_ to verify the code deployed successfully.
124
-
125
-
Wiring to an API endpoint
126
-
=========================
127
-
128
-
If you're looking to develop a simple microservice you can easily wire your function up to an http endpoint.
68
+
.. code:: python
129
69
130
-
Begin by navigating to your `AWS Lambda management console <https://console.aws.amazon.com/lambda/>`_ and clicking on your function. Click the API Endpoints tab and click "Add API endpoint".
And that's it! You've deployed a simple lambda function. You can navigate to the AWS console to create a test event to trigger it or you can invoke it directly using Boto3.
133
79
134
-
Next change Method to ``POST`` and Security to "Open" and click submit (NOTE: you should secure this for use in production, open security is used for demo purposes).
80
+
Advanced Usage
81
+
==============
135
82
136
-
At last you need to change the return value of the function to comply with the standard defined for the API Gateway endpoint, the function should now look like this:
83
+
Many of the options specified in the above code block when it came to actually deploying the function are not used. These become more useful as you want to make more complicated lambda functions. The ideal way to incorporate dependencies into lambda functions is by providing a ``requirements.txt`` file. We rely on ``pip`` to install these packages and have found it to be very reliable. While it is also possible to specify local modules as well through ``package_objects``, doing so is not recommended because those modules must be specified at the top level of the repository in order to work out of the box. There is a comment on this topic in ``example_function_package.py`` with code on how to handle it.
137
84
138
-
.. code:: python
85
+
Tests
86
+
========
139
87
140
-
defhandler(event, context):
141
-
# Your code goes here!
142
-
e = event.get('e')
143
-
pi = event.get('pi')
144
-
return {
145
-
"statusCode": 200,
146
-
"headers": { "Content-Type": "application/json"},
147
-
"body": e + pi
148
-
}
149
-
150
-
Now try and run:
151
-
152
-
.. code:: bash
153
-
154
-
$ curl --header "Content-Type:application/json" \
155
-
--request POST \
156
-
--data '{"pi": 3.14, "e": 2.718}' \
157
-
https://<API endpoint URL>
158
-
# 5.8580000000000005
88
+
Tests can be found in the ``test_aws_lambda.py``. Using the tests as a guide to develop your lambdas is probably a good idea. You can also see how to invoke the lambdas directly from Python (and interpret the response).
0 commit comments