无法从我的EC2实例中提取SQS消息

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?

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}'
    ]);

This might help you to debug your issue.

  1. Run aws sqs list-queues on command line. If your queue not listed in the result set, that means your AWS key doesn't have permission.

  2. Run aws sqs receive-message --queue-url <queue_url> where queue_url is your queue's complete url received from step 1. You should see all your messages in the queue.

If there are no errors in above both steps, there might be an issue in your application end.