Sending Events to Amazon CloudWatch Events - AWS SDK for Go (version 1)

We announced the upcoming end-of-support for AWS SDK for Go V1. We recommend that you migrate to AWS SDK for Go V2. For dates, additional details, and information on how to migrate, please refer to the linked announcement.

Sending Events to Amazon CloudWatch Events

These Go examples show you how to use the AWS SDK for Go to:

  • Create and update a rule used to trigger an event

  • Define one or more targets to respond to an event

  • Send events that are matched to targets for handling

You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

CloudWatch Events delivers a near real-time stream of system events that describe changes in 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.

In these examples, Go code is used to send events to CloudWatch Events. The code uses the AWS SDK for Go to manage instances by using these methods of the CloudWatchEvents type:

Prerequisites

Tasks Before You Start

To set up and run this example, you must first complete these tasks:

  1. Create a Lambda function using the hello-world blueprint to serve as the target for events. To learn how, see Step 1: Create an AWS Lambda function in the CloudWatch Events User Guide.

  2. Create an IAM role whose policy grants permission to CloudWatch Events and that includes events.amazonaws.com as a trusted entity. For more information about creating an IAM role, see Creating a Role to Delegate Permissions to an AWS service in the IAM User Guide.

    Use the following role policy when creating the IAM role.

{ "Version": "2012-10-17", "Statement": [ { "Sid": "CloudWatchEventsFullAccess", "Effect": "Allow", "Action": "events:*", "Resource": "*" }, { "Sid": "IAMPassRoleForCloudWatchEvents", "Effect": "Allow", "Action": "iam:PassRole", "Resource": "arn:aws:iam::*:role/AWS_Events_Invoke_Targets" } ] }

Use the following trust relationship when creating the IAM role.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

Create a Scheduled Rule

Choose Copy to save the code locally.

Create the file events_schedule_rule.go. Import the packages used in the example.

import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatchevents" "fmt" )

Initialize a session that the SDK will use to load credentials from the shared credentials file ~/.aws/credentials, load your configuration from the shared configuration file ~/.aws/config, and create a CloudWatch Events client.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create the cloudwatch events client svc := cloudwatchevents.New(sess)

Call PutRule, supplying a name, DEMO_EVENT, ARN of the IAM role you created, IAM_ROLE_ARN, and an expression defining the schedule. Finally, display the ARN of the rule.

result, err := svc.PutRule(&cloudwatchevents.PutRuleInput{ Name: aws.String("DEMO_EVENT"), RoleArn: aws.String("IAM_ROLE_ARN"), ScheduleExpression: aws.String("rate(5 minutes)"), }) fmt.Println("Rule ARN:", result.RuleArn)

See the complete example on GitHub.

Add a Lambda Function Target

Choose Copy to save the code locally.

Create the file events_put_targets.go. Import the packages used in the example.

import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatchevents" "fmt" )

Initialize a session that the SDK will use to load credentials from the shared credentials file ~/.aws/credentials, load your configuration from the shared configuration file ~/.aws/config, and create a new CloudWatch Events client.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create the cloudwatch events client svc := cloudwatchevents.New(sess)

Call PutTargets, supplying a name for the rule, DEMO_EVENT. For the target, specify the ARN of the Lambda function you created, LAMBDA_FUNCTION_ARN, and the ID of the rule, myCloudWatchEventsTarget. Print any errors, or a success message with any targets that failed.

result, err := svc.PutTargets(&cloudwatchevents.PutTargetsInput{ Rule: aws.String("DEMO_EVENT"), Targets: []*cloudwatchevents.Target{ &cloudwatchevents.Target{ Arn: aws.String("LAMBDA_FUNCTION_ARN"), Id: aws.String("myCloudWatchEventsTarget"), }, }, }) fmt.Println("Success", result)

See the complete example on GitHub.

Send Events

Choose Copy to save the code locally.

Create the file events_put_events.go. Import the packages used in the example.

import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/cloudwatchevents" "fmt" )

Initialize a session that the SDK will use to load credentials from the shared credentials file ~/.aws/credentials, load your configuration from the shared configuration file ~/.aws/config, and create a CloudWatch Events client.

sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create the cloudwatch events client svc := cloudwatchevents.New(sess)

Call PutEvents, supplying key-name value pairs in the Details field, and specifying the ARN of the Lambda function you created, RESOURCE_ARN. See PutEventsRequestEntry for a description of the fields. Finally, display the ingested events.

result, err := svc.PutEvents(&cloudwatchevents.PutEventsInput{ Entries: []*cloudwatchevents.PutEventsRequestEntry{ &cloudwatchevents.PutEventsRequestEntry{ Detail: aws.String("{ \"key1\": \"value1\", \"key2\": \"value2\" }"), DetailType: aws.String("appRequestSubmitted"), Resources: []*string{ aws.String("RESOURCE_ARN"), }, Source: aws.String("com.company.myapp"), }, }, }) fmt.Println("Ingested events:", result.Entries)

See the complete example on GitHub.