#100DaysOfWeb in Python Transcripts
Chapter: Days 85-88: Serverless Python with AWS Lambda
Lecture: 4 day plan, PEP8 Checker app, setup

Login or purchase this course to watch this video and the rest of the course contents.
0:01 All right, here's the plan for the coming four days. The goal is to create a Lambda to PEP 8 check submitted code.
0:10 So first, we're going to make a small web app with a microframework that's called Bottle that has a form that can post code to AWS.
0:20 Then we're going to create a first lambda function to run PEP 8 on the submitted code. To reach the lambda, we need to setup an AWS endpoint.
0:32 We're going to use AWS gateway API for that. Once we run the lambda we get back the response to the user which will show in the Bottle front end.
0:45 Actually when preparing this lesson the PEP 8 example was a bit more complex because we needed the external PEP 8 package. so I made a second
0:53 more basic lambda to do calculations. So, I will show that one first. But the end goal is to make the PEP 8 checker. And this is how it will look.
1:05 So we have to Bottle app, the form the verified code button makes the trip to the lambda and it gets back the result. And here I missed the line
1:15 because between constants and functions are classes. We need two lines and the PEP 8 checker spotted that and we showed the output.
1:26 And we do a bit of checking in the template if it's a good or a bad result and according to that we show a red
1:31 or a blue bar right on top of the output. Here's a another example of a PEP 8 violation. Here I left spaces in this return dictionary
1:42 where PEP 8 says that there should not be a white space after an opening curly brace and before a closing curly brace.
1:52 Once I fix that, I have a passing PEP 8. And actually in the final version I make two columns because I should see the code is truncated here.
2:02 So we will have the code on the left and the PEP 8 results at the right. So how it works, We have to Bottle app.
2:11 The user enters some code into text field. User hits verify code which makes a post request to the AWS gateway API.
2:20 We're going to use requests for that. So this is the AWS gateway API which invokes the lambda. Lambda saves the code to a temporary file
2:36 because the PEP 8 checker needs a file cannot just run on a string. And lambda then returns the response object. So, basically a dictionary containing
2:49 the status code, 200 if okay 400 if an exception, and the body with the actual PEP 8 output. Let's get set up in a virtual environment
2:59 and install Bottle and requests. So, we're going to do that next and we're going to set AWS endpoint in a virtual environment.
3:08 Because once we get the endpoint URL where we're going to post our code to it's probably something you want to keep private.
3:15 So, we're going to load that in from the virtual environment not committed to the source code. We're going to make a new directory.
3:30 And that's an alias for creating a virtual environment and activated it. So, I'm in my virtual environment. The type Python it's using the
3:39 one installed in my virtual environment and we're using Python 3.7. So, I'm going to pip install requests and bottle. Now we're going to temporarily
3:55 deactivate the virtual environment to set my AWS underscore endpoint environment variable. So I set that and then activate
4:07 It takes the activation script that runs when I activate my virtual environment. I go to the end and I do an export AWS endpoint
4:18 and for now leave it blank. Once we set up our AWS gateway API we're going to populate this variable. Activate again and that's all required for setup.
4:29 So, we have requests and Bottle and some additional dependencies. In the next video, we're going to build our Bottle app.


Talk Python's Mastodon Michael Kennedy's Mastodon