How can I set a timeout in SNS publish method? - php

I'm using the AWS SNS service in my PHP application to send messages to a queue.
If internet connection is down, the application should keep running without writing the messages. But the method that publish messages on the queue has a very long timeout, it makes the application unusable without internet.
I didn't found any place in AWS SDK where I could set a smaller timeout.
Is there some way to do this?
Here is an example of the construct of the snsClient object.
I've edit the params to use http timeout as #Anthony Neace told, I also try to use request.options connect_timeout as I found here http://docs.aws.amazon.com/aws-sdk-php/v2/guide/configuration.html
I've changed the gateway of my PC to simulate when the internet is down and I got the exception [curl] 28: Resolving timed out after 3000 milliseconds https://sns.sa-east-1.amazonaws.com/
use Aws\Sns\SnsClient;
SnsClient::factory([
'version' => '...',
'region' => '...',
'credentials' => [
'key' => '...',
'secret' => '...',
],
'http' => [
'timeout' => 5
],
'request.options' => [
'connect_timeout' => 5
]
]);

You can set a timeout in your client's constructor. This will apply to all requests you make with that client, including SnsClient Publish.
Here's an example from the documentation, modified to use SnsClient:
use Aws\Sns\SnsClient;
// Timeout after 5 seconds.
$client = new SnsClient([
'region' => 'us-west-2',
'version' => 'latest',
'http' => [
'timeout' => 5
]
]);
Further Reading:
AWS SDK for PHP - AWSClient Constructor (SnsClient inherits this constructor)
AWS SDK for PHP - SnsClient
AWS SDK for PHP - Client Configuration Options - Timeout

Related

AWS PHP SDK - S3 failing silently

So I have a block of code to upload an image to an S3 Bucket. It's fairly boilerplate. Works perfectly running from localhost.
I push it to my Ubuntu EC2 server, and the code fails. No error, no exceptions, Debug => true outputs nothing.. Trying to var_dump the $s3Client variable reports nothing.
E_ALL Error reporting is on
PHP 8.1.4
Nginx 1.21.6
Compatability-test.php passes successfully - all required modules enabled.
Code:
<?php
require 'S3/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucket = 'XXXXX';
try {
//Create a S3Client
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature' => 'v4',
'debug' => true,
'credentials' => [
'key' => XXXXX,
'secret' => XXXXX,
]
]);
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $fileName,
'SourceFile' => $filePath,
'ACL' => 'public-read'
]);
echo json_encode(array('success' => true, 'imageUrl' => $result->get('ObjectURL')));
} catch (S3Exception $exception) {
echo $exception->getMessage() . "\n";
}
Has anyone else experienced this 'quiet failure' with the AWS PHP-SDK S3?
I solved this, after 3 days. There were several problems, which I managed to solve and inspect by running the code from the PHP CLI.
First things to note:
Credentials must be stored in the /var/www/.aws/credentials file. This is where the S3Client class will look for your S3 credentials. Passing them into the S3 class instantiation does not appear to work.
Running from the CLI, indicated that I was missing the PHP module mbstring. I don't know why Amazon's compatibility-test.php file does not flag this module as missing. Go figure.
Why the script failed silently, I really don't know. Maybe some error suppression somewhere in the S3Client class. I highly recommend running from the PHP-CLI to diagnose any errors with the PHP AWS S3 SDK.
I realize this is a bit late, but I experienced a similar issue recently where specifically I did not want to use the credentials file, whereas your answer notes that the credentials must be stored in the credentials file.
In my experience, what wound up working for me was removing the 'profile' => 'default', in the s3Client creation. So like this ...
//Create a S3Client
$s3Client = new S3Client([
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature' => 'v4',
'debug' => true,
'credentials' => [
'key' => XXXXX,
'secret' => XXXXX,
]
]);
So if you minimally want to use the inline credentials instead of the credentials file, this may help you out a bit.

AWS SNS not working on live ec2 server using PHP AWS SDK

I have used SNS service to send SMS on mobile. I have used PHP AWS SDK to server this purpose.
I have tested script in our local server and it sends sms on mobile successfully but when i upload the script in ec2 server then it does not work.
Script hang the web page and sending sms does not work. It does not give any error and loads the page until it get timeout.
We have used http://xxxx.us-west-1.compute.amazonaws.com/ AWS ec2 instance.
My php script is as follows:
require 'vendor/aws-sdk/aws-autoloader.php';
use Aws\S3\SnsClient;
$params = array(
'credentials' => array(
'key' => AWS_ACCESS_KEY,
'secret' => AWS_SECRET_KEY,
),
'region' => 'us-east-1',
'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$result = $sns->publish([
'Message' => "Message here", // REQUIRED
"SMSType" => "Transactional",
'PhoneNumber' => "xxxx", // phone number with country code
'MessageStructure' => 'SMS'
]);
Please suggest.
what we are doing wrong?
Is it any server configuration required to send SMS? or
Do we need to create another ec2 instance with different AWS Region?

AWS S3Client PHP suddenly doesn't work anymore

today my web site which usually auto logs to aws s3 by php, can't connect anymore; the code hasn't changed at all.
$s3Client = new S3Client([
'region' => 'eu-west-1',
'version' => 'latest',
'credentials' => [
'key' => $key,
'secret' => $secret,
],
'http' => [
'verify' => '../ssl_crt/cacert.pem'
]
]);
//echo var_dump($s3Client);
//Listing all S3 Bucket
$result = $s3Client->listBuckets();
the code above which has worked fine till yesterday now gets tomeout connection and therefore fails listing buckets.
I have read amazon has changed something in the permissions/policy, but I can't find how this can be related to PHP s3Client... maybe I could just change some setting in AWS S3 control panel, but I can't figure out what...Has anyone experienced the same issue

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?
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.
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.
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.
It's a bad practice to store AWS credentials in EC2 instances, It's much better to create an IAM role with sqs:receiveMessage permission then attach that IAM role to your EC2 instance.

setting up cloudfront with php sdk

I am trying to configure the the amazon cloud front, I have successfully created clouldflayer url and access private s3 bucket from it via console. Now I am trying to do it by php-sdk for that I have tried following code
use Aws\CloudFront\CloudFrontClient;
$cle = new CloudFrontClient([
'version' => 'latest',
'region' => 'us-west-2',
'credentials.ini' => [
'key' => 'credentials\pk-myKey.pem',
'secret' => 'secret',
],
]);
$result = $cle->getCloudFrontOriginAccessIdentity([
'Id' => '****', // REQUIRED
]);
print_r($result);
but I am getting error
Fatal error: Uncaught exception 'Aws\CloudFront\Exception\CloudFrontException' with message 'Error executing "GetCloudFrontOriginAccessIdentity" on "https://cloudfront.amazonaws.com/2015-04-17/origin-access-identity/cloudfront/SDF345G";
AWS HTTP error: Client error: 403 SignatureDoesNotMatch (client): Credential should be scoped to a valid region, not 'us-west-2'. -
and I have tried all reason one by one but its not working
Credential should be scoped to a valid region, not 'us-west-2'.
Unlike most of AWS, CloudFront is not a regional service, it's a global one, configured and managed through us-east-1, regardless of the region where any of the related services (S3, EC2, etc.) are deployed.
'region' => 'us-east-1',

Categories