private function addCloudwatchAlarm($sqsName, $region, $accessKey, $secretKey)
 {
     $data['region'] = $region;
     if ($accessKey != null && $secretKey != null) {
         $data['key'] = $accessKey;
         $data['secret'] = $secretKey;
     }
     $cwClient = new CloudWatchClient($data);
     $cwClient->putMetricAlarm(['AlarmName' => sprintf('Syrup %s queue is full', $sqsName), 'ActionsEnabled' => true, 'AlarmActions' => ['arn:aws:sns:us-east-1:147946154733:Connection_SQS_Alerts'], 'MetricName' => 'ApproximateNumberOfMessagesVisible', 'Namespace' => 'AWS/SQS', 'Statistic' => 'Average', 'Dimensions' => [['Name' => 'QueueName', 'Value' => $sqsName]], 'Period' => 300, 'EvaluationPeriods' => 1, 'Threshold' => 5, 'ComparisonOperator' => 'GreaterThanOrEqualToThreshold']);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $noWatch = $input->getOption('no-watch');
     $queueId = $this->getQueueId();
     $snsConfig = $this->getContainer()->getParameter('sns');
     $region = isset($snsConfig['region']) ? $snsConfig['region'] : 'us-east-1';
     /** @var QueueFactory $queueFactory */
     $queueFactory = $this->getContainer()->get('syrup.queue_factory');
     $sqs = $queueFactory->create($queueId, $region, $snsConfig['key'], $snsConfig['secret']);
     /** @var Connection $conn */
     $conn = $this->getContainer()->get('doctrine.dbal.syrup_connection');
     $stmt = $conn->query("SELECT * FROM queues WHERE id='{$queueId}'");
     $res = $stmt->fetchAll();
     if (empty($res)) {
         $conn->insert('queues', ['id' => $queueId, 'access_key' => $snsConfig['key'], 'secret_key' => $snsConfig['secret'], 'region' => $region, 'url' => $sqs->get('QueueUrl')]);
     }
     $sqsClient = new SqsClient(['region' => $region, 'version' => '2012-11-05', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]);
     $sqsArn = $sqsClient->getQueueArn($sqs->get('QueueUrl'));
     // subscribe SQS to SNS
     $snsClient = new SnsClient(['region' => $region, 'version' => '2010-03-31', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]);
     $snsClient->subscribe(['TopicArn' => $snsConfig['topic_arn'], 'Protocol' => 'sqs', 'Endpoint' => $sqsArn]);
     // add policy to SQS to allow SNS sending messages to it
     $sqsPolicy = '{
       "Version": "2008-10-17",
       "Id": "' . $sqsArn . '/SQSDefaultPolicy",
       "Statement": [
         {
           "Sid": "sqs-sns",
           "Effect": "Allow",
           "Principal": {
             "AWS": "*"
           },
           "Action": "SQS:SendMessage",
           "Resource": "' . $sqsArn . '",
           "Condition": {
             "ArnEquals": {
               "aws:SourceArn": "' . $snsConfig['topic_arn'] . '"
             }
           }
         }
       ]
     }';
     $sqsClient->setQueueAttributes(['QueueUrl' => $sqs->get('QueueUrl'), 'Attributes' => ['Policy' => $sqsPolicy]]);
     $output->writeln("SQS created and registered to SNS");
     // Add Cloudwatch alarm
     if (!$noWatch) {
         $cwClient = new CloudWatchClient(['region' => $region, 'version' => '2010-08-01', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]);
         $cwClient->putMetricAlarm(['AlarmName' => sprintf('Syrup %s queue is full', $queueId), 'ActionsEnabled' => true, 'AlarmActions' => [$snsConfig['alarm_topic_arn']], 'MetricName' => 'ApproximateNumberOfMessagesVisible', 'Namespace' => 'AWS/SQS', 'Statistic' => 'Average', 'Dimensions' => [['Name' => 'QueueName', 'Value' => $queueId]], 'Period' => 300, 'EvaluationPeriods' => 1, 'Threshold' => 5, 'ComparisonOperator' => 'GreaterThanOrEqualToThreshold']);
         $output->writeln("Cloudwatch alarm created");
     }
 }
 public function terminateInstanceIfIdleForTooLong($instanceIds)
 {
     $this->client->putMetricAlarm(array('AlarmName' => 'TerminateInstanceBecauseIdle', 'AlarmDescription' => 'Terminate instances if CPU is on average < 10% for 5 minutes in a row 8 times consecutively', 'ActionsEnabled' => true, 'OKActions' => array(), 'AlarmActions' => $this->getAlarmActions(), 'InsufficientDataActions' => array(), 'MetricName' => 'CPUUtilization', 'Namespace' => $this->getNamespace(), 'Statistic' => Statistic::AVERAGE, 'Dimensions' => $this->getDimensions($instanceIds), 'Period' => 300, 'Unit' => Unit::PERCENT, 'EvaluationPeriods' => 8, 'Threshold' => 10, 'ComparisonOperator' => ComparisonOperator::LESS_THAN_THRESHOLD));
     $this->client->putMetricAlarm(array('AlarmName' => 'TerminateInstanceIfStatusCheckFails', 'AlarmDescription' => 'Terminate instances in case two status check fail within one minute', 'ActionsEnabled' => true, 'OKActions' => array(), 'AlarmActions' => $this->getAlarmActions(), 'InsufficientDataActions' => array(), 'MetricName' => 'StatusCheckFailed', 'Namespace' => $this->getNamespace(), 'Statistic' => Statistic::AVERAGE, 'Dimensions' => $this->getDimensions($instanceIds), 'Period' => 60, 'Unit' => Unit::PERCENT, 'EvaluationPeriods' => 2, 'Threshold' => 1, 'ComparisonOperator' => ComparisonOperator::GREATER_THAN_OR_EQUAL_TO_THRESHOLD));
 }