環境
- AWS
- Amazon Linux2
キュー?メッセージ?
- キュー
入口と出口のある箱 - メッセージ
箱に飛び込む人
SQS
- 自動でスケーリングしてくれるサーバレスなMQ(メッセージキューイング)
もくじ
関連
環境設定
PHPのインストール
$ sudo amazon-linux-extras install php7.2
$ sudo yum list php* | grep amzn2extra-php7.2 php.x86_64 7.2.16-1.amzn2.0.1 @amzn2extra-php7.2 php-cli.x86_64 7.2.16-1.amzn2.0.1 @amzn2extra-php7.2 php-common.x86_64 7.2.16-1.amzn2.0.1 @amzn2extra-php7.2 php-fpm.x86_64 7.2.16-1.amzn2.0.1 @amzn2extra-php7.2 php-json.x86_64 7.2.16-1.amzn2.0.1 @amzn2extra-php7.2 php-mysqlnd.x86_64 7.2.16-1.amzn2.0.1 @amzn2extra-php7.2 php-pdo.x86_64 7.2.16-1.amzn2.0.1 @amzn2extra-php7.2 php-bcmath.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-dba.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-dbg.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-devel.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-embedded.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-enchant.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-gd.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-gmp.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-intl.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-ldap.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-mbstring.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-odbc.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-opcache.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-pecl-apcu.x86_64 5.1.12-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-apcu-devel.noarch 5.1.12-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-igbinary.x86_64 2.0.7-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-igbinary-devel.noarch 2.0.7-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-imagick.x86_64 3.4.3-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-imagick-devel.noarch 3.4.3-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-mcrypt.x86_64 1.0.1-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-memcached.x86_64 3.0.4-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-msgpack.x86_64 2.0.2-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-msgpack-devel.noarch 2.0.2-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-oauth.x86_64 2.0.2-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-redis.x86_64 4.3.0-1.amzn2 amzn2extra-php7.2 php-pecl-ssh2.x86_64 1.1.2-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-uuid.x86_64 1.0.4-3.amzn2.0.1 amzn2extra-php7.2 php-pecl-zip.x86_64 1.15.2-3.amzn2.0.1 amzn2extra-php7.2 php-pgsql.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-process.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-pspell.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-recode.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-snmp.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-soap.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-xml.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2 php-xmlrpc.x86_64 7.2.16-1.amzn2.0.1 amzn2extra-php7.2
Composerのインストール
$ curl -sS https://getcomposer.org/installer | php $ sudo mv composer.phar /usr/local/bin/composer
※再ログインを行う
そうしないとcomposerコマンドが利用できない
AWS SDKのインストール
$ mkdir $HOME/sqsApp $ cd $HOME/sqsApp $ composer require aws/aws-sdk-php
バージョン確認
$ aws --version aws-cli/1.16.102 Python/2.7.14 Linux/4.14.114-105.126.amzn2.x86_64 botocore/1.12.92
クレデンシャルの設定
$ mkdir $HOME/.aws/credentials $ vi $HOME/.aws/credentials [default] aws_access_key_id = <アクセスキー ID> aws_secret_access_key = <シークレットアクセスキー>
AWS IAMで作成したユーザ情報を入力しよう。
環境変数 リージョンの設定
$ vi $HOME/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH ※下記を追記 export AWS_DEFAULT_REGION='ap-northeast-1'
※再ログインしてください。
設定の確認
$ aws configure list Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************I5XU shared-credentials-file secret_key ****************ymHz shared-credentials-file region ap-northeast-1 env AWS_DEFAULT_REGION
環境は出来ました。
SDKによるキューの操作
キューの作成
$ aws sqs create-queue --queue-name testQueue { "QueueUrl": "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" }
キューの表示
$ aws sqs list-queues { "QueueUrls": [ "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" ] }
キューのリスト表示
$ aws sqs list-queues { "QueueUrls": [ "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" ] }
キューの送信 1
$ aws sqs send-message --queue-url "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" --message-body "TEST Queue" --delay-seconds 60 { "MD5OfMessageBody": "422038d0d323112b600874ed2e9db010", "MessageId": "634cb912-a873-4bc1-84c7-cede538d5240" }
キューの送信2
$ aws sqs send-message --queue-url "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" --message-body "TEST Queue 2" --delay-seconds 60 { "MD5OfMessageBody": "45c3322526e349ad9257dac1674047b3", "MessageId": "93392523-dc44-40b0-8df7-b383f3039ac8" }
キューの受信1
$ aws sqs receive-message --queue-url "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" --visibility-timeout 60 - -wait-time-seconds 20 { "Messages": [ { "Body": "TEST Queue", "ReceiptHandle": "AQEBUmnNayh1IiR6AqyIhDaA0CjXgLs39kESxrafdd90QohJKy3JT2pDor30p61qs1vr1qX+bV5rt5Ym0mAvsW+y6ENiPbOQJTd03HyI4bb0T1WUYvdOgFBTtHbaLRefkcLyKZ0zqXz98cLHV9P0usLEKZQNkjT2RAF1NFfbTyvJPSi2zyVXn+9IcIDgp0gRx55D6/33Tjts4gNhwQicnuSJTJBwd9UMXv6B7uxk14zDhFWKGIwNLoYli8W+2z4/JOnh64iSGQmC9RCcp+bO+wTwIQX7f0BUm77qfV8KNv7I7jJ2J0pr/0tMu/nmuw8l5ehlVnatYjkfxQGAGxaWc2Z+KreYz80VvA4M8RHMplY9FwiMqjOOutH6wtmgC9Rc7bzLj3ni28kcT5wgWi5ZrOBtFw==", "MD5OfBody": "422038d0d323112b600874ed2e9db010", "MessageId": "634cb912-a873-4bc1-84c7-cede538d5240" } ] }
キューの受信2
$ aws sqs receive-message --queue-url "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" --visibility-timeout 60 --wait-time-seconds 20 { "Messages": [ { "Body": "TEST Queue 2", "ReceiptHandle": "AQEButxx8AqiIKx89BEE+HyV5gOY95IgllvONuBPttkHdqCTajVYSAkEiBTdaLaXwKhww0MWTpNqBis56bKcuQyAlLJWeFoEKtP2RzXFinsHwiH3qW6PmjTiGHFJiuhbhcPcolczM5XAxsjpUh2R03TnE8+FtmPH409a8imzCh2AVGgC41OQJC9pCIp84kJ7dX02HLxfnsQ6tayh3ckPG0khJfHatltPpFgN2uLz1Xajao/I3mXARM4w+fam97LyONIxlK6SUw5jJkkDoa/p9EqELXr9SYcNSrqVDtrM+LMtCmkkFpTTNmQBm6HOCjFe2qamF0qvPb216BPM7A0waIMz9REJBmvrL8gbmVv8stnY6L2HWMoVte7KAkvpxNABm0D2oknRElfRF2kt9gSkS2CISg==", "MD5OfBody": "45c3322526e349ad9257dac1674047b3", "MessageId": "93392523-dc44-40b0-8df7-b383f3039ac8" } ] }
メッセージの削除1
$ aws sqs delete-message --queue-url "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" --receipt-handle "AQEBUmnNayh1IiR6AqyIhDaA0CjXgLs39kESxrafdd90QohJKy3JT2pDor30p61qs1vr1qX+bV5rt5Ym0mAvsW+y6ENiPbOQJTd03HyI4bb0T1WUYvdOgFBTtHbaLRefkcLyKZ0zqXz98cLHV9P0usLEKZQNkjT2RAF1NFfbTyvJPSi2zyVXn+9IcIDgp0gRx55D6/33Tjts4gNhwQicnuSJTJBwd9UMXv6B7uxk14zDhFWKGIwNLoYli8W+2z4/JOnh64iSGQmC9RCcp+bO+wTwIQX7f0BUm77qfV8KNv7I7jJ2J0pr/0tMu/nmuw8l5ehlVnatYjkfxQGAGxaWc2Z+KreYz80VvA4M8RHMplY9FwiMqjOOutH6wtmgC9Rc7bzLj3ni28kcT5wgWi5ZrOBtFw=="
メッセージの削除2
$ aws sqs delete-message --queue-url "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" --receipt-handle "AQEButxx8AqiIKx89BEE+HyV5gOY95IgllvONuBPttkHdqCTajVYSAkEiBTdaLaXwKhww0MWTpNqBis56bKcuQyAlLJWeFoEKtP2RzXFinsHwiH3qW6PmjTiGHFJiuhbhcPcolczM5XAxsjpUh2R03TnE8+FtmPH409a8imzCh2AVGgC41OQJC9pCIp84kJ7dX02HLxfnsQ6tayh3ckPG0khJfHatltPpFgN2uLz1Xajao/I3mXARM4w+fam97LyONIxlK6SUw5jJkkDoa/p9EqELXr9SYcNSrqVDtrM+LMtCmkkFpTTNmQBm6HOCjFe2qamF0qvPb216BPM7A0waIMz9REJBmvrL8gbmVv8stnY6L2HWMoVte7KAkvpxNABm0D2oknRElfRF2kt9gSkS2CISg=="
キューの確認
$ aws sqs list-queues { "QueueUrls": [ "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" ] }
キューの削除
$ aws sqs delete-queue --queue-url "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue"
※削除すると60秒は同じ名前のキューは作れません。
キューの確認
$ aws sqs list-queues ※何も表示されなければOK
PHP によるSQSの操作
メッセージの送信
<?php require 'vendor/autoload.php'; use Aws\Sqs\SqsClient; use Aws\Exception\AwsException; define('QUEUE_URL', 'https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue'); try{ $client = new SqsClient([ 'profile' => 'default', 'region' => 'ap-northeast-1', 'version' => '2012-11-05', ]); $params = [ 'DelaySeconds' => 0, 'MessageAttributes' => [ 'Title' => [ 'DataType' => 'String', 'StringValue' => 'SQS 送信します(件名)', ] ], 'MessageBody' => '申します、申します。', 'QueueUrl' => QUEUE_URL, ]; $result = $client->sendMessage($params); var_dump($result); } catch(AwsException $e){ error_log($e->getMessage()); }
実行
$ php sendSqs.php object(Aws\Result)#119 (2) { ["data":"Aws\Result":private]=> array(4) { ["MD5OfMessageBody"]=> string(32) "7fee6a06ab0be5d9c92ee1e0978f9a11" ["MD5OfMessageAttributes"]=> string(32) "dd86344a085ab9f5a44759b48e29f90a" ["MessageId"]=> string(36) "e8101b02-2f0b-42a4-8ac8-cddb3549666d" ["@metadata"]=> array(4) { ["statusCode"]=> int(200) ["effectiveUri"]=> string(65) "https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue" ["headers"]=> array(4) { ["x-amzn-requestid"]=> string(36) "e1c469f5-f127-5865-8e07-3c9c61f4a97b" ["date"]=> string(29) "Thu, 30 May 2019 08:04:35 GMT" ["content-type"]=> string(8) "text/xml" ["content-length"]=> string(3) "459" } ["transferStats"]=> array(1) { ["http"]=> array(1) { [0]=> array(0) { } } } } } ["monitoringEvents":"Aws\Result":private]=> array(0) { } }
メッセージの受信
$ cat recieveSqs.php <?php require 'vendor/autoload.php'; use Aws\Sqs\SqsClient; use Aws\Exception\AwsException; define('QUEUE_URL', 'https://ap-northeast-1.queue.amazonaws.com/<AWSアカウントID>/testQueue'); try{ $client = new SqsClient([ 'profile' => 'default', 'region' => 'ap-northeast-1', 'version' => 'latest', ]); $receive = [ 'AttributeNames' => ['All'], 'MessageAttributeNames' => ['All'], 'MaxNumberOfMessages' => 10, 'QueueUrl' => QUEUE_URL, 'WaitTimeSeconds' => 20, 'VisibilityTimeout' => 60, ]; // キューを監視しあれば受信しキューを削除する。 while(true){ $result = $client->receiveMessage($receive); $data = $result->get('Messages'); if($data){ foreach($data as $item){ echo $item['Body']; // キューの削除 $client->deleteMessage([ 'QueueUrl' => QUEUE_URL, 'ReceiptHandle' => $item['ReceiptHandle'], 'VisibilityTimeout' => 1000, ]); } } } } catch(AwsException $e){ error_log($e->getMessage()); }
実際の利用だとデーモン化するか、Lambdaでバッチ対応
実行
$ php recieveSqs.php 申します、申します。