Sending events to Amazon CloudWatch Events with Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Sending events to Amazon CloudWatch Events with Amazon SDK for PHP Version 3

CloudWatch Events delivers a near real-time stream of system events that describe changes in Amazon Web Services (Amazon) resources to any of various targets. Using simple rules, you can match events and route them to one or more target functions or streams.

The following examples show how to:

All the example code for the Amazon SDK for PHP is available here on GitHub.

Credentials

Before running the example code, configure your Amazon credentials, as described in Credentials. Then import the Amazon SDK for PHP, as described in Basic usage.

Create a rule

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\cloudwatchevents\cloudwatcheventsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2015-10-07' ]); try { $result = $client->putRule([ 'Name' => 'DEMO_EVENT', // REQUIRED 'RoleArn' => 'IAM_ROLE_ARN', 'ScheduleExpression' => 'rate(5 minutes)', 'State' => 'ENABLED', ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Add targets to a rule

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\cloudwatchevents\cloudwatcheventsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2015-10-07' ]); try { $result = $client->putTargets([ 'Rule' => 'DEMO_EVENT', // REQUIRED 'Targets' => [ // REQUIRED [ 'Arn' => 'LAMBDA_FUNCTION_ARN', // REQUIRED 'Id' => 'myCloudWatchEventsTarget' // REQUIRED ], ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

Send custom events

Imports

require 'vendor/autoload.php'; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\cloudwatchevents\cloudwatcheventsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2015-10-07' ]); try { $result = $client->putEvents([ 'Entries' => [ // REQUIRED [ 'Detail' => '<string>', 'DetailType' => '<string>', 'Resources' => ['<string>'], 'Source' => '<string>', 'Time' => time() ], ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }