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
Copy file name to clipboardExpand all lines: content/posts/170429-python-3-6-aws-lambda.markdown
+41-30Lines changed: 41 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,10 +15,9 @@ comes in to API Gateway or a new file is uploaded to
15
15
[AWS S3](https://aws.amazon.com/s3/) then AWS Lambda can execute a function
16
16
to respond to that API call or manipulate the file on S3.
17
17
18
-
AWS Lambda is not related in any way to Python's `lambda` syntax. The
19
-
`lambda` keyword in Python is used to create anonymous functions within the
20
-
programming language and AWS Lambda's name just happens to collide with
21
-
the existing Python feature.
18
+
AWS Lambdas are not related to the Python languages' `lambda` expressions.
19
+
The AWS Lambda name just happens to collide with the the `lambda` keyword
20
+
name in Python that is used to create anonymous functions.
22
21
23
22
Let's learn how to quickly write and run a Lambda function to execute
24
23
basic Python 3.6 code which uses environment variables as input.
@@ -29,9 +28,14 @@ changed so that you can build much more complicated Python programs.
29
28
## Our Tools
30
29
No local [development environment](/development-environments.html) tools
31
30
are required for this tutorial, other than a web browser. All the work will
32
-
happen on AWS via their Console. All of these steps can also be completed
33
-
from the command line via the [boto3](https://boto3.readthedocs.io/en/latest/)
34
-
library, but we won't cover that in this post.
31
+
happen on AWS via their Console.
32
+
33
+
These steps can also be completed from the command line via the
34
+
[boto3](https://boto3.readthedocs.io/en/latest/) library, but we won't
35
+
cover that in this post.
36
+
37
+
If using Python 2 is still your jam rather than Python 3, take a look at
38
+
[this other post which shows how to execute Python 2.7 code on AWS Lambda](/blog/aws-lambda-python-2-7.html).
35
39
36
40
Sign up for a new [Amazon Web Services account](https://aws.amazon.com/)
37
41
(which provides a generous free tier), or use your existing AWS account.
@@ -149,7 +153,7 @@ required function configuration items.
149
153
150
154
Keep the default handler set to `lambda_function.lambda_handler`. Select
151
155
"Create a new Role from template(s)" from the drop-down then for the
152
-
"Role name" field enter "dynamodb_permissions". Under "Policy templates"
156
+
"Role name" field enter "dynamodb_access". Under "Policy templates"
153
157
select the "Simple Microservice permissions".
154
158
155
159
The "Simple Microservice permissions" allows our Lambda to access
@@ -159,46 +163,53 @@ temporary storage for Lambda functions.
159
163
160
164
<imgsrc="/img/170429-aws-lambda-python-3-6/lambda-handler-and-role.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="For the final configuration, keep the default handler, create a new role from a template for Simple Microservice permissions and save it with a unique name.">
161
165
162
-
Now that our code and configuration is in place, click the "Next" button
166
+
Our code and configuration is in place so click the "Next" button
163
167
at the bottom right corner of the page.
164
168
165
-
<imgsrc="/img/170428-aws-lambda-python-2-7/review-lambda.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="We can review the values set during our configuration.">
The review screen shows us our configuration settings to make sure we
172
+
selected the appropriate values for our new Lambda function. Scroll down
173
+
press "Create function".
174
+
175
+
<imgsrc="/img/170429-aws-lambda-python-3-6/create-function.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Click the create function button to continue.">
166
176
167
-
The review screen will show us our configuration settings. Scroll down
168
-
to the bottom and click the "Create function" button to continue.
177
+
Success message should appear on the next page below the "Test" button.
169
178
170
-
<imgsrc="/img/170428-aws-lambda-python-2-7/create-function.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Click the create function button to continue.">
179
+
<imgsrc="/img/170429-aws-lambda-python-3-6/test.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Test button on the execution screen.">
171
180
172
-
We should see a success message on the next page just below the
173
-
"Save and test" button.
181
+
Click the "Test" button to execute the Lambda. Lambda will prompt us for
182
+
some data to simulate an event that would kick off our function. Select
183
+
the "Hello World" sample event template, which contains some keys but our
184
+
Lambda will not use that in its execution. Click the "Save and test" button
185
+
at the bottom of the modal.
174
186
175
-
<imgsrc="/img/170428-aws-lambda-python-2-7/save-and-test.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Save and test button.">
187
+
<imgsrc="/img/170429-aws-lambda-python-3-6/sample-event-template.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Sample event template for Lambda execution.">
176
188
177
-
Click that "Save and test" button to execute the Lambda. At first it
178
-
may appear that nothing happened but scroll down to the "Execution result"
179
-
section where we can see our output.
189
+
Scroll down to the "Execution result" section where we can see our output.
180
190
181
-
<imgsrc="/img/170428-aws-lambda-python-2-7/execution-results.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Execution results from running our Lambda function.">
191
+
<imgsrc="/img/170429-aws-lambda-python-3-6/execution-results.jpg"width="100%"class="technical-diagram img-rounded bordered"alt="Results from executing our new Lambda function.">
182
192
183
-
We get the log output that shows us the return value of our function. In
184
-
this case it is the string message from `what_to_print`. We can also see
185
-
down below that our print function produced output five times.
193
+
The log output shows us the return value of our function, which in this
194
+
execution was the string message from `what_to_print`. We can also see
195
+
our print function produced output five times as expected based on the
196
+
amount set in the `how_many_times` environment variable.
186
197
187
198
188
199
## Next Steps
189
-
Awesome, you just configured, wrote and executed your first Python 3.6
190
-
code on AWS Lambda! The real power of Lambda comes in when you connect a
191
-
trigger to it so your code executes based on events. We'll take a look
192
-
at that in the next tutorial.
200
+
You just configured, coded and executed your first Python 3.6 AWS Lambda
201
+
function! The real power of Lambda comes in when you use triggers to
202
+
your Lambda function so it executes based on events that happen.
203
+
We will take a look at that in the next tutorial.
193
204
194
-
What else can you do with Python and Lambda? Take a look at the
195
-
[AWS Lambda](/aws-lambda.html) page for more examples and tutorials.
205
+
Take a look at the [AWS Lambda](/aws-lambda.html) page for additional
206
+
examples and tutorials that other folks have shared for Lambda with Python.
0 commit comments