در حال بارگذاری ...

Creating the Vapor Project0:00

So, right now we have our AWS account linked with Vapor. It's time we create our first project. I already have a fresh Laravel application that we will deploy to Vapor. This application has the basic welcome route, a simple queue job, and a scheduled command. We will deploy to Vapor and then make sure that all these parts work. So, let's get started. We will click on create project in our Vapor dashboard. Vapor will ask us if we want to create a project from a fresh Laravel application or an existing one. We will choose to create a project for an existing one. We will then need to select the AWS credentials that we want to use and the region we want to host our application inside. I will keep the default US-East-1 region. Then we will enter Laracasts as the name of the project. Then we click create. Once the project is created, Vapor asks us to install the Laravel Vapor code library using Composer.

Installing Vapor Tools0:59

Then we will enter Laracasts as the name of the project. Then we click create. Once the project is created, Vapor asks us to install the Laravel Vapor code library using Composer inside our application and also install Laravel Vapor CLI as a global Composer dependency on our local machine. We will start by copying the installation command for Vapor core. Go to our terminal and run the command. Vapor core will be installed along with several dependencies that Vapor requires in order for the application to run in a serverless environment. Then we will copy the command to install Vapor CLI and run it. Now the remaining part is to log into Vapor through the Vapor CLI we just installed. So, we will go to the terminal and run vapor login. I'll provide my email address and password. And we're in. Awesome. Now the final part before we deploy is create a Vapor.yaml file with this content.

Configuring vapor.yaml2:02

terminal and run vapor login. I'll provide my email address and password. And we're in. Awesome. Now the final part before we deploy is create a vapor.yaml file with this content. So, let's copy it and go to our code editor, create the file and paste the content. This file is used by Vapor to provision our application on AWS. We can configure multiple environments and define the requirements of each one. In this project, we will stick with the default configuration of using a 1024 megabyte memory for the Lambda that will serve the web requests and 512 megabyte for the Lambda that handled CLI commands and queue drops. We will also use the default PHP 8.1 runtime. Now it's time we start our first deployment. We will go to the terminal and run vapor deploy production. Behind the scenes, Vapor is going to inject its own runtime into our application code. This is a requirement so our Laravel application can run.

Deploying to Production3:10

terminal and run vapor deploy production. Behind the scenes, Vapor is going to inject its own runtime into our application code. This is a requirement so our Laravel application can run in the serverless environment. Vapor will also package our application into a single compressed file and upload it to AWS S3. Lambda will later use this file to install our application on Lambda containers to handle web requests, console commands, and queue drops. Also, Vapor will configure several AWS services like EventBridge, SQS, API Gateway, and DynamoDB behind the scenes. All these services will work together to run our application. Now that the deployment is done, Vapor shows an environment URL that we may use to test our application. So let's go ahead and click on that link and here we go. Our Laravel application is now running on AWS Lambda. We can see that it uses PHP 8.1 just as we configured it inside the

Testing Commands and Queues4:07

application. So let's go ahead and click on that link and here we go. Our Laravel application is now running on AWS Lambda. We can see that it uses PHP 8.1 just as we configured it inside the vapor.yaml file. Now we know our web part of the application is working. Let's make sure that console commands work as well. We will go to the terminal again and run vapor command production. This command instructs vapor to run a command inside our production environment. Let's run the inspire command and as you can see we don't need to add the php artisan part just the command name and here we go. Vapor ran the command on the console lambda and brought back the output. Let's dispatch a queue drop then. We will run vapor tinker production to open a tinker session. For the code we will run say hello dispatch. This is to dispatch a job called say hello. Vapor will execute this command inside AWS lambda and return the output.

For the code we will run php artisan say hello dispatch. This is to dispatch a job called sayHello. Vapor will execute this command inside AWS Lambda and return the output. Here we can see the Laravel application identified our sayHello job as app\Jobs\SayHello. To verify that the job was queued and processed let's go to the Vapor dashboard, select our project, select the environment and then click on the logs tab. We'll scroll down and click on the queue section. Vapor will redirect us to the CloudWatch console on AWS where all the logs for our application lives. If we scroll down a little bit we can see that the sayHello job ran and printed hello. What about the scheduler? Let's go to the console kernel in our application and we can see that we have the inspire command configured to run every minute.

Verifying Scheduler Runs6:15

What about the scheduler? Let's go to the ConsoleKernel in our application and we can see that we have the inspire command configured to run every minute. Let's go back to the vapor dashboard, click on the CLI logs to open CloudWatch for the CLI lambda and we can see that every minute the inspire command runs successfully. Here is another one and another invocation and that's it. We deployed our application to AWS lambda through vapor and verified that everything works. I will see you in the next video.

ServerlessAWS LambdaLaravel Vapor

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