Latest / Talk Python To Me / #458: Serverless Python in 2024
Transcript
- 0:00What is the state of serverless computing and Python in 2024?
- 0:03What are some of the new tools and best practices?
- 0:06Well, we're lucky to have Tony Sherman,
- 0:09who has a lot of practical experience
- 0:11with serverless programming on the show.
- 0:13This is Talk Python To Me,
- 0:15episode 458, recorded January 25th, 2024.
- 0:19Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:37This is your host, Michael Kennedy.
- 0:39Follow me on Mastodon, where I'm @mkennedy,
- 0:42and follow the podcast using @talkpython,
- 0:44both on fosstodon.org.
- 0:46Keep up with the show and listen to over seven years
- 0:49of past episodes at talkpython.fm.
- 0:52We've started streaming most of our episodes live on YouTube.
- 0:55Subscribe to our YouTube channel over at talkpython.fm
- 0:58slash YouTube to get notified about upcoming shows
- 1:01and be part of that episode.
- 1:03This episode is brought to you by Sentry.
- 1:05Don't let those errors go unnoticed.
- 1:07Use Sentry like we do here at Talk Python.
- 1:09Sign up at talkpython.fm/sentry.
- 1:12And it's brought to you by Mailtrap,
- 1:15an email delivery platform that developers love.
- 1:18Try for free at mailtrap.io.
- 1:21Tony, welcome to Talk Python To Me.
- 1:23Thank you.
- 1:24Thanks for having me.
- 1:25Fantastic to have you here.
- 1:26It's going to be really fun to talk about serverless.
- 1:29You know, the joke with the cloud is,
- 1:32well, I know you call it the cloud,
- 1:33but it's really just somebody else's computer.
- 1:35But we're not even talking about computer.
- 1:37We're just talking about functions.
- 1:38Maybe it's someone else's function.
- 1:40I don't know.
- 1:40We're going to find out.
- 1:40Yeah, I actually, I saw a recent article about server-free recently with somebody trying to,
- 1:46yeah, move completely.
- 1:48Yeah.
- 1:49Yeah.
- 1:49Because as you might know, serverless doesn't mean actually no servers.
- 1:53Of course.
- 1:54Of course.
- 1:54Server-free.
- 1:56All right.
- 1:56So we could just get the thing to run on the BitTorrent network.
- 2:00Got it.
- 2:01Okay.
- 2:01Yeah.
- 2:02I don't know.
- 2:03I don't know.
- 2:03We'll figure it out.
- 2:04But it's going to be super fun.
- 2:06And we're going to talk about your experience working with serverless.
- 2:10We'll talk about some of the choices people have out there
- 2:12and also some of the tools that we can use to do things like observe and test our serverless code.
- 2:18Yeah.
- 2:19For that, though, tell us a bit about yourself.
- 2:21Sure.
- 2:22So I'm actually a career changer.
- 2:26So I worked in the cable industry for about 10 years and doing a lot of different things
- 2:34from installing, you know, knock out the door cable guy to working on more of the outside plant.
- 2:40But at some point, I was seeing limits of career path there.
- 2:46And so my brother-in-law is a software engineer.
- 2:50And I had already started kind of going back to school, finishing my degree.
- 2:54And I was like, okay, well, maybe I should kind of look into this.
- 2:57And so I took an intro to programming class.
- 3:00It was in Python.
- 3:01And that just kind of, yeah, led me down this path.
- 3:05So now for the past, I don't know, four years or so, been working professionally in the software world,
- 3:09started out in a QA role at an IoT company.
- 3:14And now, yeah, doing a lot of serverless programming in Python these days.
- 3:19Second company now, but that does some school bus safety products.
- 3:24Interesting.
- 3:25Very cool.
- 3:26Yeah.
- 3:26Yep.
- 3:27But a lot of Python and a lot of serverless.
- 3:29Well, serverless and IoT, I feel like they go pretty hand in hand.
- 3:34Yes.
- 3:35Yep.
- 3:35Yeah.
- 3:36Another thing is with serverless is when you have very like spiky traffic.
- 3:42Like if you think about school buses that, you know, you have a lot coming on twice a day.
- 3:47To bimodal.
- 3:48Exactly.
- 3:48Like the 8 a.m. shift and then the 2.30 to 3 shift.
- 3:52So, yeah, that's a really good use case for serverless is something like that.
- 3:57Okay.
- 3:58Are you enjoying doing the programming stuff instead of the cable stuff?
- 4:01Absolutely.
- 4:02I sometimes I live in Michigan, so I look outside and look at, you know, the snow coming down or these storms.
- 4:10And yeah, I just, yeah, I really, some people like you don't miss being outside.
- 4:14I'm like, maybe every once in a while, but I can, I can go walk outside on a nice day.
- 4:19You can choose to go outside.
- 4:21You're not ready to go outside in the sleet or rain.
- 4:24Yeah.
- 4:25Yeah, absolutely.
- 4:26We just had a mega storm here and just the huge tall trees here in Oregon just fell left and right.
- 4:33And there's in every direction that I look, there's a large tree on top of one of the houses of my neighbors.
- 4:39Oh, man.
- 4:40Maybe a house or two over.
- 4:42But it just took out all the, everything that was a cable in the air was taken out.
- 4:46So it's just been a swarm of people who are out in 13 degree Fahrenheit, you know, negative nine Celsius weather.
- 4:52And I'm thinking, you know, not really choosing to be out there today, probably.
- 4:55Right.
- 4:56Excellent.
- 4:57Well, thanks for that introduction.
- 4:58I guess maybe we could, a lot of people probably know what serverless is, but I'm sure there's a lot who are not even really aware of what serverless programming is.
- 5:07Right.
- 5:08Let's talk about what's the idea.
- 5:10What's the, what's the Zen of this?
- 5:12Yeah.
- 5:13So, yeah, I kind of, you know, made the joke that serverless doesn't mean there are no servers, but, and there's, hopefully I don't butcher it too much, but it's more like, functions as a service.
- 5:25There's other things that can be serverless too.
- 5:28Like there's, you know, serverless databases or, a lot of different, services that can be serverless.
- 5:35Meaning you don't have to think about like how to operate them, how to think about, you know, scaling them up.
- 5:40You don't have to, you know, spin up, VMs or, or Kubernetes clusters or anything.
- 5:45You, you don't have to think about that part.
- 5:48It's just your, your code that goes into it.
- 5:51And so, yeah, serverless functions are probably what people are most familiar with.
- 5:55And that's, I'm sure what we'll talk about most today, but, and, yeah, that's really the idea.
- 6:01You don't have to, you don't have to manage the server.
- 6:04Oh, sure.
- 6:05And that's a huge barrier.
- 6:07I remember when I first started getting into web apps and programming and then another level, when I got into Python, because I had not done that much Linux work, getting stuff up running.
- 6:19It was really tricky.
- 6:20And then having the concern of, is it secure?
- 6:24How do I patch it?
- 6:25How do I back it up?
- 6:26How do I keep it going?
- 6:27All of those things, they're non-trivial, right?
- 6:31Right.
- 6:31Yeah.
- 6:32Yeah.
- 6:32There's, there's a lot to think about.
- 6:33And, and if you like work at an organization, it's probably different everywhere you go to that.
- 6:39They, you know, how they manage their servers and things.
- 6:42So, so putting in some stuff in the cloud kind of brings some, some commonality to it too.
- 6:46Like you can, you can learn how, you know, the Azure cloud or Google cloud or AWS, how those things work and, and kind of have some common ground too.
- 6:55So, yeah, for sure.
- 6:58Like having, I mean, also feels more accessible to the developers in a larger group.
- 7:03You know, in the sense that it's not a DevOps team that kind of takes care of the servers or a production engineers where you hand them your code.
- 7:11It's, it's a little closer to just, I have a function and then I get it up there and it continues to be the function, you know?
- 7:17Yeah.
- 7:17You, and that is a different mindset too.
- 7:19You kind of see it all the way through from writing your code to deploying it.
- 7:23yeah.
- 7:25With, without maybe an entire DevOps team that you just kind of say, here you go, go deploy this.
- 7:31Yeah.
- 7:32Yeah.
- 7:32So in my world, I mostly have virtual machines.
- 7:37I've moved over to kind of a Docker cluster.
- 7:40I think I've got 17 different things running in the Docker cluster at the moment, but both of those are really different than serverless.
- 7:48Right.
- 7:48Yeah.
- 7:49Yeah.
- 7:49So it's been working well for me, but when I think about serverless, let me know if this is true.
- 7:55It feels like you don't need to have as much of a, a Linux or server or, or sort of an ops experience to create these things.
- 8:04Yeah.
- 8:05I would say like you could probably get away with like almost none, right?
- 8:09Like at the simplest form, like with, like AWS, for instance, their Lambda functions, you can, and that's the one I'm most familiar with.
- 8:19So, forgive me for using them as an example for everything.
- 8:22There's a lot of, different, serverless, options, but, you could go into the AWS console and you could actually, write your, you know, Python code right in the console, deploy that they have function URLs now.
- 8:37So you could actually have like, I mean, within a matter of minutes, you can have a serverless function set up.
- 8:43And so, AWS Lambda, right.
- 8:46That's the one.
- 8:47Yep.
- 8:48Lambda being, I guess, a simple function, right?
- 8:50We have Lambdas in Python.
- 8:51They can only be one line.
- 8:52So I'm sure you can have more than one line in the AWS Lambda, but.
- 8:56Yeah.
- 8:56There are limitations though, with, with Lambda that you, that are, definitely some pain points that I ran into.
- 9:03So.
- 9:03Oh, really?
- 9:04Okay.
- 9:04What are some of the limitations?
- 9:05Yeah.
- 9:06So, package size is one.
- 9:09So if you start thinking about all these like amazing, packages on PyPI, you do have to start thinking about how, how many you're going to bring in.
- 9:19So, and I don't know the exact limits off the top of my head, but it's pretty quick Google search on their, their package size.
- 9:27It might be like 50 megabytes zipped, but two 50.
- 9:31when you decompress it, to do a zip base, then they do have, containerized Lambda functions that go up to like a 10 gig limit.
- 9:41So that helps, but, okay.
- 9:43Yeah.
- 9:43Yeah.
- 9:44They, those ones, used to be less performant, but they're, they're kind of catching up to where they're, that was really on something called cold starts.
- 9:52but they're, they're getting, I think pretty close to it.
- 9:56Not, not being a very big difference, whether you Dockerize or zip these functions, but, but yeah.
- 10:03So when you start just like pip installing everything, you've got to think about how to get that code into your function.
- 10:10and, how much it's going to bring in.
- 10:13So yeah, that definitely was, a limitation that, had to quickly learn.
- 10:19Yeah.
- 10:20I guess it's probably trying to do pip install -r effectively.
- 10:24Yeah.
- 10:25And it's like, you know, you, you can't go overboard with this.
- 10:27Right.
- 10:28Right.
- 10:28Right.
- 10:29Yeah.
- 10:29Yeah.
- 10:29When you start bringing in packages, like, you know, maybe like some of the like, scientific packages, you're, you're definitely going to be hitting some, some size limits.
- 10:38Okay.
- 10:38And with the containerized ones, basically you get probably give it a Docker file and a command to run in it and it can, it can build those images before and then just execute and just do a Docker run.
- 10:49Yeah.
- 10:49I think how those ones work is you, you store an image, on like their container registry, Amazon's, is it ECR, I think.
- 10:58And so then you kind of pointed at that and yeah, it'll, execute your like handler function.
- 11:05when that, when the Lambda gets called.
- 11:08This portion of talk Python and me is brought to you by multi-platform error monitoring at Sentry.
- 11:14Code breaks.
- 11:16It's a fact of life.
- 11:17With Sentry, you can fix it faster.
- 11:19Does your team or company work on multiple platforms that collaborate?
- 11:23Chances are extremely high that they do.
- 11:27It might be a reactor view front end JavaScript app that talks to your FastAPI backend services.
- 11:32Could be a go microservice talking to your Python microservice or even native mobile apps talking to your Python backend APIs.
- 11:41Now let me ask you a question.
- 11:42Whatever combination of these that applies to you, how tightly do these teams work together?
- 11:47Especially if there are errors originating at one layer, but becoming visible at the other.
- 11:52It can be super hard to track these errors across platforms and devices.
- 11:56But Sentry has you covered.
- 11:58They support many JavaScript front end frameworks.
- 12:00Obviously Python backend, such as FastAPI and Django.
- 12:04And they even support native mobile apps.
- 12:06For example, at Talk Python, we have Sentry integrated into our mobile apps for our courses.
- 12:11Those apps are built and compiled in native code with Flutter.
- 12:15With Sentry, it's literally a few lines of code to start tracking those errors.
- 12:18Don't fly blind.
- 12:20Fix code faster with Sentry.
- 12:22Create your Sentry account at talkpython.fm/sentry.
- 12:25And if you sign up with the code TALKPYTHON, one word, all caps,
- 12:28it's good for two free months of Sentry's business plan,
- 12:32which will give you up to 20 times as many monthly events,
- 12:34as well as some other cool features.
- 12:36My thanks to Sentry for supporting Talk Python To Me.
- 12:39Yeah, so out in the audience, Kim says,
- 12:43AWS does make a few packages available directly just by default in Lambda.
- 12:48That's kind of nice.
- 12:49Yeah, yep.
- 12:50So, yeah, Bodo, which if you're dealing with AWS and Python,
- 12:54you're using the Bodo package.
- 12:57And yeah, that's included for you.
- 12:59So that's definitely helpful in any of their, you know,
- 13:02transitive dependencies would be there.
- 13:04I think Bodo used to even include, like, requests,
- 13:08but then I think they eventually dropped that with some, like, SSL stuff.
- 13:12But yeah, you definitely, you can't just, like, pip install anything
- 13:17and not think of it unless, depending on how you package these up.
- 13:21So.
- 13:21Sure.
- 13:22Sure.
- 13:22That makes sense.
- 13:23Yeah.
- 13:23Of course they would include their own Python libraries, right?
- 13:26Yeah.
- 13:26And it's not a, yeah, it's not exactly small.
- 13:29I think, like, botocore used to be, like, 60 megabytes,
- 13:34but I think they've done some work to really get that down.
- 13:36So.
- 13:37Yeah.
- 13:38Yeah, that's, yeah, that's not too bad.
- 13:40I feel like botocore, boto3, those are constantly changing.
- 13:43Like, constantly.
- 13:45Yeah.
- 13:45Yeah, well, as fast as AWS adds services, they'll probably keep changing quickly.
- 13:52Yeah.
- 13:53I feel like those are auto-generated, maybe, just from looking at the way the API
- 13:57looks at it, you know, the way they look written.
- 13:59And so.
- 14:00Yeah.
- 14:00Yeah, that's probably the case.
- 14:03Yeah.
- 14:03I know they do that with, like, their infrastructure is code CDK.
- 14:09It's all, like, TypeScript originally, and then you have your Python bindings for it.
- 14:13And so.
- 14:13Right, right, right, right.
- 14:15I mean, it makes sense, but at the same time, when you see a change, it doesn't necessarily
- 14:18mean, oh, there's a, there's a new important aspect added.
- 14:22It's probably just, yeah, I don't know.
- 14:23People have actually pulled up the console for AWS, but just the amount of services that
- 14:29are there.
- 14:30And then each one of those has its own full API, like a little bit of the one of those
- 14:33things.
- 14:34So we regenerated it, but it might be for some part that you'd never, never call, right?
- 14:38Like you might only work with S3 and it's only changed.
- 14:41Yeah.
- 14:41I don't know, EC2 stuff, right?
- 14:44Right.
- 14:44Yep, exactly.
- 14:45Yeah, indeed.
- 14:47All right.
- 14:47Well, let's talk real quickly about some of the places where we could do serverless, right?
- 14:53Yeah.
- 14:53You mentioned AWS Lambda.
- 14:54Yeah.
- 14:55And I also maybe touch on just 1 million requests free per month.
- 15:00That's pretty cool.
- 15:01Yeah.
- 15:01Yeah.
- 15:02So yeah, getting like jumping into AWS sometimes sounds scary, but they have a pretty generous
- 15:08free tier.
- 15:08Definitely do your research on some of the security of this, but yeah, you can, you know, a million
- 15:13requests free per month.
- 15:15You probably have to look into that a little bit because it's, you know, you have your memory
- 15:21configurations too.
- 15:22So there's probably, I don't know exactly how that works within their free tier, but
- 15:26you're charged like with Lambda, at least it's your like invocation time and memory and
- 15:33also amount of requests.
- 15:34So yeah.
- 15:36I'm always confused when I look at that and go, okay, with all of those variables, is that
- 15:41a lot or a little?
- 15:42I know it's a lot, but it's hard for me to conceptualize.
- 15:45Like, well, I use a little more memory than I thought.
- 15:47So it costs like, wait a minute.
- 15:48How do I know how much memory I use?
- 15:49You know, like, yeah.
- 15:50What does this mean in practice?
- 15:52Yeah.
- 15:52It's built by how you configure it too.
- 15:55So if you say I need a Lambda with 10 gigs of memory, you're, you're being built at that
- 16:01like 10 gigabyte price threshold.
- 16:05So there is a really, a really cool tool called power to our AWS Lambda power tuner.
- 16:13So yeah, what that'll do is you can, it creates a state machine in AWS.
- 16:19Yeah.
- 16:19I think I did send you a link to that one.
- 16:21So the power tuner will create a state machine that, invocates your Lambda
- 16:28with several different memory configurations.
- 16:31And you can say, I want either the best cost optimized version or the best performance optimized
- 16:37version.
- 16:37So, and that'll tell you, like, it'll say, okay, yeah, you're best with a Lambda configured
- 16:43at, you know, 256 megabytes, you know, for memory.
- 16:48So, sorry.
- 16:50Yeah.
- 16:51For the link it's, this is power tools.
- 16:54This is a different, amazing package.
- 16:55Yeah.
- 16:56Maybe I didn't send you the, power tuner.
- 16:58I should, okay.
- 16:59Sorry.
- 16:59That's news to me.
- 17:00Okay.
- 17:01Sorry.
- 17:02Yeah.
- 17:02And they have similar names.
- 17:04Yeah.
- 17:04There's only so many ways to describe stuff.
- 17:07Right.
- 17:07Yeah.
- 17:08Okay.
- 17:08They have it right in their head.
- 17:09Yep.
- 17:09And it is an open source package.
- 17:11So there's probably a GitHub link in there, but yeah.
- 17:14Yeah.
- 17:14And, this will tell you like the best way to optimize your, your Lambda function, at
- 17:19least as far as memory is concerned.
- 17:22So, yeah, really good tool.
- 17:24It gives you a visualization and gives you a graph that will say like, okay, here's,
- 17:28here's kind of where cost and performance meet.
- 17:31And so, yeah, it's, it's really excellent for figuring that out.
- 17:36yeah, at least in, in AWS land, I don't know if some of the other cloud providers have
- 17:42something similar to this, but, yeah, it's definitely a really helpful tool.
- 17:47Sure.
- 17:48Yeah.
- 17:49Like I said, I've, I'm confused and I've been doing cloud stuff for a long time when I look
- 17:53at it.
- 17:53Yeah.
- 17:53So, well, there's some interesting things here.
- 17:55So like, you can actually have, a Lambda invocation that costs less with a higher
- 18:02memory configuration because it'll run faster.
- 18:05So you're, I think Lambda bills like by the millisecond now.
- 18:09So, you can actually, because it runs faster, it can be cheaper to run.
- 18:14So, well, that explains all the rust that's been getting written.
- 18:17Yeah.
- 18:17Yeah.
- 18:19There's a real number behind this.
- 18:21I mean, we need to go faster, right?
- 18:23Okay.
- 18:24So, you know, the, the, I think maybe AWS Lambda is one of the very first ones as well to come
- 18:30on with this concept of serverless.
- 18:32Yeah.
- 18:32It, it, I don't know for sure, but it probably is.
- 18:36And then, yeah, your other big cloud providers have them.
- 18:38And now you're actually even seeing them, come up with, a lot of like Vercel has
- 18:44some kind of, some type of serverless function.
- 18:47I don't know what they're using behind it, but it's almost like they just put a,
- 18:51a nicer UI around AWS Lambda or whichever cloud provider that's, you know, that's potentially
- 18:57backing this up.
- 18:58But, yeah, they're just reselling their flavor of, of somebody else's cloud.
- 19:03Yeah.
- 19:03Yeah.
- 19:04It could be because yeah, Vercel, obviously they have a really, nice suite of products
- 19:09with a, with a good UI, very usable.
- 19:11So, no.
- 19:12Okay.
- 19:13So Vercel, some of them people can try.
- 19:15And then we've got the two other hyperscale clouds, I guess you call them.
- 19:20Google cloud has serverless, right?
- 19:22Yep.
- 19:23Okay.
- 19:23So I'm not sure which ones that they might just be called cloud functions.
- 19:27and yeah, Azure also has, they got cloud run and cloud functions.
- 19:33I have no idea what the difference is though.
- 19:35Yep.
- 19:36Yep.
- 19:36And, yeah, Azure also has a serverless product and I'd imagine there's probably like
- 19:41even more that we're, we're not aware of, but, yeah, it's kind of, nice to not think
- 19:47about, setting up servers for something.
- 19:51So then I think maybe, is it fast?
- 19:53Yeah.
- 19:54Function as a service.
- 19:54Let's see.
- 19:55Yeah.
- 19:55But if we search for F-A-A-S instead of pass or, IaaS, right?
- 20:01There's, we've got Almeda, Intel.
- 20:04I saw that IBM had some, oh, there's also, the, we've got digital ocean.
- 20:09I'm a big fan of digital ocean because I feel like their pricing is really fair and they've
- 20:14got good documentation and stuff.
- 20:16So they've got functionless, sorry, serverless functions, that you can,
- 20:22I don't use these.
- 20:23Yeah.
- 20:23Yeah.
- 20:24I haven't used these either, but yeah.
- 20:26and, yeah, as far as costs, like for, especially for like small, like personal
- 20:30projects and things where you don't need to have a server on all the time, they're yeah,
- 20:36pretty, pretty nice.
- 20:37If you have a website that you need something server side where you got to have some Python,
- 20:41but you don't need a server going all the time.
- 20:44Yeah.
- 20:44Okay.
- 20:44Like maybe I have a static site, but then I want this one thing to happen.
- 20:49If somebody clicks a button, something like that.
- 20:51Yeah.
- 20:51Yeah, absolutely.
- 20:52Yep.
- 20:53You could be completely static, but have something that is, yeah.
- 20:57Yeah.
- 20:57That one function call that you do need.
- 20:59Yeah.
- 20:59Exactly.
- 21:00And then you also pointed out that cloud flare has some form of serverless.
- 21:05Yeah.
- 21:05And I haven't used these either, but, yeah, I do know that they have, some type
- 21:11of, you know, functions as a service.
- 21:14I don't know what frameworks for languages.
- 21:17They let you write them in there.
- 21:19Yeah.
- 21:19I use, bunny.net for my CDN.
- 21:23It's just absolutely awesome platform.
- 21:25I really, really love it.
- 21:26And one of the things that they've started offering, I can get this stupid, completely useless cookie
- 21:30banner to go away is they've offered a, what they call, edge compute.
- 21:35Oh, yeah.
- 21:36Okay.
- 21:36What you would do.
- 21:38I don't know where to find it somewhere, maybe, but basically the CDN has 115, 120 points of
- 21:46presence all over the world where, you know, this one's close to Brazil.
- 21:49This one's close to Australia, whatever.
- 21:51And, but you can actually run serverless functions on those things.
- 21:56Like, so you deploy them.
- 21:57So the code actually executes in 150, 115 locations.
- 22:01Yes.
- 22:02Yeah.
- 22:02Probably cloudflare or something like that as well, but I don't know.
- 22:04Yeah.
- 22:05AWS has, they have like Lambda at edge, at the edge.
- 22:10So that's kind of, goes hand in hand with their, like CDN, cloud, cloud front,
- 22:16I believe.
- 22:17Yeah.
- 22:17So they have something similar like that, where you have a, a Lambda that's going to
- 22:21be, you know, perform it because it's yeah.
- 22:23Distributed across their CDN.
- 22:25Yeah.
- 22:26CDN that's a whole nother world.
- 22:27They're, they're getting really advanced.
- 22:30Yeah.
- 22:30Yeah.
- 22:31Yeah.
- 22:33So, we won't, maybe that's a different show.
- 22:35It's not a show today, but it's just the idea of like you distribute the compute
- 22:40on the CDN is pretty nice.
- 22:42The drawback is just JavaScript, which is okay, but it's, it's not the same as right.
- 22:47Yes.
- 22:48Yeah.
- 22:48I wonder if you could do high script.
- 22:51Oh yeah.
- 22:52That's an interesting thought.
- 22:53Yeah.
- 22:53Yeah.
- 22:54Yeah.
- 22:54We're getting closer and closer to Python in the browser.
- 22:57So yeah.
- 22:57Yeah.
- 22:58My JavaScript includes this little bit of WebAssembly and I don't, I don't like semicolons,
- 23:02but go ahead and run it anyway.
- 23:03Yeah.
- 23:05Out in the audience.
- 23:06It looks like, Cloudflare probably does support Python, which is.
- 23:10Okay.
- 23:10Yeah.
- 23:11Yeah.
- 23:11There's, there's so many options out there for, for serverless functions that are, yeah,
- 23:15really, especially if you're already in, you know, if you're maybe deploying, you know,
- 23:19some static stuff over Cloudflare, Cloudflare or Vercel.
- 23:23Yeah.
- 23:24It's sometimes nice just to be all, all in on one, one service.
- 23:28Yeah.
- 23:29Yeah.
- 23:29It really is.
- 23:30Let's talk about using, choosing serverless over other things, right?
- 23:35You actually laid out two really good examples or maybe three, even with the static site
- 23:39example, but you know, I've got bursts of activity.
- 23:43Yeah.
- 23:43Right.
- 23:44And really, really low, incredibly, incredibly low usage other times.
- 23:50Right.
- 23:50Yeah.
- 23:51Yeah.
- 23:51You think of like, yeah, you're, you're black Friday traffic, right?
- 23:54Like you to not have to think of like how many servers to be provisioned for something
- 24:01like that.
- 24:02Or if you don't know, I think there's probably some like, well, I actually know there's
- 24:07been like some pretty popular articles about people like leaving the cloud.
- 24:11and, and yeah, like if you know your scale and you know, you know exactly what you
- 24:17need, yeah, you, you probably can save money by just having your, your own infrastructure
- 24:24set up or, but yeah, if you don't know, or it's very like spiky, you don't need
- 24:30to have a server that's consuming a lot of power running, you know, 24 hours a day.
- 24:34You can, just invoke a function as you need.
- 24:38So this portion of talk Python may is brought to you by Mailtrap and email delivery platform
- 24:46that developers love an email sending solution with industry, best analytics, SMTP and email
- 24:53APIs, SDKs for major programming languages and 24 seven human support.
- 24:59Try for free at Mailtrap.io.
- 25:02Yeah.
- 25:03There's a super interesting series by David Heinemeyer Hansen of Ruby on rails fame and
- 25:10from, base camp about how base camp has left the cloud and how they're saving $7 million
- 25:15and getting better performance over five years.
- 25:19Yeah.
- 25:19But that's, that's a big investment, right?
- 25:21They bought $600,000 for hardware, right?
- 25:26Yeah.
- 25:26Yeah.
- 25:27Only so many people can do that.
- 25:28Right.
- 25:29And you, you know, you gotta have that running somewhere that, you know, with, with,
- 25:33with backup power and, yeah.
- 25:36Yeah.
- 25:37So what they ended up doing for this one is they went with some service called theft,
- 25:43okay.
- 25:43Hosting, which is like white glove, white.
- 25:46So white, labeled is the word I'm looking for where it just looks like it's your hardware,
- 25:52but they put it into a mega data center and there's, you know, they'll have the hardware
- 25:57shipped to them and somebody will just come out and install it into racks and go, here's
- 26:00your IP.
- 26:00Right.
- 26:01Like a, yeah.
- 26:02virtual, virtual VM or a VM in a cloud that, but it takes three days, three
- 26:09weeks to boot.
- 26:09Right.
- 26:11Yeah.
- 26:11Yeah.
- 26:12Yeah.
- 26:12Which is kind of the opposite is almost, I mean, I'm kind of diving into it because it's,
- 26:16it's almost the exact opposite of the serverless benefits, right?
- 26:20This is insane stability.
- 26:22I have this thing for five years, 4,000 CPUs we've installed and we're using them for the
- 26:29next five years rather than how many milliseconds am I going to run this code for?
- 26:33Right.
- 26:34Exactly.
- 26:34Yeah.
- 26:35Yeah.
- 26:35Yeah.
- 26:36It's definitely the far opposite.
- 26:37And so, yeah, you kind of, you know, maybe serverless isn't for every use case, but it's
- 26:42definitely a nice like tool to have in the toolbox.
- 26:45And yeah, you definitely even working in serverless, like if you're, yeah, eventually you're going
- 26:51to need like maybe to interact with the database that's got to be on all the time.
- 26:54You know, it's yeah, there's a lot of, it's a good tool, but it's definitely not the,
- 26:59uh, one size fits all solution.
- 27:02So yeah, let's talk databases in a second, but for, you know, when does it make sense
- 27:07to say we're going to put this, like if this suppose I have an API, right?
- 27:10That's a pretty, an API is a real similar equivalent to what a serverless thing is.
- 27:15Like I'm going to call this API, I think it's going to happen.
- 27:17I'm going to call this function.
- 27:18The thing's going to happen.
- 27:19Let's suppose I have an API and it has eight endpoints.
- 27:22It's written in FastAPI or whatever it is.
- 27:24It might make sense to have that as serverless, right?
- 27:27You don't want to run a server and all that kind of thing.
- 27:29But what if I have an API with 200 endpoints?
- 27:32Like, where is the point where like, there are so many little serverless things.
- 27:35I don't even know where to look.
- 27:36They're everywhere.
- 27:37Which version is this one?
- 27:38You know what I mean?
- 27:38Like where, where's that trade-off and how do, you know, you and the people you work with
- 27:43think about that?
- 27:43Yeah, I guess that's a, a good, good question.
- 27:48I mean, as you start like, you know, getting into these like microservices, how small
- 27:54do you want to break these up?
- 27:55And so there, there, there is some different thoughts on that.
- 27:58Even like a, a Lambda function, for instance, if you, if you put this behind an API,
- 28:03uh, you can use a single Lambda function to, for your entire rest API, even if it is,
- 28:12you know, 200 endpoints.
- 28:13So, okay.
- 28:15So you put the whole app there and then when a request comes in, it routes to whatever
- 28:19part of your app.
- 28:20Theoretically.
- 28:21Yeah.
- 28:22Yeah.
- 28:22So there's, a package called power tools for, AWS power tools.
- 28:28I don't know.
- 28:29Power tools for Python.
- 28:30Yeah.
- 28:30I don't care.
- 28:30Yeah.
- 28:31Yeah.
- 28:31I know the similar name.
- 28:32Yeah.
- 28:33So they have a, a really good like event resolver.
- 28:36So you can actually, it's, it almost looks like, you know, flask or, some of the
- 28:42other Python web frameworks.
- 28:44And so you, you kind of have this resolver, whether it's, you know, API gateway and, and
- 28:49AWS are different.
- 28:50they have a few different options for, the API itself, but yeah, you, in, in theory,
- 28:57you could have your entire API behind a single Lambda function, but then that's probably not
- 29:03optimal.
- 29:04Right.
- 29:04So you're, that's where you have to, figure out how to break that up.
- 29:09And so, yeah, they do like that same, the decorators, you know, app dot post or
- 29:16yeah.
- 29:16Yeah.
- 29:17Yeah.
- 29:17And your endpoints and you can do the, with the, have them, have variables in there
- 29:23where maybe you have like ID, as your lookup and it can, you know, slash user slash
- 29:29ID is going to find your, find, you know, a single user.
- 29:33So, and, and their documentation, they actually addressed this a little bit.
- 29:38Like, do you want to do, they, they call it either like a, a micro function pattern where
- 29:44maybe every single endpoint has its own Lambda function.
- 29:48but yeah, that's a lot of overhead to maintain.
- 29:51If you had, like you said, 200 endpoints, you have 200 Lambdas.
- 29:55you gotta upgrade them all at the same time.
- 29:58So they have the right.
- 29:58Yeah.
- 29:59Yeah.
- 29:59Yeah.
- 29:59Yeah.
- 30:00That's right.
- 30:01So, yeah.
- 30:02So, there, there's definitely some, even, even conflicting views on this.
- 30:07How, how micro do you want to go?
- 30:09And so, I was able to go to AWS reinvent and, November and, and they actually kind
- 30:17of pitched this, this hybrid, maybe like if you take your like CRUD operations, right.
- 30:22And, and maybe you have, your create update and delete all on one Lambda that's con with
- 30:29its configuration for those, but your read is on another Lambda.
- 30:33So maybe your CRUD operations, they all interact with a relational database, but your reader just,
- 30:39uh, does like, reads from a dynamo database, where you kind of sync that, that data up.
- 30:45And so you could have your permissions kind of separated for each of those, Lambda functions.
- 30:50And, you know, people reading from an API don't always need the same permissions as,
- 30:56you know, updating, deleting.
- 30:58And so, so yeah, there's a lot of different ways to, to break that up and how, how micro
- 31:02do you go with this?
- 31:03definitely.
- 31:04How micro can you go?
- 31:05Yeah.
- 31:06Yeah.
- 31:07Cause it sounds to me like if you had many, many of them, then all of a sudden you're back
- 31:11to like, wait, I did this because I didn't want to be in DevOps and now I'm a different
- 31:16kind of DevOps.
- 31:17Yeah.
- 31:18Yeah.
- 31:18So, yeah, that.
- 31:20Python, that package, the power tools is, is does a lot of like heavy lifting for you.
- 31:27at PyCon, there was a talk on serverless that, they, the way they described the power
- 31:34tools package was it, they had said it like codified your, your serverless best practices.
- 31:39And, and it's really true.
- 31:41They give a lot, there's like so many different tools in there and there's a, a logger,
- 31:45like a structured logger, that works really well with Lambda and you, you don't even have
- 31:50to use like the AWS, login services.
- 31:54If you want to use like, you know, data dog or, or Splunk or something else, you, it's just
- 31:59a structured logger and how you aggregate them.
- 32:01It's like up to you and you can even customize how you format them, but it's, works really
- 32:07well with Lambda.
- 32:07yeah.
- 32:09So that's.
- 32:09You probably could actually capture exceptions and stuff with something like century even.
- 32:13Right.
- 32:14Oh yeah.
- 32:14Python code.
- 32:15There's no reason you couldn't.
- 32:16Right.
- 32:16Exactly.
- 32:17Yep.
- 32:18yeah, some of that comes into, you know, packaging up those, those libraries for
- 32:23that.
- 32:23you know, you, you do have to think of some of that stuff, but like, yeah, yeah.
- 32:29Data dog, for instance, they, they provide something called like a Lambda layer or a Lambda extension,
- 32:34which is another way to package code up.
- 32:37That just makes it a little bit easier.
- 32:38So, yeah, there's a lot of, a lot of different ways to, to attack some of these problems.
- 32:43A lot of that stuff, even though they have nice libraries for them, it's really just calling
- 32:47a HTTP endpoint and you could go, okay, we need something really light.
- 32:51I don't know if requests is already included or, but there's some gotta be some kind of HTTP
- 32:54thing already included.
- 32:55We're just going to directly call it.
- 32:57Not sure.
- 32:58Yeah.
- 32:58All these packages.
- 32:59Yeah.
- 32:59Yep.
- 33:00Yeah.
- 33:00Yeah.
- 33:01This code looks nice.
- 33:02This, this power tools code, it looks like well-written Python code.
- 33:07It's they, they do some really amazing stuff and they, they bring in a Pydantic too.
- 33:13So, yeah, like, being mostly in serverless, I I've never really gotten to use like FastAPI,
- 33:20right.
- 33:20And leverage Pydantic as much, but with, power tools, you really can.
- 33:25So, so they'll package up, Pydantic for you.
- 33:28And so you can actually, yeah, you can have Pydantic models for, you know, validation
- 33:35function on these.
- 33:36It's like a, a Lambda function, for instance, it always receives an event.
- 33:41There's always like two arguments to the fun, to the handler function.
- 33:43It's event and context.
- 33:45And like event is always a, it's a, a dictionary in Python.
- 33:50And so they, they can always look different.
- 33:53And so, yeah.
- 33:55Yeah.
- 33:56So, cause the event.
- 33:57Yeah.
- 33:58so if you look in the power tools, GitHub their, their tests, they have like,
- 34:04uh, okay, here's what a, an event.
- 34:07API gateway proxy event dot JSON or whatever.
- 34:11Right.
- 34:11Yes.
- 34:12Yeah.
- 34:12So they have, yeah.
- 34:14Examples.
- 34:14Yes.
- 34:15Yeah.
- 34:16So like, you don't want to parse that out by, by yourself.
- 34:19you know, so they, they have, Pydantic models or they, they might actually just be
- 34:25Python data classes, but, that you can, you can say like, okay, yeah, this function
- 34:31is going to be for, yeah.
- 34:32An API gateway proxy event, or it's going to be an S3 event or whatever it is.
- 34:38You know, there's, there's so many different ways to receive events, from different AWS
- 34:42services.
- 34:43So, so yeah, power tools kind of gives you some, some nice validation.
- 34:47And yeah, you might just say like, okay, yeah, the body of this event, even though I,
- 34:51I don't care about all this other stuff, that they include the path headers,
- 34:58queer string parameters, but I just need like the body of this.
- 35:01So you just say, okay, event dot body, and you can even use, you can validate that further.
- 35:06The event body is going to be a Pydantic model that you created.
- 35:10So yeah, there's a lot of different pieces in here.
- 35:12If I was working on this and it didn't already have Pydantic models, I would take this and go
- 35:17to Jason Pydantic.
- 35:19Oh, I didn't even know this existed.
- 35:21That's where it's.
- 35:21Okay.
- 35:22Boom.
- 35:22Put that right in there.
- 35:23And boom, there you go.
- 35:25It parses it onto a nested tree, object tree of, of the model.
- 35:30Very nice.
- 35:30But if they already give it to you, they already give it to you.
- 35:32Yeah.
- 35:32Yeah.
- 35:33Just take what they give you.
- 35:34Yeah.
- 35:35Those specific events might be data classes instead of Pydantic.
- 35:38just because you don't, that way you don't have to package Pydantic up in your Lambda.
- 35:43But, yeah, if you're already, figuring out a way to package power tools, you're, you're,
- 35:48you're, you're close enough that you probably just include Pydantic too.
- 35:51But yeah, yeah, it's, and they also, I think they just added this feature where
- 35:58it'll actually generate, open API schema for you.
- 36:02kind of, I think, yeah, fast, FastAPI does that as well.
- 36:04Right.
- 36:05So, yeah, so that's something you can leverage power tools to do now as well.
- 36:10Oh, excellent.
- 36:11And then you can actually take the open API schema and generate a Python client board on
- 36:15top of that.
- 36:16I think with, yeah, yeah.
- 36:17So you just say it's robots all the way down.
- 36:20Right.
- 36:20Turtles all the way down.
- 36:22Yeah.
- 36:22Yeah.
- 36:23Yeah.
- 36:23yeah.
- 36:25I, I haven't used those open API generated clients, very much.
- 36:30I, I was always like skeptical of them, but, yeah.
- 36:32In theory.
- 36:34I just feel heartless or, you know, soulless, I guess the word looks more.
- 36:37And he's just like, okay, here's another star org, star, star KW orgs thing where it's like,
- 36:42couldn't you just like write it, make some reasonable defaults and give me some keyword
- 36:46argue, you know, just like, yeah, but if it's better than nothing, you know, it's better than
- 36:50right.
- 36:50Yeah.
- 36:50Yeah.
- 36:51So, but yeah, you, you can see like power tools.
- 36:55They, they took a lot of influence from FastAPI and it does seem like it.
- 36:59Yeah, for sure.
- 37:00Yeah.
- 37:00Yeah.
- 37:00Oh, it's, it's definitely really powerful.
- 37:03And you get some of those same benefits.
- 37:05Yeah.
- 37:05This is new to me.
- 37:06It looks, it looks quite nice.
- 37:07So, another comment by Kim is tended to use serverless functions for either things that
- 37:12run briefly, like once a month on a schedule or the code that processes stuff coming in on
- 37:17an AWS SQS simple queuing service queue of unknown schedule.
- 37:23So maybe that's an interesting segue into how do you call your serverless code?
- 37:29Yeah.
- 37:29Yeah.
- 37:30So, as we kind of touched on, there's a lot of different ways from like, you know,
- 37:34AWS for instance, to do it.
- 37:36So, yeah, like AWS Lambda has like Lambda function URLs, but I haven't used those as much,
- 37:43but if you just look at like the different options and, and like power tools, for instance,
- 37:47you can, you can have a, a load balancer that's gonna, where you set the endpoint to invoke
- 37:53a Lambda, you can have, API gateway, which is another service they have.
- 37:59so there's a lot of different ways.
- 38:02Yeah.
- 38:02So, that's kind of almost getting into like a way of like streaming or an asynchronous
- 38:10way of processing data.
- 38:11So, yeah, maybe, in AWS, you're using a queue, right?
- 38:17That's, filling up and you say like, okay, yeah, every time this queue is at this size or,
- 38:22or this timeframe, invoke this Lambda and process all these messages.
- 38:27So there's a lot of different ways to, to, to invoke a Lambda function.
- 38:33So, if it's, I mean, really as simple as, you can invoke them like from the AWS CLI
- 38:40or, but yeah, most people are probably have some kind of API around it.
- 38:44Yeah.
- 38:44Yeah.
- 38:45Almost make them look like just HTTP endpoints.
- 38:47Right.
- 38:48Yeah.
- 38:48Yeah.
- 38:49Mark out there says not heard talk of ECS.
- 38:53I don't think, but I've been running web services using Fargate serverless tasks on ECS for years
- 38:59now.
- 38:59Are you familiar with this?
- 39:01I haven't done it.
- 39:01I, yeah, I, I'm like vaguely familiar with it, but yeah, this is like a, a serverless,
- 39:08yeah.
- 39:08Serverless compute for containers.
- 39:10So, I haven't used this personally, but yeah, very like similar concept where it
- 39:18kind of scales up for you and, yeah, you don't have to have things running all
- 39:22the time, but yeah, it can be dockerized applications.
- 39:25yeah, in fact, the company I work for now, they do this with their Ruby on rails applications.
- 39:29They, they dockerize them and run, with, with Fargate.
- 39:34so, Docker creating Docker containers of these things, the less familiar you are with
- 39:41running that tech stack, the better it is in Docker.
- 39:44You know what I mean?
- 39:45Yeah.
- 39:45Yeah.
- 39:46Like I could run straight Python, but if it's Ruby on rails or PHP, maybe it's going
- 39:50into a container that would make me feel a little bit better about it.
- 39:53Yeah.
- 39:53Especially if you're in that workflow of like handing something over to a DevOps team, right?
- 39:58Like you, you can say like, here, here's an image or a container or Docker file that,
- 40:02you know, that will work for you.
- 40:04You know, that's maybe a little bit easier than, trying to explain how to set up an environment
- 40:10or something.
- 40:10So, yeah.
- 40:11Yeah.
- 40:12Yeah.
- 40:12Fargate's a really good serverless option too.
- 40:15Excellent.
- 40:16What about performance?
- 40:17You know, you talked about having like a whole API apps, like FastAPI or flask or whatever.
- 40:23Yeah.
- 40:23Start up those apps can be somewhat, can be non-trivial basically.
- 40:27And so then on the other side, we've got databases and stuff.
- 40:31And one of the bits of magic of databases is the connection pooling that happens, right?
- 40:36So the first connection might take 500 milliseconds, but the next one takes one.
- 40:40It's already open effectively, right?
- 40:42Yeah.
- 40:43Yeah.
- 40:43That's definitely something you really have to take into consideration is like how, how
- 40:48much you can do.
- 40:49That's where some, some of that like observability, some of like the tracing that you can do
- 40:53and profiling is, is really powerful.
- 40:56Yeah.
- 40:57Yeah.
- 40:57AWS Lambda, for instance, they have, they have something called cold starts.
- 41:02So like, yeah.
- 41:05So the first time like a Lambda gets invoked or maybe you have, you know, 10 Lambdas that
- 41:12get called at the same time, that's going to, you know, invoke 10 separate Lambda functions.
- 41:17So that's like great for the scale, right?
- 41:19That's really nice, but on a cold start, it's usually a little bit slower invocation because
- 41:26it has to initialize.
- 41:27Like I think what's happening, you know, behind the scenes is they're like, they're moving your
- 41:33code over.
- 41:34That's going to get executed.
- 41:35And anything that happens like outside of your handler function.
- 41:40So importing libraries sometimes you're establishing a database connection.
- 41:47Maybe you're, you know, loading some environment variables or, or some secrets.
- 41:52And so, yeah, there's definitely, performance is something to consider.
- 41:57I, yeah, that's probably, you, you mentioned rust.
- 42:01yeah, there's probably some more performant, like run times for some of these serverless functions.
- 42:06So, I've even heard some people say, okay, for, for like client facing things, we're, we're
- 42:14not going to use serverless.
- 42:15Like we just want that performance.
- 42:17So, so that cold start definitely can, that can have an impact on you.
- 42:21Yeah.
- 42:23Yeah.
- 42:23On both ends that I've pointed out, like the app start, but also the service, the database
- 42:28stuff with like the connection.
- 42:29Right.
- 42:29Yeah.
- 42:30So yeah.
- 42:31Relational databases too.
- 42:32That's an interesting thing.
- 42:34with, yeah.
- 42:34What do you guys do?
- 42:35You mentioned Dynamo already.
- 42:36Yeah.
- 42:37So Dynamo really performant for, a lot of connections.
- 42:41Right.
- 42:42But a, so Dynamo is a, you know, serverless database that can, that can scale.
- 42:47You can query it over and over and that's not going to, it doesn't reuse a connection
- 42:52in the same way that like a SQL database would.
- 42:56so that's, that's an excellent option, but if you do have to connect to a relational
- 43:02database and you have a lot of invocations, you can, you can use a, like a proxy,
- 43:09if, if you're all in on AWS.
- 43:11And so again, sorry for this is really AWS heavy, but, but if you're using their like
- 43:16relational database service, RDS, you can use RDS proxy, which will use like a pool of connections
- 43:22for your Lambda function.
- 43:24Oh, that can, yeah.
- 43:25That can give you a lot of, performance or, at least you won't be, you know, running
- 43:33out of connections to your database.
- 43:34So, another thing too, is just how you structure that connection.
- 43:39So, I mentioned cold Lambdas, you obviously have warm Lambdas too.
- 43:43So, a Lambda has its, its handler function.
- 43:47And so anything outside of the handler function can get reused on a, on a, on a warm Lambda.
- 43:52So you can establish the connection to a database and it'll get reused on every invocation that
- 43:58it, that it can.
- 43:58That's cool.
- 43:59Do you have to do anything explicit to make it do that?
- 44:02Or is that just, it just has to be outside of that handler function.
- 44:06So, you know, kind of at your, your top level of your, your file.
- 44:10So.
- 44:10Yeah.
- 44:11Excellent.
- 44:12Yeah.
- 44:12It makes me think almost one thing you would consider is like profiling the import statement
- 44:18almost.
- 44:18Right.
- 44:19And yeah, that's what we normally do, but there's a library called import profiler that actually
- 44:24lets you time how long different, different things take to import.
- 44:27It could take a while, especially if you come from, not from a native Python way of thinking
- 44:33in like C# or C++ or something you say hash include or using such and such
- 44:40like that's a compiler type thing that really has no cost.
- 44:44Yeah.
- 44:44There's code execution.
- 44:46And when you import something in Python and some of these can take a while, right?
- 44:49Yes.
- 44:50Yeah.
- 44:51So, there's a lot of tools for that.
- 44:53There's some, I think even maybe specific for Lambda.
- 44:55I don't like data dog has a profiler that gives you like this, I forget what the graphic
- 45:01is called, like a flame, a flame graph.
- 45:03Yeah.
- 45:04That'll give you like a flame graph and show like, okay, yeah, it took this long to,
- 45:09make your database connection this long to import Pydantic.
- 45:12And it took this long to, make a, a call to DynamoDB, you know, so you actually
- 45:19kind of like break that up.
- 45:21AWS has x-ray, I think, which does something similar to, so, yeah, it's definitely something
- 45:27to consider.
- 45:27another, just what you're packaging, is definitely, something to, to watch
- 45:34for.
- 45:35And so, I mentioned, yeah, I mentioned using pants to package Lambdas and they, they
- 45:42do, hopefully I don't butcher how this works behind the scenes, but, they're using,
- 45:48they're using Rust and they'll actually kind of like infer your dependencies for you.
- 45:52And so they have a, an integration with AWS Lambda.
- 45:58They also have it for Google cloud function.
- 46:00So yeah, it'll go through.
- 46:02You say, here's like my, AWS Lambda function.
- 46:05This is the file for it and the function that needs to be called.
- 46:09And it's going to create a zip file for you, that has your, your Lambda code in it.
- 46:15And it's going to find all those dependencies you need.
- 46:17So it'll actually, you know, by default, it's going to include like, you know, Bodo that you
- 46:23need.
- 46:23If, if you're using Bodo, it'll, if you're going to use, you know, pie, my SQL or whatever
- 46:30library, it's going to pull all those in and zip that up for you.
- 46:34And so if you just like open up that zip and you see, and especially if you're,
- 46:40sharing code across your code base, maybe you have a shared function to, to make some of these
- 46:45database connections or calls.
- 46:46Like, you, you see everything that's going to go in there.
- 46:50And so, yeah.
- 46:52And so how, how like pants does it is it's, it's file-based.
- 46:55So, sometimes just for like ease of imports, you might throw a lot of stuff in like your,
- 47:00um, your init.py file and say like, okay, yeah.
- 47:03From, you know, you add all kind of bubble up all your things that you want to import in
- 47:08there.
- 47:09well, if one of those imports, is also using, open CV and you don't need
- 47:17that, then pants is going to say like, oh, he's importing this.
- 47:21And because it's file-based now this Lambda needs open CV, which is a, you know, massive
- 47:27package.
- 47:28That's going to, it's going to impact your, your performance, especially in those cold
- 47:33starts because that, that code has to be moved over.
- 47:36So yeah, that's, that's pretty interesting.
- 47:38So kind of an alternative to saying, here's my requirements or my pyproject.toml, my lock
- 47:45file or whatever, that just lists everything the entire program might use.
- 47:48This could say, you're going to import this function.
- 47:51And to do that, it imports these things, which import those things.
- 47:54And then, and then it just says, okay, that means here's what you need.
- 47:57Right.
- 47:57Right.
- 47:57Yeah.
- 47:58Yeah.
- 47:59It's definitely one of like the best ways that I found to, to package up,
- 48:03Lambda functions.
- 48:04I think some of the other tooling might do some of this too, but yeah, a lot of times
- 48:08it, it would require like requirements that TXT, but if you have like a large code
- 48:13base to where maybe you do have this, this shared, you know, module for, that, you
- 48:19know, maybe you have, you know, 30 different Lambda functions that are all going to use
- 48:23some kind of helper function.
- 48:24It's just going to go in, like grab that.
- 48:27And it doesn't have to be like pip installable pants is smart enough to just be like, okay,
- 48:30it needs this code.
- 48:31And so, but yeah.
- 48:33You definitely have to be careful.
- 48:34Yeah.
- 48:35Yeah.
- 48:35And then there's so many other cool things that, that pants is doing that, you know,
- 48:39they have some, really nice stuff for testing and, linting and formatting.
- 48:43And it's, yeah, there's a lot of really good stuff that they're doing.
- 48:48Yeah.
- 48:49I had Benji on the show to talk about pants.
- 48:51That was, that was fun.
- 48:52Yeah.
- 48:53So let me go back to this picture.
- 48:55Is this the picture?
- 48:56I have a lot of things open on my screen now there.
- 49:00So on my server setup that I described, which is a bunch of Docker containers running on one
- 49:05big machine.
- 49:06I can go in there and I can say, tail this log and see all the traffic to all the different
- 49:11containers.
- 49:11I can tell another log and just see like the logging log book, log guru, whatever output
- 49:17of that, or just the web traffic.
- 49:19Like there's different ways to just go, I'm just going to sit back and look at it for
- 49:22a minute to make sure it's chilling.
- 49:24Right.
- 49:24If everything's so transient, not so easy in the same way.
- 49:28So what do you do?
- 49:29Yeah.
- 49:30So, yeah, power tools does, they have their, their structured logger that helps a lot, but
- 49:37yeah, you have to kind of like aggregate these logs somewhere.
- 49:39Right.
- 49:39Because yeah, you can't, you know, a Lambda function, you can't like SSH into.
- 49:44Right.
- 49:45So, yeah, you can't have to make too long.
- 49:47Yeah.
- 49:48Yeah.
- 49:48so yeah, you, you need to have some way to aggregate these.
- 49:54So like AWS has cloud watch where, that will like by default kind of log all of your
- 50:00standard out.
- 50:00So even like a print statement would go to, cloud watch, just by, by default.
- 50:06but you probably want to like structure these better with, most likely in, in, you
- 50:11know, JSON format, just most tooling around those is going to help you.
- 50:16So, yeah, the power tool structured logger is, is really good.
- 50:20And you can, you can, even like you can have like a single log statement, but you can
- 50:26append different keys to it.
- 50:28And, it's, it's pretty powerful.
- 50:31especially cause you, you don't want to like, I think like, so if you just like
- 50:36printed something in a Lambda function, for instance, that's going to be like a different
- 50:40row on each of your, like, by like the default cloud watch, like it'll be, I, how
- 50:46it breaks it up is really odd unless you have some kind of structure to them.
- 50:50Okay.
- 50:51And so, yeah, so definitely something to, to consider.
- 50:54something else you can do is, yeah, there's, there's metrics you can do.
- 51:00so like how it works with like cloud watch, they have a specific format.
- 51:04And if you use that format, you can, it'll automatically pull that in as a metric
- 51:11and like data dog has something, similar where you can actually kind of like go in there.
- 51:15You can look at your logs and say, like, find a value and be like, I want this to be a metric
- 51:20now.
- 51:20And so that's really powerful.
- 51:23Oh, the metric sounds cool.
- 51:24So I see logging and racing.
- 51:27What's the difference between those things?
- 51:29Like tracing as a level and just a high level of logging.
- 51:33Yeah.
- 51:34Tracing, and hopefully I do do the justice, differentiating too.
- 51:40I, I feel like tracing does have a lot more to do with your, your performance
- 51:45or, maybe even closer to like tracking some of these metrics.
- 51:49Right.
- 51:49I've used the data dog tracer a lot.
- 51:55and I've used the AWS, like X-ray, they're tracing utility a little bit too.
- 52:01And so like those might, those will show you.
- 52:04So like, maybe you are reaching out to a database, writing to S3.
- 52:08APM, application performance monitoring, where it says, yes, you spent this much time in a
- 52:14SQL query in this much time in.
- 52:17Identic serialization.
- 52:17Whereas the login would say a user has been sent a message.
- 52:22Right.
- 52:23Exactly.
- 52:23Yeah.
- 52:24Yeah.
- 52:24Yep.
- 52:24Tracing definitely is probably more around your, your performance and yeah.
- 52:28Things like that.
- 52:28I see.
- 52:29It's kind of insane that they can do that.
- 52:31You know, you, you, you see it in the Django debug tool bar and the pyramid debug tool bar,
- 52:36but they'll be like, here's your code and here's all your SQL queries.
- 52:38And here's how long they took.
- 52:39And you're just like, wow, that thing is reaching deep down in there.
- 52:42You know, the data dog one is very interesting because like it, it just knows like that this
- 52:48is a SQL connection and it tells you like, oh, okay.
- 52:51This SQL connection took this long.
- 52:52And it was like, I didn't tell it to even trace that.
- 52:55Like it just like, it, it knows really well.
- 52:58Yeah.
- 52:58So like the connections open is another to say, and here's what it sent over SSL, by the way.
- 53:04How'd you get in there?
- 53:05Yeah.
- 53:06Yeah.
- 53:06So it's in process.
- 53:08So it can do a lot.
- 53:09It is impressive to see those things that work.
- 53:12All right.
- 53:12So that's probably what the tracing is about, right?
- 53:14Yes.
- 53:15Yeah.
- 53:15Yeah.
- 53:15Definitely.
- 53:16Probably more around performance.
- 53:17You can put some different things in tracing too.
- 53:20Like I've, I've used it to say, like we talked about those like database connections to say
- 53:25like, oh yeah, I, this is a, this is reusing a connection here.
- 53:29Cause I was, I was trying to like debug some stuff on, am I creating a connection too many
- 53:33times?
- 53:33So I don't want to be, so yeah, you can, you can put some other useful things in tracing
- 53:38as well.
- 53:38But yeah.
- 53:39And pat out the audience.
- 53:40Oops.
- 53:41Move around.
- 53:41When you're using many microservices, like single execution involves many services.
- 53:46Basically it's hard to follow the logs between the services and tracing helps.
- 53:50Tie that together.
- 53:51Yeah.
- 53:52Yeah.
- 53:52That's for sure.
- 53:53All right.
- 53:54Let's close this out, Tony, with one more thing that I'm not sure how constructive it
- 53:59can be.
- 53:59There probably is some ways, but testing, right?
- 54:02Yeah.
- 54:02Yeah.
- 54:03That's definitely.
- 54:05If you could set up your own Lambda cluster, you might just run that for yourself.
- 54:09Right.
- 54:10So yeah.
- 54:11How do you do this?
- 54:12Right.
- 54:12Yeah.
- 54:13To some extent you can write like, like there's, there's a Lambda Docker image that you could run
- 54:17locally and you can do that.
- 54:19But if, if your Lambda is reaching out to DynamoDB, I guess there's, there's technically
- 54:24a DynamoDB container as well.
- 54:27Like, like you could, it's a lot of overhead to set this up, but rather than just doing like,
- 54:32you know, flask start or, you know, whatever the command is to like spin
- 54:37up a flask.
- 54:38I pressed the go button in my IDE and now it's.
- 54:41Yeah.
- 54:41Yeah.
- 54:42Yeah.
- 54:42So that's definitely, and there's more and more tooling coming out, you know,
- 54:47that's, that's coming out for this kind of stuff.
- 54:49But, if you can like unit tests, there's no reason you can't just like, you know,
- 54:55run, run unit tests, locally.
- 54:58But when you start getting into the integration test, you're probably getting to the point where
- 55:03maybe you just deploy to actual services.
- 55:08and you know, it's, it's always trade-offs, right?
- 55:11Like there's, there's costs associated with it.
- 55:13There's the overhead of like, okay, how can I deploy to an isolated environment, but maybe
- 55:19it interacts with another microservice.
- 55:20So, yeah, so there's, there's definitely trade-offs, but.
- 55:25I can see that you might come up with, like a QA environment, almost like a mirror image
- 55:31that doesn't share any data, but it's sufficiently close, but then you're running, I mean, that's
- 55:37a pretty big commitment because you're running a whole replica of whatever you have.
- 55:41Right.
- 55:41Yeah.
- 55:42And so, yeah, QA environments are great.
- 55:45but you might even want lower than QA.
- 55:48You might want to have a, dev or like a, one place I worked at, we would
- 55:56spin up an entire environment for every PR.
- 55:59So, you could actually, yeah, like when you created a PR, that environment got spun
- 56:05up and it ran your integration tests and system tests against that environment, which, you
- 56:10know, simulated, your prod environment a little bit better than running locally on your
- 56:15machine.
- 56:16So, certainly a challenge, to, to test this.
- 56:20And yeah.
- 56:21And there's always these like one-off things too.
- 56:24Right.
- 56:24Like, you can't really simulate like that memory limitation of a Lambda locally,
- 56:30you know, as much as when you deploy it and things like that.
- 56:33So.
- 56:33Yeah.
- 56:34Yeah.
- 56:34That would be much, much harder.
- 56:36I, maybe you could run a Docker container and put a memory limit on it.
- 56:40You know, that might work.
- 56:41Yeah.
- 56:42Yeah.
- 56:42Maybe you're back into like more and more DevOps to avoid DevOps.
- 56:46Right.
- 56:46Yeah.
- 56:47Yeah.
- 56:48So there it goes.
- 56:49But interesting.
- 56:50All right.
- 56:51Well, anything else you want to add to this conversation before we wrap it up about out of
- 56:55time here?
- 56:55Yeah.
- 56:56I guess, I don't know if I have it.
- 56:58Hopefully we covered enough.
- 56:59there's just a lot of like good.
- 57:01Yeah.
- 57:02There's a lot of good resources.
- 57:03The, the tooling that I've mentioned, like power tools and pants, just amazing communities
- 57:09like power tools has a discord and go on there and ask for help.
- 57:13And they're super helpful.
- 57:14pants has a Slack channel.
- 57:16You can join their Slack and ask, you know, about things.
- 57:19And, and so those two communities have been really good and really helpful in this.
- 57:24A lot of good, talks that are available on YouTube too.
- 57:27So just, yeah, there's definitely resources out there and, that a lot of people have,
- 57:31you know, fought this for a while.
- 57:32So yeah, excellent.
- 57:34And you don't have to start from just create function and start typing.
- 57:38Yeah.
- 57:38Yeah.
- 57:39Cool.
- 57:39All right.
- 57:40Well, before you get out of here though, let's, let's get your recommendation for a PI PI package,
- 57:45something, something fun.
- 57:47I probably, you know, we've talked a lot about it, but power tools is definitely one,
- 57:53uh, that is like every day getting used for me.
- 57:56So the, yeah, power tools for Lambda and Python,
- 58:00they actually support other, other, languages too.
- 58:03So they have like the same functionality for like, you know, no JS, you know, for like
- 58:08typescript and.net.
- 58:09And so, yeah, but this one definitely, leveraging power tools and, Pydantic together
- 58:17just really made like, serverless, a lot of fun to, to write.
- 58:22So, yeah, definitely doing great things there.
- 58:25Excellent.
- 58:25Well, I'll put all those things in the show notes and it's been great to talk to you.
- 58:29Thanks for sharing your journey down the serverless path.
- 58:33Yeah.
- 58:34Yep.
- 58:34Thanks.
- 58:34Thanks for having me.
- 58:35You bet.
- 58:36Yeah.
- 58:36Enjoy chatting.
- 58:37Same.
- 58:37Bye.
- 58:38That's it.
- 58:38This has been another episode of talk Python to me.
- 58:42Thank you to our sponsors.
- 58:44Be sure to check out what they're offering.
- 58:45It really helps support the show.
- 58:47Take some stress out of your life.
- 58:49Get notified immediately about errors and performance issues in your web or mobile applications with
- 58:55Sentry.
- 58:55Just visit talkpython.fm/sentry and get started for free and be sure to use the promo
- 59:01code talkpython, all one word.
- 59:03Mailtrap, an email delivery platform that developers love.
- 59:07Try for free at mailtrap.io.
- 59:11Want to level up your Python?
- 59:12We have one of the largest catalogs of Python video courses over at Talk Python.
- 59:17Our content ranges from true beginners to deeply advanced topics like memory and async.
- 59:22And best of all, there's not a subscription in sight.
- 59:25Check it out for yourself at training.talkpython.fm.
- 59:27Be sure to subscribe to the show.
- 59:29Open your favorite podcast app and search for Python.
- 59:32We should be right at the top.
- 59:33You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 59:39and the direct RSS feed at /rss on talkpython.fm.
- 59:43We're live streaming most of our recordings these days.
- 59:46If you want to be part of the show and have your comments featured on the air,
- 59:49be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 59:54This is your host, Michael Kennedy.
- 59:56Thanks so much for listening.
- 59:57I really appreciate it.
- 59:58Now get out there and write some Python code.
- 1:00:00We'll see you next time.
- 1:00:01Bye.
- 1:00:01Bye.
- 1:00:01Bye.
- 1:00:01Bye.
- 1:00:01Bye.
- 1:00:02Bye.
- 1:00:02Bye.
- 1:00:02Bye.
- 1:00:02Bye.
- 1:00:03Bye.
- 1:00:03Bye.
- 1:00:03Bye.
- 1:00:04Bye.
- 1:00:04Bye.
- 1:00:04I'll see you next time.