|
| 1 | +title: Getting Started with AWS Lambda & Python 2.7 |
| 2 | +slug: aws-lambda-python-2-7 |
| 3 | +meta: Learn how to create and deploy your first Amazon Web Services (AWS) Lambda function with Python 2.7. |
| 4 | +category: post |
| 5 | +date: 2017-04-28 |
| 6 | +modified: 2017-04-28 |
| 7 | +headerimage: /img/170428-aws-lambda-python-2-7/header.jpg |
| 8 | +headeralt: AWS, AWS Lambda and Python logos, copyright their respective owners. |
| 9 | + |
| 10 | + |
| 11 | +[Amazon Web Services (AWS) Lambda](/aws-lambda.html) |
| 12 | +is a "serverless" compute service that executes arbitrary Python code in |
| 13 | +response to developer-defined events, such as inbound API calls or file |
| 14 | +uploads to [AWS S3](https://aws.amazon.com/s3/). Note that AWS Lambda has |
| 15 | +nothing to do with the `lambda` keyword in Python that is used to create |
| 16 | +anonymous functions, it's just the product name that happens to collide |
| 17 | +with an existing Python language feature name. |
| 18 | + |
| 19 | +In this tutorial we'll learn how to quickly write and run a Lambda |
| 20 | +function that executes some simple Python 2.7 code and handles environment |
| 21 | +variables. The code can then be modified to build far more complicated |
| 22 | +Python applications. |
| 23 | + |
| 24 | + |
| 25 | +## Tools We Need |
| 26 | +We do not need any local development environment tools to get through |
| 27 | +this walkthrough other than a web browser because all the work will |
| 28 | +happen on AWS. |
| 29 | + |
| 30 | +Grab a new free tier [Amazon Web Services account](https://aws.amazon.com/) |
| 31 | +or use your existing AWS account. |
| 32 | + |
| 33 | + |
| 34 | +## First Steps with Lambda |
| 35 | +Head to the |
| 36 | +[AWS Lambda landing page](https://aws.amazon.com/lambda/) in your |
| 37 | +web browser. Sign into your account, or sign up for a new account which |
| 38 | +comes with a free tier so you don't have to pay. |
| 39 | + |
| 40 | +<img src="/img/170428-aws-lambda-python-2-7/aws-amazon-com.jpg" width="100%" class="technical-diagram img-rounded" alt="AWS Lambda landing page."> |
| 41 | + |
| 42 | +If you're not taken directly to the |
| 43 | +[Lambda Console page](https://console.aws.amazon.com/lambda/home) after |
| 44 | +logging in you'll see the main Console. AWS has a ridiculous number of |
| 45 | +services (that seems to expand every week) so the best way to get around |
| 46 | +is to select the search text box and search for "lambda" as shown in the |
| 47 | +following screenshot. |
| 48 | + |
| 49 | +<img src="/img/170428-aws-lambda-python-2-7/search-for-lambda.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="Search for lambda in the dashboard text box."> |
| 50 | + |
| 51 | +Press the "Create a Lambda function" button and you'll see the |
| 52 | +"Select Blueprint" page. |
| 53 | + |
| 54 | +<img src="/img/170428-aws-lambda-python-2-7/select-blueprint.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="The select blueprint Lambda screen, where you should select Blank Function."> |
| 55 | + |
| 56 | +Choose "Blank Function". The next screen gives the option to select a |
| 57 | +"trigger", which is how the Lambda function gets executed. A trigger is |
| 58 | +some event that is integrated with other AWS services and can be exposed |
| 59 | +externally via an API or device such as Alexa. |
| 60 | + |
| 61 | +<img src="/img/170428-aws-lambda-python-2-7/configure-triggers.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="Configure trigger screen, which we will not use for now because we will manually kick off our Lambda."> |
| 62 | + |
| 63 | +However, we aren't going to set up a trigger for this function because |
| 64 | +we can manually test the Lambda later before connecting it to a trigger. |
| 65 | +Leave the trigger icon blank and click the "Next" button to move along |
| 66 | +to the next screen. |
| 67 | + |
| 68 | +<img src="/img/170428-aws-lambda-python-2-7/blank-lambda.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="Blank Lambda configuration screen."> |
| 69 | + |
| 70 | +Now we're on the screen where we can enter our specific configuration |
| 71 | +and code for our new Lambda. |
| 72 | + |
| 73 | + |
| 74 | +## Writing Our Python Code |
| 75 | +Start by entering a name for your Lambda function, such as "my_first_python_lambda" and a description. The description field is optional but it's handy |
| 76 | +when you start using Lambda regularly to keep all your functions straight. |
| 77 | +In the Runtime drop-down, select Python 2.7 as the execution language. |
| 78 | + |
| 79 | +<img src="/img/170428-aws-lambda-python-2-7/first-python-lambda.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="Enter a name, description and select Python 2.7 on the Lambda configuration screen."> |
| 80 | + |
| 81 | +Below the Runtime drop-down you'll see a large text box for writing code. |
| 82 | +We can also choose to upload a ZIP file with our Python application which |
| 83 | +is handy for more than simple test Lambdas. However, for our simple starter |
| 84 | +Lambda application you can copy or type in the following code |
| 85 | +([or copy it from this GitHub repo](https://github.com/fullstackpython/blog-code-examples/blob/master/aws-lambda-python-2-7/lambda.py)). |
| 86 | +Make sure to replace what's already in the text box. |
| 87 | + |
| 88 | + |
| 89 | +```python |
| 90 | +import os |
| 91 | + |
| 92 | + |
| 93 | +def lambda_handler(event, context): |
| 94 | + what_to_print = os.environ.get("what_to_print") |
| 95 | + how_many_times = int(os.environ.get("how_many_times")) |
| 96 | + |
| 97 | + # make sure what_to_print and how_many_times values exist |
| 98 | + if what_to_print and how_many_times > 0: |
| 99 | + for i in range(0, how_many_times): |
| 100 | + print(what_to_print) |
| 101 | + return what_to_print |
| 102 | + return None |
| 103 | +``` |
| 104 | + |
| 105 | +The above code has the required `lambda_handler` function definition |
| 106 | +that provides a hook for the Lambda service to know where to begin executing |
| 107 | +the Python code. Think of `lambda_handler` as a `main` function when you're |
| 108 | +using this service. |
| 109 | + |
| 110 | +Our Python code expects and reads two environment variables, which we will |
| 111 | +set in just a moment, and then prints a message zero to many times, based |
| 112 | +on the amount defined in the `how_many_times` variable. If a message is |
| 113 | +printed then the function returns the `what_to_print` string, if nothing |
| 114 | +is printed then `None` is returned. |
| 115 | + |
| 116 | +Just below the code input text box there are environment variable key-value |
| 117 | +pairs that can be set. Our code will use two environment variables, named |
| 118 | +`what_to_print` and `how_many_times`. |
| 119 | + |
| 120 | +Enter the keys named `what_to_print` and `how_many_times` then enter their |
| 121 | +values. Use a string message for `what_to_print`'s value and an integer |
| 122 | +whole number above 0 for `how_many_times`. Our Python code's error handling |
| 123 | +is not very robust so a value other than a number in the `how_many_times` |
| 124 | +variable will cause the script to throw an error when it is executed. |
| 125 | + |
| 126 | +<img src="/img/170428-aws-lambda-python-2-7/environment-variables.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="Enter the exact keys of what_to_print and how_many_times along with corresponding values as environment variables."> |
| 127 | + |
| 128 | +Our code and environment variables are in place and we just need to set |
| 129 | +a few more AWS-specific settings before we can test the Lambda function. |
| 130 | + |
| 131 | + |
| 132 | +## Executing the Lambda |
| 133 | +Scroll down below the environment variables to the |
| 134 | +"Lambda function handler and role" section. This section contains the last |
| 135 | +few required configuration items. Keep the default handler, which should |
| 136 | +be `lambda_function.lambda_handler`. Select |
| 137 | +"Create a new Role from template(s)" from the drop-down then for the |
| 138 | +"Role name" field enter "dynamodb_permissions". Under "Policy templates" |
| 139 | +select the "Simple Microservice permissions". |
| 140 | + |
| 141 | +<img src="/img/170428-aws-lambda-python-2-7/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."> |
| 142 | + |
| 143 | +The "Simple Microservice permissions" gives our Lambda access to |
| 144 | +[AWS DynamoDB](https://aws.amazon.com/dynamodb/). We won't use DynamoDB in |
| 145 | +this tutorial but it's super useful as either permanent or temporary |
| 146 | +storage when working with Lambda. |
| 147 | + |
| 148 | +Now that our code and configuration is in place, click the "Next" button |
| 149 | +at the bottom right corner of the page. |
| 150 | + |
| 151 | +<img src="/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."> |
| 152 | + |
| 153 | +The review screen will show us our configuration settings. Scroll down |
| 154 | +to the bottom and click the "Create function" button to continue. |
| 155 | + |
| 156 | +<img src="/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."> |
| 157 | + |
| 158 | +We should see a success message on the next page just below the |
| 159 | +"Save and test" button. |
| 160 | + |
| 161 | +<img src="/img/170428-aws-lambda-python-2-7/save-and-test.jpg" width="100%" class="technical-diagram img-rounded bordered" alt="Save and test button."> |
| 162 | + |
| 163 | +Click that "Save and test" button to execute the Lambda. At first it |
| 164 | +may appear that nothing happened but scroll down to the "Execution result" |
| 165 | +section where we can see our output. |
| 166 | + |
| 167 | +<img src="/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."> |
| 168 | + |
| 169 | +We get the log output that shows us the return value of our function. In |
| 170 | +this case it is the string message from `what_to_print`. We can also see |
| 171 | +down below that our print function produced output five times. |
| 172 | + |
| 173 | + |
| 174 | +## What's Next? |
| 175 | +Awesome, you just configured, wrote and executed your first Python 2.7 |
| 176 | +code on AWS Lambda! The real power of Lambda comes in when you connect a |
| 177 | +trigger to it so your code executes based on events. We'll take a look |
| 178 | +at that in the next tutorial. |
| 179 | + |
| 180 | +What else can you do with Python and Lambda? Take a look at the |
| 181 | +[AWS Lambda](/aws-lambda.html) page for more examples and tutorials. |
| 182 | + |
| 183 | +Questions? Contact me via Twitter |
| 184 | +[@fullstackpython](https://twitter.com/fullstackpython) |
| 185 | +or [@mattmakai](https://twitter.com/mattmakai). I am also on GitHub with |
| 186 | +the username [mattmakai](https://github.com/mattmakai). |
| 187 | + |
| 188 | +Something wrong with this post? Fork |
| 189 | +[this page's source on GitHub](https://github.com/mattmakai/fullstackpython.com/blob/master/content/posts/170428-python-2-7-aws-lambda.markdown). |
| 190 | + |
0 commit comments