AWS SDK for C++
Developer Guide

Sending Events to CloudWatch

CloudWatch Events delivers a near real-time stream of system events that describe changes in AWS resources to Amazon EC2 instances, Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, Amazon SNS topics, Amazon SQS queues, or built-in targets. You can match events and route them to one or more target functions or streams by using simple rules.

Note

These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ and have configured default AWS credentials using the information in Providing AWS Credentials.

Add Events

To add custom CloudWatch events, call the CloudWatchEventsClient's PutEvents function with a PutEventsRequest object that contains one or more PutEventsRequestEntry objects that provide details about each event. You can specify several parameters for the entry such as the source and type of the event, resources associated with the event, and so on.

Note

You can specify a maximum of 10 events per call to putEvents.

Includes

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/events/CloudWatchEventsClient.h> #include <aws/events/model/PutEventsRequest.h>

Code

lude <aws/core/utils/Outcome.h> lude <iostream> :String MakeDetails(const Aws::String &key, const Aws::String& value) Aws::Utils::Json::JsonValue value_entry; value_entry.AsString(value); Aws::Utils::Json::JsonValue detail_map; detail_map.WithObject(key, value_entry); Aws::String event_key(argv[2]); Aws::String event_value(argv[3]); Aws::CloudWatchEvents::CloudWatchEventsClient cwe; Aws::CloudWatchEvents::Model::PutEventsRequestEntry event_entry; event_entry.SetDetail(MakeDetails(event_key, event_value)); event_entry.SetDetailType("sampleSubmitted"); event_entry.AddResources(resource_arn); event_entry.SetSource("aws-sdk-cpp-cloudwatch-example"); Aws::CloudWatchEvents::Model::PutEventsRequest request; request.AddEntries(event_entry); auto outcome = cwe.PutEvents(request); if (!outcome.IsSuccess()) { std::cout << "Failed to post cloudwatch event: " << outcome.GetError().GetMessage() << std::endl; } else

Add Rules

To create or update a rule, call the CloudWatchEventsClient's PutRule function with a PutRuleRequest with the name of the rule and optional parameters such as the event pattern, IAM role to associate with the rule, and a scheduling expression that describes how often the rule is run.

Includes

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/events/CloudWatchEventsClient.h> #include <aws/events/model/PutRuleRequest.h>

Code

Aws::String rule_name(argv[1]); Aws::String role_arn(argv[2]); Aws::CloudWatchEvents::CloudWatchEventsClient cwe; Aws::CloudWatchEvents::Model::PutRuleRequest request; request.SetName(rule_name); request.SetRoleArn(role_arn); request.SetScheduleExpression("rate(5 minutes)"); request.SetState(Aws::CloudWatchEvents::Model::RuleState::ENABLED); auto outcome = cwe.PutRule(request); if (!outcome.IsSuccess()) { std::cout << "Failed to create cloudwatch events rule " << rule_name << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created cloudwatch events rule " <<

Add Targets

Targets are the resources that are invoked when a rule is triggered. Example targets include Amazon EC2 instances, Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, and built-in targets.

To add a target to a rule, call the CloudWatchEventsClient's PutTargets function with a PutTargetsRequest containing the rule to update and a list of targets to add to the rule.

Includes

CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/events/CloudWatchEventsClient.h> #include <aws/events/model/PutTargetsRequest.h>

Code

Aws::String lambda_arn(argv[2]); Aws::String target_id(argv[3]); Aws::CloudWatchEvents::CloudWatchEventsClient cwe; Aws::CloudWatchEvents::Model::Target target; target.SetArn(lambda_arn); target.SetId(target_id); Aws::CloudWatchEvents::Model::PutTargetsRequest request; request.SetRule(rule_name); request.AddTargets(target); auto putTargetsOutcome = cwe.PutTargets(request); if (!putTargetsOutcome.IsSuccess()) { std::cout << "Failed to create cloudwatch events target for rule " << rule_name << ": " << putTargetsOutcome.GetError().GetMessage() << std::endl; } else { std::cout <<

See the complete example.

More Information