Menu
AWS SDK for Go
Developer Guide

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.

Copy
{ "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.

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

Create a Scheduled Rule

Create a new Go file named events_schedule_rule.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials, and create a new Amazon EC2 service client.

Copy
func main() { // Load session from shared config. sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) // Create the cloudwatch events client svc := cloudwatchevents.New(sess)

Call PutRule, supplying a name, ARN of the IAM role you created, and an expression defining the schedule. Print any errors, or a success message.

Copy
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"), }, }, })

Add a Lambda Function Target

Create a new Go file named events_put_targets.go.

Call the PutRule method to create the rule. The method returns the ARN of the new or updated rule.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials, and create a new Amazon EC2 service client.

Copy
func main() { // Load session from shared config. 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. For the target, specify the ARN of the Lambda function you created, and the ID of the rule. Print any errors, or a success message.

Copy
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"), }, }, }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", result) }

Send Events

Create a new Go file named events_put_events.go.

You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

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

Initialize a session that the SDK will use to load credentials from the shared credentials file, ~/.aws/credentials, and create a new Amazon EC2 service client.

Copy
func main() { // Load session from shared config. 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. See PutEventsRequestEntry for a description of the fields. Print out any errors, or a success message.

Copy
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"), }, }, }) if err != nil { fmt.Println("Error", err) return } fmt.Println("Success", result) }