Modifying Handler Output0:00
By now you should be familiar with this line here, helloWorld execute. helloWorld is our function and execute is how we invoke it on lambda. We've been invoking it like this, we've also been passing through data and invoking it like that. I want to look at some other ways that we can invoke these functions and let's modify our handler just a little bit so we can make the point more clear. Instead of the old environment variable, we'll just return event.name. I'm also going to add a sleep function. All this does is it resolves a promise after a certain number of milliseconds. It's just a way to simulate processing. Down here we'll put await sleep. We'll just sleep for 250 milliseconds. Let's check out our helloWorld function and see if we need to
Deploying and Timing Calls0:43
to simulate processing. Down here we'll put await sleep. We'll just sleep for 250 milliseconds. Let's check out our helloWorld function and see if we need to update anything here. We can drop these variables, we're not using these anymore. So it's still a node function, index.js, helloWorld. Okay let's get this deployed. php artisan sidecar activate. That's been deployed and activated. Let's go find it in the browser. I have the dev tools open so that we can see the timing on these requests and we'll just try serverless.whip. It does return Aaron in 854 milliseconds. That's the cold boot so let's do it again. So it looks like on average about 600 milliseconds. That's probably fine but if we go back to web.php and what if we want to execute a bunch of these? So instead of just one
Executing Many in Parallel4:20
Now it's taking 953 milliseconds which is way faster. 578, 596, 606. So we're in good shape here. It's running them in parallel. Remember each function is sleeping for about 250 milliseconds so they're not being run sequentially. They are being run in parallel but what the heck is going on here. We're not getting our names back. Well remember how we've been pulling the body out of the response. Instead of returning let's dump this and see what we get. We get an array full of settled results. So we have the request ID from AWS. We have the function that was called. We get this report that shows us how long it took. Let's go look at the SettledResult class. Let's go SettledResult. In here we have a whole lot of helper functions. We have an error. We
Understanding SettledResult5:12
long it took. Let's go look at the SettledResult class. Let's go settledResult. In here we have a whole lot of helper functions. We have an error. We have a function that tells us if it was an error. You can grab the raw AWS result if you want. If there was an exception in lambda we can throw an exception in php. I'll show you how to do that in the debugging section. What matters to us right now is the body method. This is the actual method that we've been calling to get the value that the handler sends back. So in our case we have this array full of settled results. We'll just call this results equals executeMany. We'll collect the results, map over them, and take the body out of it. And if we return this
Mapping Results to Bodies5:56
collect the results, map over them, and take the body out of it. And if we return this, this is what we expect to see. Each settled result that was in the array we made a collection out of it, made a higher-order map, and then pulled the body out of each settled result and returned that to the browser. This is a great way to take advantage of one of the huge benefits of lambda which is that it's massively scalable. Now we're running, I don't know, nine functions in parallel instead of sequentially. So you can imagine if you're running browser automation it's going to be much nicer to have nine, ten, a hundred browsers running at once instead of backing up behind each other waiting for each one.
