To get started with serverless, which is a best practice, let's go through and document a little bit about serverless and microservices. And what's the point? And why is this even anything to do with DevOps? Well, to start with, a serverless and microservices all DevOps best practices. So DevOps and microservice are closely related together. And part of the reason behind this is that in the old days, let's say from 1970 to even I would say, potentially 2005, something like this, we'll call this pre-Cloud era, right? And in this era, we even people are still hyping these technologies. They're hyping these kind of like monolithic frameworks. And so this is where you hear about web frameworks in particular. And I would call this like legacy thinking, right? This is thinking that everything should be this giant monolithic. You'll see that everything has to be a relational database, RDB plus almost as well a CGI Web request model. Also templating and people are using like templates for the server itself is essentially translating Json data back and forth. So this is all like a legacy way of thinking, and it's really expensive computationally. So we know that it's low efficiency as well. And it also is difficult to debug. Debugging is difficult and also has lots of security holes. Right, so this is really, I think, the old way. The new way, so we'll just say, the modern era and I would say probably 2010. Maybe even we say 2010 to potentially even let's just say, this only cover the next 10 years. So we'll say 2030 is this DevOps world. And this DevOps world says, look, all that stuff. Hey, it got us to where we're at now. But really, there's some big problems. And one of them is that a microservice is a lot easier to maintain in that it does a specialized task. And that's really one of the reasons for a microservice. Also, the complexity is low, so this is really a key component of this. Also, the other thing that's about a lot of these frameworks is that it's almost like a cult, where you're caught into this world where everyone has to talk the same language. But a microservice like flasks, for example, really lets you get out of that cult and and think for yourself where you can use this microservice in any environment you want. So flask, for example, fits really well with potentially any cloud. You can have the same flask gap, and it can be applied in a container. Or it can be applied as maybe a microservice or or whatever it is you're building. So I think this is also a big take away here is that less is more in the DevOps world. And also that the runtime should be encapsulated with the microservice. And then further, even flask in a way is a little bit legacy, although it's a microservice in that is really not event driven. And so that's something we'll talk about as well is that a lot of the Lambda services allow you to, or Google cloud allow you to respond to an event which this world has only basically pull based right where you're in a loop. And you're just constantly just by burning down trees in the forest somewhere to get your server to run where this can just be waiting for you patiently and not doing things. So this is another, I think, often not talked about the problem with this legacy world of web frameworks that everybody's pushing. Still, surprisingly, is that this is bad for the environment, right? So if we look at the word green, this is horrible, right? We have really inefficient servers there. We know that there's the science says that climate change is real, it's not fake. And we have these really expensive servers that are just running for no reason. Where if we built an event-based architecture, the events are a much more specialized way to run. So this is another thing that's kind of not necessarily talked about enough. And even if you don't believe in climate change, still, why would you want to pay extra money when you can do an event-based architecture? So I think that's a key take away here. So now that we know the architecture of microservices DevOps and why the legacy world needs to be abandoned, let's dive into a repo here in this serverless cookbook. In a particular this cookbook has a few different things that we can run. The first one would just be this Marco Polo AWS Lambda function. And what I'm going to do here is also do a quick sketch of what the point is with certain with these serverless functions. So I talked about pulling right. And I think the best analogy that I will use that I typically talk about for what are we building is events. And again, this is directly in opposition from 1970 to 2005 where the legacy world of web frameworks. The new world is events. And the way events work is that it's much like what would happen in your house in terms of your garage. So we have a house here that's got some windows. And we have a door here. And then inside of this we have a garage. And inside the garage, let's say a light bulb like this. Now think about all the different ways that a light bulb in your garage could be triggered. Well, let's look at one. So one is the door opens, right? So this open event we could subscribe essentially this light bulb to the open event. And then all of a sudden, the light bulb turns on, right? So that's that's one way we can handle this. And just like the example of the really poor performing legacy architectures of web frameworks, well, you don't want to just leave your light on in your garage 24, 7 just to burn electricity. Why would you do that? You're going to have a high electricity bill. Instead, why don't you respond to an event which is the door opens and then the light bulb triggers, and then the light bulb maybe stays on for two minutes, right? That's a pretty elegant architecture. But that's only one of the events that you can do. The other thing you can do is you can say that you can manually turn it on, like why not? Also, whenever you want to, You just click the light on, you have to do something in the garage and then you click off, right? So that's a second kind of event. And there's even a third kind of an event that you could do, which is a timer, where, and again this works, this really is something that's very common in the cloud. Is you could say, listen from midnight to one in the morning, I want you to turn on, just so that it's like a deterrent for burglars, and I want this timer to turn on, and then it will turn off at one AM. So this is such a more powerful and more efficient approach. So server lists, is basically, let's look at all these benefits. It's more powerful. So it's got more power, so we'll say power is greater. Also it's more efficient as well, because you're only being charged for when you run it. And also it has the ability to solve more problems, right? And so it's part of a more complex potential solution that you can build. So there's many benefits to using serverless. So let's now dive, with that kind of background behind us, let's dive into building a function in Lambda. So first I'm going to log into my account here and do my auth really quick. And here we go. Let's go ahead and log in real quick. So in AWS, if you want to run a lambda function, what you can do is just go to Lambda. And, in fact, now, Lambda support containers, which is pretty awesome. So, look, you can say look, container image, which is totally awesome. We're going to do one from scratch here, and I'm going to call this Marco, Marco 2030 to represent the future of how we should be building services. I'm going to select Python 38, and then if I create a function here, the way it works is that it will give me a IDE that allows me to play around with this particular service. So let's go ahead and create this function, and it creates this version here. I'm going to click on it, and then I can go to the code section here and actually start writing out my function. So it's It's kind of similar to like a Visual Studio code development environment. The key takeaway here, though, is this event again. This is where everything happens magically. And if I test it, it's just going to send adjacent payload into this event. But if I want to do a trigger, which, if I go to configuration and I go to triggers, this is where you would set up, again, back to the light bulb analogy, right? If I want to set any of these any of these three kinds of triggers, I was setting the trigger there. So let's see how you do this. You would go here and you can say I want to create a web service trigger, or I want to have an IoT device trigger, or I want to do voice recognition trigger or I want to do event bridge, which is basically events. I could do a trigger on that. That would be a timer. There's tons of different ways I could trigger this. S3 is another one. Where you could have a bucket, you put an image into the bucket, it triggers some kind of event. And then let's say you have Amazon recognition label the image or something like that. There's lots of ways to trigger, or SQS is another one where if you wanted to build a high performance processing engine, you could use SQS, and you could just process millions of requests if you needed to. Essentially, SQS handles unlimited amount of traffic. So now that we know that, what I'm going to do is go back to my server list cookbook here, and I'm going to go to this Marco Polo Lambda and just kind of copy this out. So let's go ahead and put this first part in and put it into the code, and notice what I'm going to do here. Is I say, if the event has the word, if it has a name in it and it has Marco in it, return back Polo, otherwise return back, No. And so I'm going to go ahead and deploy this, and then I'm going to click on test. And all you do to create a test is is put in adjacent payload here to test it out. So let's go ahead and do this and we'll say, name is Marco, there we go, and we'll invoke it. And when we invoke this thing, you should see that the Polo word appears. Backs right. So pretty, pretty easy to test. And now let's do a bad request. So let's have one called Bob. And then here, if we put Bob in, Bob should get a no, this thing says no. There we go. So, pretty easy to test a Marco Polo function. And so I like to do the Marco Polo because it's just so intuitive, I have an event, it has to match. If event matches, I do one thing, If I don't, I do another thing. So what's neat about this Lambda service is that we can we can expand it and make it do even a little more magic by a couple of different things. One, we can invoke it via the command line. And so I'm going to create a second environment here, using Cloud Nine, which is similar to Github code spaces. I have one, I could either set up a new one or have one already existing. I'm going to create a new one real quick to show how to do it, and we'll just say, Lambda Central or something, where I want to play around with my Lambda functions. And by default, I believe we can just say everything here, Amazon Linux 2, we'll use Amazon Linux 2. And then we'll say next. There we go. What's nice about these cloud environments and why I mostly do my code for a particular cloud environment in their cloud shell is that they already have the tools preinstalled. I'm lazy, I don't want to do a bunch of work again, like install the SDK and all the stuff, it's already for me. And it has the right role based privileges to do these invocations and so in here, we'll just take a few seconds here and let's let this scroll. What this will allow us to do is if I go, while this is spinning up, I'm going to go back to this command, which is this Lambda invoke, and I'll show you how cool this is. So here we go. This thing should be ready in a second. Okay, so this thing is woken up now. [COUGH] So, why it's awesome, there's a lot of reasons why it's awesome. But, one of them is that you have the shell that's got AWS already installed, right? So if I wanted to list my buckets in s three, there you go, I can list my buckets. In this case though, I want to run this command, which is this, I want to invoke this lambda. I actually want to invoke this one, which is called 2030. So let's go ahead and do this one. We'll just say 2030, and in particular here. Let's look at the different things that I'm going to do. So I'm going to say payload, so I'm going to basically make a payload, and I'm going to put in the word name Marco. And then I'm going to tell this command to take capture the output of what happens here, and then just basically printed out. Now, I could just take this part out to be less out initially. Payload, lambda, invoke, MarcoPolo, function not found. MarcoPolo, That's weird, I'm not sure why it's not finding this function. I certainly seem to have maybe I'm misspelling it, let's try again. And let's see, did I spell it differently? Maybe I have different caps or something. Marco, that's why, okay, so let's go out and run this. There we go, so it ran it, and look, it creates this output and notice that there is, there we go, Polo. And if I want to say, bad out, We can put in the word Bob, and we can actually also test this out. There we go, bad out, and then it has, there we go. No, right? So, in my opinion, these lambdas are one of the coolest things you can possibly play around with, because of how much awesome things you can do. And this is again just an intentionally simple script, but imagine you have at your finger tips basically infinite concurrency. Because, these lambda functions can run basically true parallelization where python itself, the language has some real big critical flaws like you can't do true concurrency with threads. But if you use the cloud, the cloud itself can do true concurrency. So you can invoke concurrency from a command line tool, for example. So this particular function here, one of the other things we can take a look at as well, is if we go to monitor now, I can actually go to the logs. And I can look at these logs actually right here, which is pretty awesome. And I can see everything that's happening, if I go to log stream, show me all the invocations, and I can track down exactly everything that's happening. So, here we go, we've got the different statements, I believe that this is the one that I care about here. Now, notice that there's no log messages inside of here, it just shows me the invocation. So, how would I change that a little bit? In lambda, the way you would write logs is you just do a print statement, and so what I can do here this Marco 2030, let's actually tweak it and make it a little smarter. And so go here, and I'll just put a print and say print this was the row event, like that. And then, I can put in the name of the event. So this is why f- strings that we talked about, are so powerful, is that they allow you to quickly build a log statement, and then I could even put line by line some debugging. And I could say print here, and I can say this events was Marco, we'll just do a like that, and then I could do another one below here. The said print this event was not Marco, like that. So basically, it just shows me the execution path of what everything is doing. So we'll deploy this, and if I go back to my environment here, I can execute it. And instead of saying, Bob, let's put in Sally and, there we go. It will overwrite this, and we can look at this, we can see, no. And then if I go back to my CloudWatch logs here, and we refresh, we should see additional messages here. It'll take a second for them to populate, but we should see basically new messages that have more output inside of them. So this one, here we go, there we go, the event was row event, this was Sally, this event was not Marco, and we could go iterate from there, and there, and there. So that's really it in a nutshell why it's so powerful. Now, what else can we do that's cool? Another thing we can do if we go back to our serverless repo, is that there is a another thing that's pretty awesome. I guess maybe let's do this before I go away, let me grab this, and I'll just put this as a file in there as a template. And I'll just say this is a logging version, so we'll just say, create a new file, aws-lambda- [COUGH] marco-logs, there we go. Just there's another version of this. All right, let's go to this one, though, the step function. So what a step function does, is allows you to change multiple lambda together, and capture the input of one and put the output into another. And so, how do you do this? Well, the way you do this is you, I guess this link here is not working, but basically, is the way the code works is, that step by step here? I can just say, here we go, here's the first function, here is the second step function, and I just put these in together. So, I'm actually going to build this from scratch. So I'm going to go and grab these together. I'm going to go to step functions here, and let's go to step functions. And I'm going to say, create state machine, and I'm going to just do a regular step function here, and notice that, What I can do is just paste it in like this and notice that that this word starts at Marco. This is Marco Polo, and I take one function here. And then after I take the output, I go to the polo function, and then I do a finish operation, right? So I'm able to chain together something pretty complex. So let's go ahead to next year, and we'll just call this a Marco Polo, step function like this. And now for the role, because we're not calling any puppies. We don't need to do anything tricky. I'm just going to say, create new role. And then, I'm going to go ahead and create the state machine. Okay, Perfect. Now that I've got this, how do I execute it? What I can do is say, start execution, and similar in testing is I have to initialize this thing somewhere. And where I'll initialize it is by first putting inside of here an initial payload. So all want to do name here at first, and we'll see name and inside of this will say, Bob again, or in fact, actually, Let's actually leave this by default and I'm going to start this execution. And what's going to happen is that this will fail. It will first, you can see that the input goes here. But it's going to fail, because my function here isn't smart enough to handle that. Because if we go back to the Lambda that is calling notice, let's look at the code for this lambda. It does nothing, right? This Lambda code just accepts a payload like that and it if it doesn't match the word name, it just blows up. I don't handle that problem and it returns back a dictionary, which is named Polo. So in this case, it it can't work. But what's cool about this is look how I can easily spot every single execution what the state is. So from a devops perspective, the fact that I can look at my pipeline step by step and see exactly what's happening really is powerful. And again, this is so different than, let's say, a legacy Web framework that doesn't have these tooling. This is really the modern way to develop applications, so let's go ahead and fix this, so let's go ahead and create a new execution, and I'm going to put in the word name here and then I'm going to put in, Bob. So let's see what happens when we put Bob in. Let's go ahead and start this execution, and this should be a little bit different. So it got to Marco. We see what the input is. It says name. So it was at least able to accept it. And then now it failed a little bit further. So why did it fail? Well, let's look, if we scroll down here, we see that this Lambda was successful. But then it entered, the Polo Lambda. And what is the Polo lamb to do? Well, it's expecting that the this code will have the word, polo in it. If it doesn't, then basically returns back a nun value, which we don't know how to handle. So we have another problem that we need to fix, right? So it's it's stuck here. So how do I fix this? Well, let's go back to another execution and let's just fix this. Let's put in Marco. And what will happen is that it will be able to fully passed through all of these phases, because step one is we put in the word Marco. And what does it return back out and returns back Polo? This was expecting that you put in the word Polo. And if you put in the word polo, it will then return back Marco. Right? So just transpose is what goes in one function, goes out the other, and then it runs a finished operation. And we can see the finished if we go back here, is if we edit our step function here, that's just that final step, right? Which is? It Returns back. Finished. You could put in anything you wanted to do at the end, but this shows how you could build some really cool tools with incredible debugging and logging by using AWS lambda and or AWS stem function. And as I mentioned earlier, maybe as a wrap up here that what's awesome about the new lambda does is that you can build them now with docker containers. So if we go to container image, for example, and I just type in python container and I could say inter image. URL, but we could do is just browse a container and we can see here we go as a flask demo, or whatever anything from my Amazon image repository. Or I also could check in my own container and override it and build things in here. So there's a lot of new things that are developing with the Lambda functions as well. The last thing I'll show you with this is that if you go to this tab here, AWS resources, one thing that you can do is you can actually import a a function remotely. So if I go to AWS Resources here, click on this and I go to Marco 2,3,0 which is. Well, let's just grab one. Let's just grab this one. I can actually say, double click on it. And what's nice about it is I can later if I want to, tweak this code and actually add more information to it. Right? So that's the other thing that's great about this environment is I can go through here and say this is an event, put some other, updates to it. Prince, and we'll just say string, this is not a match, right? Something like that. And then what I can do here is I can actually test. I can save this and actually test it out local. So test out. My, let's just refresh this year, test this out. Local. So if I right click, I can say run local. And I can do test this whole workflow outs to say, name and we'll just say, Marco, what happens here were able to run it. Sometimes you have to kind of wake it up. The debugging takes a second to kind of wake up, but there we go. Let's see, the first time I do it, I I've noticed. Sometimes you have to just do it a couple times, and then it wakes up. Anyway, I'm not sure why this thing is not invoking. It could be, because I used Python three eight, maybe, but in general, most of the time this works, there we go. So it was I must have been waking up. So there we go. And then if I type in, Bob, here, run this. We can see that this is the other patch pathway, right? And then if I want to to deploy this, I can go through here and right click, and do a deploy. And what's awesome is that's pretty quick. And then I go back to my functions here. And here's Marco Polo 9,000. Look, I just updated it for seconds ago, and then I can test it out, go to this and test the same piece of code if I wanted to, or even look at the code you'll see the codes exact. I added that code there so really powerful development environment, so that's probably good as a stopping point for this section.