04/09/2018 - PHP, SYMFONY
In this example we are going to deal with Amazon Simple Queue Service (AWS SQS) messages and queues. The only thing we won't do is, message consuming.
Run composer require aws/aws-sdk-php
to install AWS SDK for PHP library.
Go to "Identity and Access Management" dashboard and add a new user by assigning "AmazonSSQSFullAccess" policy to it. The secret
can be seen only once at the time of creating a new user so make sure you note it somewhere safe. Also make sure that the user cannot login to AWS like normal person would do so the "Group" should be "None" instead of something like "AdministratorAccess" so on.
parameters:
aws_sdk.config.default:
version: 'latest'
region: 'eu-west-1'
aws_sdk.credentials.default:
credentials:
key: 'AWS_KEY'
secret: 'AWS_SECRET'
services:
Aws\Sdk: ~
App\Util\AwsSqsUtil:
arguments:
$config: '%aws_sdk.config.default%'
$credentials: '%aws_sdk.credentials.default%'
declare(strict_types=1);
namespace App\Controller;
use App\Util\AwsSqsUtilInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* @Route("/aws")
*/
class AwsSqsController
{
private $awsSqsUtil;
public function __construct(AwsSqsUtilInterface $awsSqsUtil)
{
$this->awsSqsUtil = $awsSqsUtil;
}
/**
* @Route("/sqs/create-queue", methods="POST")
*/
public function createQueue(Request $request): Response
{
$content = json_decode($request->getContent(), true);
return new JsonResponse($this->awsSqsUtil->createQueue($content['name']));
}
/**
* @Route("/sqs/list-queues", methods="GET")
*/
public function listQueues(): Response
{
return new JsonResponse($this->awsSqsUtil->listQueues());
}
/**
* @Route("/sqs/get-queue-url", methods="POST")
*/
public function getQueueUrl(Request $request): Response
{
$content = json_decode($request->getContent(), true);
return new JsonResponse($this->awsSqsUtil->getQueueUrl($content['name']));
}
/**
* @Route("/sqs/send-message", methods="POST")
*/
public function sendMessage(Request $request): Response
{
$content = json_decode($request->getContent(), true);
return new JsonResponse($this->awsSqsUtil->sendMessage($content['url'], json_encode($content['message'])));
}
/**
* @Route("/sqs/get-total-messages", methods="POST")
*/
public function getTotalMessages(Request $request): Response
{
$content = json_decode($request->getContent(), true);
return new JsonResponse($this->awsSqsUtil->getTotalMessages($content['url']));
}
/**
* @Route("/sqs/purge-queue", methods="DELETE")
*/
public function purgeQueue(Request $request): Response
{
$content = json_decode($request->getContent(), true);
$this->awsSqsUtil->purgeQueue($content['url']);
return new JsonResponse('Purged!');
}
/**
* @Route("/sqs/delete-queue", methods="DELETE")
*/
public function deleteQueue(Request $request): Response
{
$content = json_decode($request->getContent(), true);
$this->awsSqsUtil->deleteQueue($content['url']);
return new JsonResponse('Deleted!');
}
}
declare(strict_types=1);
namespace App\Util;
interface AwsSqsUtilInterface
{
public function createQueue(string $name): ?string;
public function listQueues(): iterable;
public function getQueueUrl(string $name): ?string;
public function sendMessage(string $url, string $message): ?string;
public function getTotalMessages(string $url): string;
public function purgeQueue(string $url): void;
public function deleteQueue(string $url): void;
}
declare(strict_types=1);
namespace App\Util;
use Aws\Result;
use Aws\Sdk;
class AwsSqsUtil implements AwsSqsUtilInterface
{
private const QUEUE_PREFIX = 'INANZZZ_';
private $client;
public function __construct(Sdk $sdk, iterable $config, iterable $credentials)
{
$this->client = $sdk->createSqs($config+$credentials);
}
/**
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#createqueue
*/
public function createQueue(string $name): ?string
{
/** @var Result $result */
$result = $this->client->createQueue(['QueueName' => self::QUEUE_PREFIX.$name]);
return $result->get('QueueUrl');
}
/**
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#listqueues
*/
public function listQueues(): iterable
{
$queues = [];
/** @var Result $result */
$result = $this->client->listQueues();
foreach ($result->get('QueueUrls') as $queueUrl) {
$queues[] = $queueUrl;
}
return $queues;
}
/**
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#getqueueurl
*/
public function getQueueUrl(string $name): ?string
{
/** @var Result $result */
$result = $this->client->getQueueUrl(['QueueName' => self::QUEUE_PREFIX.$name]);
return $result->get('QueueUrl');
}
/**
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#sendmessage
*/
public function sendMessage(string $url, string $message): ?string
{
/** @var Result $result */
$result = $this->client->sendMessage([
'QueueUrl' => $url,
'MessageBody' => $message,
]);
return $result->get('MessageId');
}
/**
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#getqueueattributes
*/
public function getTotalMessages(string $url): string
{
/** @var Result $result */
$result = $this->client->getQueueAttributes([
'QueueUrl' => $url,
'AttributeNames' => ['ApproximateNumberOfMessages'],
]);
return $result->get('Attributes')['ApproximateNumberOfMessages'];
}
/**
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#purgequeue
*/
public function purgeQueue(string $url): void
{
$this->client->purgeQueue(['QueueUrl' => $url]);
}
/**
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html#deletequeue
*/
public function deleteQueue(string $url): void
{
$this->client->deleteQueue(['QueueUrl' => $url]);
}
}
curl -X POST \
> http://192.168.99.20:81/api/v1/aws/sqs/create-queue \
> -d '{"name": "1"}'
"https:\/\/sqs.eu-west-1.amazonaws.com\/046441758132\/INANZZZ_1"
curl -X GET \
> http://192.168.99.20:81/api/v1/aws/sqs/list-queues
["https:\/\/sqs.eu-west-1.amazonaws.com\/046441758132\/INANZZZ_1"]
curl -X POST \
> http://192.168.99.20:81/api/v1/aws/sqs/get-queue-url \
> -d '{"name": "1"}'
"https:\/\/sqs.eu-west-1.amazonaws.com\/046441758132\/INANZZZ_1"
curl -X POST \
> http://192.168.99.20:81/api/v1/aws/sqs/send-message \
> -d '{"url": "https://sqs.eu-west-1.amazonaws.com/046441758132/INANZZZ_1","message": {"name": "Inanzzz 1", "age": 18, "is_test": true}}'
"370b429d-6803-4066-80a1-4f8de50e4c38"
curl -X POST \
> http://192.168.99.20:81/api/v1/aws/sqs/get-total-messages \
> -d '{"url": "https://sqs.eu-west-1.amazonaws.com/046441758132/INANZZZ_1"}'
"4"
curl -X DELETE \
> http://192.168.99.20:81/api/v1/aws/sqs/purge-queue \
> -d '{"url": "https://sqs.eu-west-1.amazonaws.com/046441758132/INANZZZ_1"}'
"Purged!"
curl -X DELETE \
> http://192.168.99.20:81/api/v1/aws/sqs/delete-queue \
> -d '{"url": "https://sqs.eu-west-1.amazonaws.com/046441758132/INANZZZ_1"}'
"Deleted!"