Search code examples
phpsymfonyaws-sdkamazon-sqs

Cannot pull SQS message from my EC2 instance


When I deploy my application to a EC2 instance, it fails to fetch messages from my SQS queue. And instead throws an exception with the status code 403 Forbidden, access to the resource {sqs queue} is denied. However, when I run the same code from my local environment my application can fetch messages from the SQS queue.

My application uses the symfony framework and passes pre-configured AWS credentials, for a user who has access to this queue, from the parameters.yml into \Aws\Sqs\SqsClient().

If on the EC2 instance I run aws configure and configure the aws cli with the same credentials the application can pull messages from the SQS queue. I am concerned here because it is like the aws sdk is overriding the credentials I pass it.

As a example the following code even with hard coded parameters which I have checked are valid credentials, returns a 403 when ran on a EC2 instances.

 $sqs = new \Aws\Sqs\SqsClient([
        [
            'key' => '{my key}',
            'secret' => '{my secret}'
        ],
        'region' => 'us-east-1',
        'version' => 'latest'
    ]);

    $response = $sqs->receiveMessage([
        'QueueUrl' => 'https://sqs.us-east-1.amazonaws.com/{my account}/{my queue}'
    ]);

Does anyone have any suggestions about what may be happening here?


Solution

  • Try with credentials key in config.

    $sqs = new \Aws\Sqs\SqsClient([
        'credentials' => [
                'key'    => '{my key}',
                'secret' => '{my secret}',
            ],
            'region' => 'us-east-1',
            'version' => 'latest'
        ]);
    
        $response = $sqs->receiveMessage([
            'QueueUrl' => 'https://sqs.us-east-1.amazonaws.com/{my accoun}/{my queue}'
        ]);