10

我正在尝试在 AWS 无服务器平台上部署我的 Laravel 应用程序,我在 laravel-mix 中使用动态导入和代码拆分来编译资产,执行此操作我按照bref 包文档中的步骤安装了所需的库按照指示,我还将我的公共目录与我的 s3 存储桶同步

npm run prod
aws s3 sync public/ s3://<bucket-name>/ --delete --exclude index.php

并将我的.env文件配置为:

MIX_ASSET_URL=https://<bucket-name>.s3.amazonaws.com
ASSET_URL=https://<bucket-name>.s3.amazonaws.com

接下来,我将blade文件配置为:

<script src="{{ asset('nits-assets/js/app.js') }}"></script>

我的webpack.mix.js文件有:

const mix = require('laravel-mix')

const webpack = require('webpack');

const ASSET_URL = process.env.ASSET_URL + "/";

mix.js('resources/js/app.js', 'public/nits-assets/js')
    .postCss('resources/sass/app.css', 'public/nits-assets/css', [
        require("tailwindcss"),
    ])
    .webpackConfig({
        output: {
            chunkFilename: 'nits-assets/chunks/[name].[contenthash].js',
            publicPath: ASSET_URL
        },
        resolve: {
            symlinks: false,
            alias: {
                NitsModels: path.resolve(__dirname, 'Models'),
            },
        },  
        plugins: [
            new webpack.DefinePlugin({
                "process.env.ASSET_PATH": JSON.stringify(ASSET_URL)
            })
        ],
    }).sourceMaps().version();

由于我将代码拆分为块,因此在获取块时遇到了麻烦,我的初始app.js文件从 s3 存储桶加载,但为了加载块,它尝试通过公共目录获取。

控制台错误

如何配置我laravel-mix从与我的公共目录同步的 s3 存储桶加载块?

编辑:

正如答案中所建议的,我更改了我的 serverless.yml ,它看起来像这样:

service: laravel

provider:
    name: aws
    # The AWS region in which to deploy (us-east-1 is the default)
    region: ap-south-1
    # The stage of the application, e.g. dev, production, staging… ('dev' is the default)
    stage: dev
    runtime: provided.al2
    environment:
      AWS_BUCKET: # environment variable for Laravel
        Ref: Storage
      iamRoleStatements:
        # Allow Lambda to read and write files in the S3 buckets
        - Effect: Allow
          Action:
            - s3:PutObject
            - s3:ListBucket
            - s3:GetObject
          Resource:
            - Fn::GetAtt: Storage.Arn # the storage bucket
            - Fn::Join: [ '', [ Fn::GetAtt: Storage.Arn, '/*' ] ] # everything in the storage bucket

resources:
  Resources:
    Storage:
      Type: AWS::S3::Bucket

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-74-fpm}
        events:
            -   httpApi: '*'
    # This function lets us run artisan commands in Lambda
    artisan:
        handler: artisan
        timeout: 120 # in seconds
        layers:
            - ${bref:layer.php-74} # PHP
            - ${bref:layer.console} # The "console" layer

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

现在我得到:

发生错误:ArtisanLambdaFunction - 属性变量的值必须是具有字符串(或简单类型)属性的对象。

编辑2:

问题在于 serverless.yml 中的 indent(tab) 配置:

environment:
  AWS_BUCKET: # environment variable for Laravel
    Ref: Storage
iamRoleStatements:
  # Allow Lambda to read and write files in the S3 buckets
  - Effect: Allow
    Action: s3:*

但现在得到单独的问题:

在此处输入图像描述

错误:CloudFormation 模板无效:模板错误:每个 Fn::GetAtt 对象都需要两个非空参数,即资源名称和资源属性

4

1 回答 1

0

403 错误表明您未经授权或存在某种权限错误。

基于此页面:

https://bref.sh/docs/frameworks/laravel.html

您能否确认您已为 lambda 设置适当的权限以从您使用的 s3 存储桶读取和写入?

...

provider:
    ...
    environment:
        AWS_BUCKET: # environment variable for Laravel
            Ref: Storage
    iamRoleStatements:
        # Allow Lambda to read and write files in the S3 buckets
        -   Effect: Allow
            Action: s3:*
            Resource:
                - Fn::GetAtt: Storage.Arn # the storage bucket
                - Fn::Join: ['', [Fn::GetAtt: Storage.Arn, '/*']] # everything in the storage bucket

resources:
    Resources:
        Storage:
            Type: AWS::S3::Bucket

文档继续说,您必须在 config/filesystems.php 的以下代码段中添加令牌行。你这样做了吗?

's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'token' => env('AWS_SESSION_TOKEN'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
        ],

我不使用 Laravel,所以上述建议只是基于查看您的错误并阅读文档。但是,如果您提供更多信息,我很乐意看看

于 2021-01-01T10:24:58.747 回答