laravelamazon-web-serviceslaravel-8aws-php-sdkbref

AWS Token Issue with Bref and Laravel on Lambda: Invalid Configuration Value for 'token'


I am using Bref to run Laravel on AWS Lambda, and here is my serverless.yml configuration:

service: lambdajob

provider:
  name: aws
  # The AWS region in which to deploy (us-east-1 is the default)
  region: us-east-2
  # Environment variables
  environment:
    APP_ENV: ${sls:stage} # Or use ${sls:stage} if you want the environment to match the stage
    QUEUE_CONNECTION: sqs
    SQS_QUEUE: lambda-jobs-${sls:stage}
    SQS_PREFIX: https://sqs.us-east-2.amazonaws.com/xxxxxxxxxxx
    CACHE_DRIVER: dynamodb
    DYNAMODB_CACHE_TABLE: testtools-staging-cache
    LOG_CHANNEL: stderr
    TELESCOPE_ENABLED: false
    DB_HOST: ${ssm:/${sls:stage}/DB_HOST}
    DB_DATABASE: ${ssm:/${sls:stage}/DB_DATABASE}
    DB_PASSWORD: ${ssm:/${sls:stage}/DB_PASSWORD}
    DB_USERNAME: ${ssm:/${sls:stage}/DB_USERNAME}
    AWS_BUCKET: staging
  iam:
    role: arn:aws:iam::xxxxxxxx:role/lambda-role

package:
  exclude:
    - .env
    - node_modules/**
    - public/storage
    - resources/js/**
    - rr
    - docker/**
    - storage/logs/**
    - storage/app/**
    - storage/framework/**
    - tests/**

functions:
  worker:
    handler: Bref\LaravelBridge\Queue\QueueHandler
    timeout: 900
    runtime: php-81
    events:
      - sqs:
          arn:
            Fn::GetAtt: [ YourQueueName, Arn ]
          batchSize: 1

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

resources:
  Resources:
    # The queue
    YourQueueName:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: lambda-jobs-${sls:stage}
        VisibilityTimeout: 900
        RedrivePolicy:
          maxReceiveCount: 3 # jobs will be retried up to 3 times
          # Failed jobs (after the retries) will be moved to the other queue for storage
          deadLetterTargetArn:
            Fn::GetAtt: [ YourQueueFailedName, Arn ]
    # Failed jobs will go into that SQS queue to be stored, until a developer looks at these errors
    YourQueueFailedName:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: lambda-jobs-failed-${sls:stage}
        MessageRetentionPeriod: 1209600 # maximum retention: 14 days


The configuration works, and the Lambda function executes when a job is triggered. However, I get this error:

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

I tried changing the token to Aws\Token\Token inside the BrefServiceProvider in fixAwsCredentialsConfig method, but then I got this error at /var/task/vendor/aws/aws-sdk-php/src/Signature/SignatureV4.php:328:

taging.ERROR: Object of class Aws\Token\Token could not be converted to string {"exception":"[object] (Error(code: 0): Object of class Aws\\Token\\Token could not be converted to string at /var/task/vendor/aws/aws-sdk-php/src/Signature/SignatureV4.php:328)

Here is the code in BrefServiceProvider:

private function fixAwsCredentialsConfig(): void
    {
        $accessKeyId = $_SERVER['AWS_ACCESS_KEY_ID'] ?? null;
        $sessionToken = $_SERVER['AWS_SESSION_TOKEN'] ?? null;
        $sessionToken = new AwsToken($sessionToken); // what I added in BrefServiceProvider
         //....
}

It seems the AWS PHP SDK needs the token to be TokenInterface However in some other places the token needs to be string. I also tried to create a custom Token class where the __toString method returns the string value of the token. Although this seems to fix previous issues. However, I faced another error where GuzzleHttp didn't accept the Token as an object.

Looks like in laravel logic the token is supposed to be a String but in AWS-PHP-SDK the token supposed to be Aws\Token\Token

Can anyone help me fix this issue?

and these are versions of my packages:


Solution

  • Turns out that laravel 8.83.27 is not compatible with the AWS SDK ^2.245. If anyone facing same issue they must consider upgrading to an updated version of laravel. However if you need this to work with laravel 8 just downgrade the AWS SDK to 2.244.1.