Sending Events to Amazon CloudWatch Events with AWS SDK for PHP Version 3 - AWS SDK for PHP

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

CloudWatch Events delivers a near real-time stream of system events that describe changes in Amazon Web Services (AWS) 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 AWS SDK for PHP is available here on GitHub.

Credentials

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

Create a Rule

Imports

require 'vendor/autoload.php'; use Aws\CloudWatchEvents\CloudWatchEventsClient; 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(array( '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\CloudWatchEvents\CloudWatchEventsClient; 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\CloudWatchEvents\CloudWatchEventsClient; 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()); }