forked from awsdocs/aws-doc-sdk-examples
/
CreateRule.go
80 lines (68 loc) · 2.45 KB
/
CreateRule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudwatch.go.create_rule]
package main
// snippet-start:[cloudwatch.go.create_rule.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
)
// snippet-end:[cloudwatch.go.create_rule.imports]
// CreateRule creates a rule that watches for events on a schedule.
// Inputs:
//
// sess is the current session, which provides configuration for the SDK's service clients
// ruleName is the name of the rule
// roleARN is the ARN of the role
// schedule is the schedule expression
//
// Output:
//
// If successful, a PutRuleOutput and nil
// Otherwise, nil and the error from a call to PutRule
func CreateRule(sess *session.Session, ruleName *string, roleARN *string, schedule *string) (*cloudwatchevents.PutRuleOutput, error) {
// Create the service client
// snippet-start:[cloudwatch.go.create_rule.call]
svc := cloudwatchevents.New(sess)
result, err := svc.PutRule(&cloudwatchevents.PutRuleInput{
Name: ruleName,
RoleArn: roleARN,
ScheduleExpression: schedule,
})
// snippet-end:[cloudwatch.go.create_rule.call]
if err != nil {
return nil, err
}
return result, nil
}
func main() {
// snippet-start:[cloudwatch.go.create_rule.args]
ruleName := flag.String("r", "", "The name of the rule")
roleARN := flag.String("a", "", "The ARN of the role")
schedule := flag.String("s", "", "The schedule expression")
flag.Parse()
if *ruleName == "" || *roleARN == "" || *schedule == "" {
fmt.Println("You must supply a rule name (-r RULE), role ARN (-a ROLE-ARN), and schedule expression (-s EXPRESSION)")
return
}
// snippet-end:[cloudwatch.go.create_rule.args]
// Initialize a session that the SDK uses to load
// credentials from the shared credentials file (~/.aws/credentials)
// snippet-start:[cloudwatch.go.create_rule.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[cloudwatch.go.create_rule.session]
result, err := CreateRule(sess, ruleName, roleARN, schedule)
if err != nil {
fmt.Println("Got an error creating the rule:")
fmt.Println(err)
return
}
// snippet-start:[cloudwatch.go.create_rule.print]
fmt.Println("Rule ARN:" + *result.RuleArn)
// snippet-end:[cloudwatch.go.create_rule.print]
}
// snippet-end:[cloudwatch.go.create_rule]