在 aws lambda 上部署 Laravel 外观问题

问题描述

我正在使用 laravel 8 和 bref 在 lambda 上部署它。制作一个 cron 作业功能后发送电子邮件。我部署的时候facade有问题

{
  "errorType": "RuntimeException","errorMessage": "A facade root has not been set.","stackTrace": [
    "#0 /var/task/app/functions/sendTestMail.PHP(11): Illuminate\\Support\\Facades\\Facade::__callStatic()","#1 /var/task/vendor/bref/bref/src/Runtime/Invoker.PHP(34): Bref\\Runtime\\FileHandlerLocator->App\\Functions\\{closure}()","#2 /var/task/vendor/bref/bref/src/Runtime/LambdaRuntime.PHP(102): Bref\\Runtime\\Invoker->invoke()","#3 /opt/bref/bootstrap.PHP(43): Bref\\Runtime\\LambdaRuntime->processNextEvent()","#4 {main}"
  ]
}

这是我的目录结构和功能sendTestMail.php

serverless.yml:

service: test
provider:
  name: aws
  # The AWS region in which to deploy (us-east-1 is the default)
  region: ap-southeast-1
  # The stage of the application,e.g. dev,production,staging… ('dev' is the default)
  stage: dev
  runtime: provided.al2

package:
  # Directories to exclude from deployment
  exclude:
    - node_modules/**
    - public/storage
    - resources/assets/**
    - storage/**
    - tests/**

functions:
  # This function runs the Laravel website/API
  web:
    handler: public/index.PHP
    timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
    layers:
      - ${bref:layer.PHP-80-fpm}
    events:
      - httpApi: "*"
  # This function lets us run artisan commands in Lambda
  artisan:
    handler: artisan
    timeout: 120 # in seconds
    layers:
      - ${bref:layer.PHP-80} # PHP
      - ${bref:layer.console} # The "console" layer
  cron:
    handler: app/functions/sendTestMail.PHP
    layers:
      - ${bref:layer.PHP-80}
    events:
      - schedule: rate(5 minutes)

plugins:
  # We need to include the Bref plugin
  - ./vendor/bref/bref

有人知道如何解决这个问题吗?顺便说一句,在部署之前如何在本地计算机上测试处理程序函数?谢谢

解决方法

你可以试试这个错误

  • php 工匠配置:缓存
  • php artisan 配置:清除
  • php artisan 缓存:清除
,

我相信这是由于在您的 web 函数中处理程序是 public/index.php 的问题。这正确地初始化了 Laravel 应用程序。你的 cron 函数处理程序是 app/functions/sendTestMail.php,所以 index.php 永远不会被调用,Laravel 内核永远不会处理请求。

我目前没有很好的解决方案,因为我觉得它违反了 Laravel 中的许多最佳实践和规则,并希望对其进行更多试验。但是我能够获取整个 index.php 内容并将其粘贴到 Lambda 正在调用的文件中的返回函数上方。

换句话说,我想如果你粘贴这个

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

use App\Models\Task;
define('LARAVEL_START',microtime(true));
if (file_exists(__DIR__.'/storage/framework/maintenance.php')) {
    require __DIR__.'/storage/framework/maintenance.php';
}
require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$kernel = $app->make(Kernel::class);

$response = tap($kernel->handle(
    $request = Request::capture()
))->send();

$kernel->terminate($request,$response);

作为 app/functions/sendTestMail.php 文件中的第一件事,它可能会起作用。取决于您在中间件中编码的内容,因为它将首先运行。

这在我的应用程序中对我有用。