Error AWS SQS POST job, with Laravel on serverless

884 Views Asked by At

I have a Laravel api with Serverless that connects to the AWS Lambda service, which tries to do a simple Job as the Laravel documentation advises (https://laravel.com/docs/7.x/queues#dispatching-jobs).

my ProcessPodcast job try a SQS service, but returneda error.

I have this error returning:

Aws\Sqs\Exception\SqsException: Error executing "SendMessage" on "https://sqs.us-east-1.amazonaws.com/your-account-id/$%7Bconstruct:jobs.queueUrl%7D"; AWS HTTP error: Client error: `POST https://sqs.us-east-1.amazonaws.com/your-account-id/$%7Bconstruct:jobs.queueUrl%7D` resulted in a `403 Forbidden` response:
<?xml version="1.0"?><ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"><Error><Type>Sender</Type><Code>S (truncated...)
 SignatureDoesNotMatch (client): Credential should be scoped to a valid region, not 'us-east-2'.  - <?xml version="1.0"?><ErrorResponse xmlns="http://queue.amazonaws.com/doc/2012-11-05/"><Error><Type>Sender</Type><Code>SignatureDoesNotMatch</Code><Message>Credential should be scoped to a valid region, not 'us-east-2'. </Message><Detail/></Error><RequestId>548cdabe-0c6a-58a6-971d-80a53eafa104</RequestId></ErrorResponse> in file /var/task/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php on line 195

my serveless.yml:

service: laravel

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

plugins:
  - ./vendor/bref/bref
  - serverless-lift

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

functions:
    website:
        handler: public/index.php
        timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
        layers:
            - ${bref:layer.php-74-fpm}
        events:
            -   http: 'ANY /'
            -   http: 'ANY /{proxy+}'
            -   httpApi: '*'
    artisan:
        handler: artisan
        timeout: 120 # in seconds
        layers:
            - ${bref:layer.php-74} # PHP
            - ${bref:layer.console} # The "console" layer
    worker:
        handler: worker.php
        layers:
          - ${bref:layer.php-74}
        events:
          # Declares that our worker is triggered by jobs in SQS
            - sqs:
                arn: !GetAtt AlertQueue.Arn
                # If you create the queue manually, the line above could be:
                # arn: 'arn:aws:sqs:us-east-1:1234567890:my_sqs_queue'
                # Only 1 item at a time to simplify error handling
                batchSize: 1

resources:
  Resources:
    # The SQS queue
    AlertQueue:
      Type: AWS::SQS::Queue
      Properties:
        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: !GetAtt DeadLetterQueue.Arn

    # Failed jobs will go into that SQS queue to be stored, until a developer looks at these errors
    DeadLetterQueue:
      Type: AWS::SQS::Queue
      Properties:
        MessageRetentionPeriod: 1209600 # maximum retention: 14 days

obs: My serverless it's working, don't have any error, I can access the public page normally...

1

There are 1 best solutions below

0
On

Laravel ships with a broken configuration for AWS Lambda: https://github.com/laravel/laravel/pull/5138#issuecomment-624025825

You need to add the token key to the SQS configuration in config/queue.php:

        'sqs' => [
            'driver' => 'sqs',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
+           'token' => env('AWS_SESSION_TOKEN'), // ADD THIS LINE
            'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),

See https://github.com/brefphp/laravel-bridge to learn more.