brefphp / laravel-bridge

Package to use Laravel on AWS Lambda with Bref
https://bref.sh/docs/frameworks/laravel.html
MIT License
314 stars 63 forks source link

Invalid configuration value provided for "token". Expected Aws\Token\TokenInterface|Aws\CacheInterface|array|bool|callable, but got string #151

Closed giulioprinaricotti closed 5 months ago

giulioprinaricotti commented 5 months ago

Error

Invalid configuration value provided for "token". Expected Aws\Token\TokenInterface|Aws\CacheInterface|array|bool|callable, but got string(872)

This is similar to

https://github.com/aws/aws-sdk-php/issues/2567 https://github.com/brefphp/bref/issues/1330 (closed, tried the provided answer without success)

I'm using a serverless.yml SQS configuration similar to #103 and https://bref.sh/docs/laravel/queues.

provider:
    region: eu-central-1
    environment:
        QUEUE_CONNECTION: sqs
        SQS_QUEUE: ${construct:jobs.queueUrl}
        EVENT_PROJECTOR_QUEUE_NAME: sqs
        SQS_PREFIX: https://sqs.eu-central-1.amazonaws.com/704985899778
...
constructs:
  jobs:
    type: queue
    worker:
        handler: Bref\LaravelBridge\Queue\QueueHandler
        runtime: php-81
        timeout: 720 # seconds

I've also tried to use the version in getting-started/serverless.yml that uses iam and resources.Queue to build the infrastructure instead.

In both cases I see the queue created correctly in SQS but when I try to dispatch anything (events or jobs) I receive

Invalid configuration value provided for "token". Expected Aws\Token\TokenInterface|Aws\CacheInterface|array|bool|callable, but got string(872)

I've tried to add this line in queue.sqs config to no avail.

'token' => new Aws\Token\Token(env('AWS_SESSION_TOKEN')),

My configuration

queue.php

'sqs' => [
            'driver' => 'sqs',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'prefix' => env('SQS_PREFIX', 'https://sqs.eu-central-1.amazonaws.com/704985899778'),
            'queue' => env('SQS_QUEUE', 'default'),
            'suffix' => env('SQS_SUFFIX'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
            'after_commit' => false,
],

composer installed packages

aws/aws-sdk-php                      3.296.6 #I also tried to downgrade to 3.237.1 
bref/laravel-bridge                  2.2.1
laravel/framework                    v10.41.0

Full serverless.yml for reference

service: laravel

provider:
    name: aws
    region: eu-central-1
    environment:
        APP_NAME: Example
        APP_ENV: production # Or use ${sls:stage} if you want the environment to match the stage
        APP_URL: https://www.example.com
        APP_KEY: ${ssm:/example-app/app_key}
        DB_CONNECTION: planetscale
        DB_HOST: aws.connect.psdb.cloud
        DB_PORT: 3306
        DB_DATABASE: ${ssm:/example-app/db_planetscale_database}
        DB_USERNAME: ${ssm:/example-app/db_planetscale_username}
        DB_PASSWORD: ${ssm:/example-app/db_planetscale_password}
        MYSQL_ATTR_SSL_CA: /opt/bref/ssl/cert.pem
        LOG_CHANNEL: stderr
        FILESYSTEM_PUBLIC_DISK: s3
        FILESYSTEM_DISK: s3
        AWS_BUCKET: !Ref PublicS3
        AWS_USE_PATH_STYLE_ENDPOINT: false
        MAIL_FROM_ADDRESS: "hello@example.it"
        MAIL_FROM_NAME: "Example From"
        MAIL_DRIVER: postmark_transactional
        MAIL_MAILER: postmark_transactional
        POSTMARK_TOKEN: ${ssm:/example-app/postmark_token}
        POSTMARK_MESSAGE_STREAM_TRANSACTIONAL_ID: outbound
        QUEUE_CONNECTION: sqs
        SQS_QUEUE: ${construct:jobs.queueUrl}
        EVENT_PROJECTOR_QUEUE_NAME: sqs
        SQS_PREFIX: https://sqs.eu-central-1.amazonaws.com/my-id
    iam:
        role:
            statements:
                -   Effect: Allow
                    Action: s3:*
                    Resource:
                        - !Sub '${PublicS3.Arn}' 
                        - !Sub '${PublicS3.Arn}/*' 

resources:
    Resources:
        PublicS3:
            Type: AWS::S3::Bucket
            Properties:
                BucketName: my-bucket

package:
    # Files and directories to exclude from deployment
    patterns:
        - '!node_modules/**'
        - '!public/storage'
        - '!resources/assets/**'
        - '!storage/**'
        - '!tests/**'

functions:

    # This function runs the Laravel website/API
    web:
        handler: public/index.php
        runtime: php-82-fpm
        layers:
            - ${bref-extra:gd-php-82}
        timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
        events:
            - httpApi: '*'

    # This function lets us run artisan commands in Lambda
    artisan:
        handler: artisan
        runtime: php-82-console
        timeout: 720 # in seconds
        # Uncomment to also run the scheduler every minute
        events:
           - schedule:
                 rate: rate(1 minute)
                 input: '"schedule:run"'

plugins:
    - ./vendor/bref/bref
    - serverless-lift
    - ./vendor/bref/extra-php-extensions

constructs:
  website:
    type: server-side-website
    domain: app.example.it
    certificate: cert-arn
    assets:
      '/build/assets/*': public/build/assets/
      '/favicon.ico': public/favicon.ico
      '/robots.txt': public/robots.txt
  jobs:
    type: queue
    worker:
        handler: Bref\LaravelBridge\Queue\QueueHandler
        runtime: php-81
        timeout: 720 # seconds
mnapoli commented 5 months ago

Hi, as far as I know, this is indeed a variant of this bug: https://github.com/laravel/framework/pull/44979

Everything looks ok in your config, except you don't need to define SQS_PREFIX (I'd remove it completely).

Also are you sure the error is related to SQS and not S3? (because in most reports people had problems with the S3 config).

Are you also 100% sure the Laravel-Bref bridge's service provider is correctly registered? It should automatically set up the token:

https://github.com/brefphp/laravel-bridge/blob/master/src/BrefServiceProvider.php#L48-L51

Maybe you have a specific config that is not the default (e.g. a SQS connection not called sqs, or a S3 driver not called s3?)

giulioprinaricotti commented 5 months ago

Found the issue. I had vapor in my packages due to a previous attempt on vapor. For some reason, it intercepts the SQS connection and does not indeed have the fix that removes token from the configuration used to build the SqsClient.

Closing this one. Thanks!