تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Creating LambdaFunction Class0:00

Let's deploy a node function to Lambda. I'm going to start by creating a new directory called sidecar in my app directory. In there I'll place a new class called, we'll just call this one HelloWorld. All of our functions need to extend the LambdaFunction class. LambdaFunction is a class that ships with sidecar and it's got all sorts of methods in it, which we'll cover as we go, but it serves as the base for every single function. You can see that our editor is complaining that we're missing some stuff, so we'll let it fill out the method steps. The two that are required are handler and package. We're going to start with the second one, package.

Defining Package Files0:39

The two that are required are handler and package. We're going to start with the second one, the package. The package is just the set of files that you want deployed. Your function fundamentally is a set of files. It has at least one file that runs your code, but it probably has a bunch of other supporting files. For this hello world we're just going to ship one file and that's going to be sidecar/index.js. We are doing a node function, so we need to ship a JavaScript file. Let's come out to our base path and create a new directory called sidecar. In there we'll put a new file called index.js.

Configuring the Handler1:10

Let's come out to our base path and create a new directory called sidecar. In there we'll put a new file called index.js. This is the file that we'll be shipping to Lambda. The next thing that's required is this handler method. The handler is just a pointer. It tells AWS exactly which method you want to execute when a new event comes in or a new request comes in. When your function is being run, what method should be run? The way that you do this is you have to point it to a file, sidecar/index, and we don't put the .js here because this is a node function.

The way that you do this is you have to point it to a file, index.js, and we don't put the js here because this is a node function. It has to be js, so we don't put that there. What we do put is we put the name of the method that we want Lambda to run for us. Hopping back to index.js, we now need to write that method. The way you do that is pretty straightforward. Whatever you named your handler, we named ours handler, which is pretty common, but you can name it whatever you want. You put it on the global exports object. Because we configured it to be handler, this is the function that AWS will run.

You put it on the global exports object. Because we configured it to be handler, this is the function that AWS will run. For now, we're just going to return hello world to make sure we've set everything up correctly. You'll notice we do have an event parameter. We're not going to use that right now, but we will in a minute. That's it. That is the entirety of a Lambda function. We're going to add one more piece that's not strictly necessary, but I sometimes like to add it.

Setting the Runtime2:37

We're going to add one more piece that's not strictly necessary, but I sometimes like to add it. We're going to add the runtime. The reason it's not necessary is because by default, the runtime is node 14. If we look in the base class, you'll see that all functions are node 14 by default. I do like to sometimes put that there just so that it's explicit. You can check the runtime class and see all of the runtimes that Amazon supports. We have constants for each one so that your editor will help you out here. We're going to put node 14 just because that helps me remember, even though it's not totally necessary at this point.

Deploying and Activating3:12

We're going to put node 14 just because that helps me remember, even though it's not totally necessary at this point. The last thing we need to do is we need to go back to our configuration file. Remember this functions array at the very top? This is the one that tells Sidecar which functions to manage on our behalf. We're just going to plop hello world right in there. Now Sidecar knows, okay, I have to worry about this hello world class. We're good to go, so let's deploy. In our app directory, we run php artisan sidecar deploy. What's going to happen here is Sidecar will package our function into a zip file, put

In our app directory, we run php artisan sidecar deploy. What's going to happen here is Sidecar will package our function into a zip file, put it in the S3 bucket, create a new function, and deploy our code into that function. You can see it all happened. Created a new function, it packaged the files, new zip file, zip file created. After you deploy a function, you do need to activate it. We're going to run php artisan sidecar activate. You'll see it created an alias for version one of the function that we just deployed. Now that function is ready to be used. If we look in our AWS console in the Lambda section, so you can come up here and you type

Now that function is ready to be used. If we look in our AWS console in the Lambda section, so you can come up here and you type in Lambda, you'll see that there are no functions. Did we do something wrong? This is one of the things you always have to remember with AWS console is you need to check your region. We set ours up to be US East 2. So if we look at US East 2, we do see the function that we just deployed. It's a Node.js function. We deployed it a minute ago and we called it hello world.

Invoking from Laravel4:44

It's a Node.js function. We deployed it a minute ago and we called it helloWorld. So just make sure you're always checking that you're in the right region. Now that that function is there, let's invoke it from our Laravel application. We're going to do most of this work in web.php and we'll come down here and say return helloWorld(). That's it. You can do more stuff, but that's all that's absolutely required. And what's going to return here is an object full of stuff. What we want to return to the browser is just the body.

And what's going to return here is an object full of stuff. What we want to return to the browser is just the body. It comes with logs. It comes with totalDuration. It comes with a lot of helpers. But the thing that we're interested in right now is the body. With any luck, this should say hello world in our browser. We'll come to the browser. We still have the default Laravel start screen. We'll hit refresh.

Passing Event Data5:35

We still have the default Laravel start screen. We'll hit refresh. There we go. Hello world. And that string right there was generated in JavaScript, not in php. So the function ran on Lambda, generated the string hello world, sent it back to Laravel, and then Laravel is sending it back to the browser. I told you that there was an event parameter. Let's change this to a template string. Instead of saying hello world, let's say hello event.name.

Let's change this to a template string. Instead of saying hello world, let's say hello event.name. And instead of executing it with no data, we can pass data through to our function. We're also going to save Laracast to the dictionary. Whatever you pass through to execute will end up as the event on the Lambda side. You can pass through anything that you want and then just pull it off of the event object. Let's deploy this and see what happens. We'll run artisan side card deploy again. Now that's been updated, let's check in the browser. But weirdly, we're still seeing hello world.

Now that's been updated, let's check in the browser. But weirdly, we're still seeing hello world. And the reason is we didn't activate the function. Remember, there's two steps. The first is deploy and the second is activate. So you need to activate the new function so that AWS will use your most recent code. From here on out, we'll likely be doing that all in one step. So we'll say deploy --activate. And that runs it all as one command. You can see that the package didn't change.

And that runs it all as one command. You can see that the package didn't change. So it didn't create a new zip file. And then it activated it as well. Hitting it from the browser, we now see hello Laracasts. And the fun part about this is we don't actually have to change our function handler to make it more dynamic. So we can say hello Jeffrey. Now we have a dynamic handler because we're driving it with data from the Laravel side. So depending on how you write your handler, you can make it super dynamic and then just

Now we have a dynamic handler because we're driving it with data from the Laravel side. So depending on how you write your handler, you can make it super dynamic and then just send over the data from the php side.

Deploy Node Functions

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟