#100DaysOfWeb in Python Transcripts
Chapter: Days 85-88: Serverless Python with AWS Lambda
Lecture: First Lambda: a simple calculator (using operator)

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now it's time to get to the core of this lesson and that's to define a Lambda function. I'm logged into AWS Web Services
0:11 and I'm at the management console. I already have some Lambdas here. To create a new one, use the create function button. There are several options
0:25 but I usually author them from scratch. You have to give your Lambda a name so let's call it Calculator, because as I mentioned in the previous video
0:36 we're going to go with the simple one first because the fact check will require external dependencies so that will be a more advanced use case
0:45 which I'm eager to teach you as well. For the run time, recently they upgraded to 3.7 so that's the preferred Python I would use
0:55 and I already set up a role previously but if this is the first time you visit AWS Lambda you can create a new role from one or more templates.
1:06 You can call it then anything you want. For example, Lambda role and for policy you can use simple microservice permissions.
1:23 And that's it. Then we click on Create Function and we get into this new screen and AWS gives us some default code. And it's pretty simple.
1:38 A Lambda function is just a function that receives an event and a context if not really used context, but event is where you get your payload from
1:47 and you do your implementation, calculations whatever you want to do, and you return a dict like object with status code and body.
1:57 And we've seen in the Bottle app that I was parsing out those two elements. Now to do a simple calculator, I define one.
2:06 So we import operator, we define a couple of calculations: adding, subtraction, multiplication, and division.
2:15 Here's your Lambda handler as well as provided with the default code. We get the code attribute from the event object that's passed in.
2:22 Remember from our Bottle app, when we made the post request? Here's the other side of that equation. So the Lambda receives the event
2:31 and here we extract that code attribute from that dictionary. So here the Lambda receives the data that's passed in
2:38 we set a default status code of OK 200 and then we try to do some calculations. So the code passed in will be something like two plus two.
2:50 We try to split that into the first number the sign, and the second number then we get the actual operator from the dictionary.
2:57 So we look up the sign, plus, minus, etc. and we get back this function which we then call with the first and the second number.
3:06 So this basically, if I give it two plus two it's almost like an eval of two plus two. And basically that's the math. And then, start a return value
3:18 but if that data was provided so we get a key error or a value error sample. If we provide a, b, c, then this split, of course
3:27 will complain that there are not enough elements to put into these three variables. I can also have a zero division error, if I pass it
3:36 for example, one divided by zero. So those are tested exceptions and I catch 'em all on this line and if there's an exception, I just return
3:45 the exception string to get our status code 400. Then I return a dict of status code with whatever came out which could be OK or not OK, and the body
3:55 which is the return value, which could be the result of the calculation or the exception string. And that's it, really. I mean, there are more options.
4:04 You can tweak them but usually the defaults are just what you want. A memory consumption of 128 meg and a time out of three seconds.
4:13 If your calculations are more intensive for example, in code challenges we recently supported PEMDAS, and we had to bump up
4:20 this time out to 20 or even 30 seconds because that library just takes more time to run. So we can tweak that here under Basic Settings.
4:29 For example, this should suffice. So we click Save and next video we'll see how we can define test events
4:36 so we can test our Lambda from this GUI interface.


Talk Python's Mastodon Michael Kennedy's Mastodon