Skip to content

Commit b8c5576

Browse files
committed
Add tutorial
1 parent 4879129 commit b8c5576

File tree

2 files changed

+123
-13
lines changed

2 files changed

+123
-13
lines changed

README.rst

+122-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,128 @@ awslambda
33

44
*A tool for deploying Python projects to AWS Lambda.*
55

6+
Getting started
7+
---------------
8+
Authentication is left to *boto3* so you can set it up just like the `regular
9+
AWS CLI <http://docs.aws.amazon.com/lambda/latest/dg/setup.html>`_. You need an
10+
`S3 bucket
11+
<http://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html>`_ for
12+
temporary storage. For a quick tutorial on execution roles, see the `official
13+
docs
14+
<http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-create-iam-role.html>`_
15+
(of course you need one that can execute lambdas).
16+
17+
In a new folder, create *mymodule.py*:
18+
19+
.. code:: python
20+
21+
def hello(*args):
22+
return "Hello, world!"
23+
24+
25+
Then deploy the function (fill in your execution role resource name from the AWS
26+
console):
27+
28+
::
29+
30+
awslambda . mybucket --create hello mymodule.hello arn:aws:iam::xxxxxxxxxxxx:role/myrole
31+
32+
33+
From now on, if you make changes to the function, just run:
34+
35+
::
36+
37+
awslambda . mybucket --update hello
38+
39+
40+
You can use as many options as you like (shown here with shorthand names):
41+
42+
::
43+
44+
awslambda . mybucket -u hello -u myotherlambda --delete myoldlambda
45+
46+
47+
Or specify your functions in a YAML file (lets call it *sync.yaml*):
48+
49+
.. code:: yaml
50+
51+
hello:
52+
handler: mymodule.hello
53+
role: arn:aws:iam::xxxxxxxxxxxx:role/myrole
54+
# myotherlambda:
55+
# handler: myothermodule.myotherhandler
56+
# role: arn:aws:iam::xxxxxxxxxxxx:role/myrole
57+
58+
Syncing from a file, *awslambda* will update existing functions and create the
59+
others automatically.
60+
61+
::
62+
63+
awslambda . mybucket --sync sync.yaml
64+
65+
66+
To add dependencies, use your `pip *requirements.txt*
67+
<https://pip.readthedocs.io/en/stable/user_guide/#requirements-files>`_:
68+
69+
::
70+
71+
awslambda . mybucket -s sync.yaml --requirements requirements.txt
72+
73+
74+
A template greeting page
75+
........................
76+
77+
Let's use the features introduced above to create a greeting page. We will use
78+
the `Jinja2<http://jinja.pocoo.org>`_ templating engine.
79+
Edit *mymodule.py*
80+
81+
.. code:: python
82+
83+
from jinja2 import Template
84+
85+
template = Template('''
86+
<html>
87+
<body>
88+
<h1>Hello, {{ parameters.name }}!</h1>
89+
<p>{{ parameters.message }}</p>
90+
</body>
91+
</html>
92+
''')
93+
94+
95+
def hello(event, context):
96+
return {
97+
'statusCode': 200,
98+
'headers': {'Content-Type': 'text/HTML'},
99+
'body': template.render(parameters=event['queryStringParameters'])}
100+
101+
102+
And create your simple *requirements.txt*
103+
104+
::
105+
106+
Jinja2
107+
108+
109+
Deploy
110+
111+
::
112+
113+
awslambda . mybucket -s sync.yaml -r requirements.txt
114+
115+
116+
Open the function in your AWS console. Go to *Triggers* and add an
117+
*API Gateway* trigger. Set security to *Open* for now. Open the URL of the
118+
created trigger in your browser. You should see "Hello, !". To customize the
119+
page append e.g.
120+
121+
::
122+
123+
?name=Commander Shepard&message=You've received a new message at your private terminal.
124+
125+
126+
to the URL.
127+
6128

7129
Usage
8130
-----
@@ -40,15 +162,3 @@ Usage
40162
-s, --sync FILENAME Keep lambdas defined in YAML file in sync
41163
with deployed lambdas.
42164
--help Show this message and exit.
43-
44-
45-
Getting started
46-
---------------
47-
Authentication is left to *boto3* so you can set it up just like the `regular
48-
AWS CLI <http://docs.aws.amazon.com/lambda/latest/dg/setup.html>`_. You need an
49-
`S3 bucket
50-
<http://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html>`_ for
51-
temporary storage. For a quick tutorial on execution roles, see the `official
52-
docs
53-
<http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-create-iam-role.html>`_
54-
.

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name='awslambda',
12-
version='1.0.5',
12+
version='1.0.6',
1313
description='A tool for deploying Python projects to AWS Lambda.',
1414
long_description=long_description,
1515
keywords='aws lambda',

0 commit comments

Comments
 (0)