Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Cloudwatch output plugin Connect() #3335

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/outputs/cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ API endpoint. In the following order the plugin will attempt to authenticate.
5. [Shared Credentials](https://github.com/aws/aws-sdk-go/wiki/configuring-sdk#shared-credentials-file)
6. [EC2 Instance Profile](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html)

The IAM user needs only the `cloudwatch:PutMetricData` permission.

## Config

For this output plugin to function correctly the following variables
Expand Down
16 changes: 8 additions & 8 deletions plugins/outputs/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/sts"

"github.com/influxdata/telegraf"
internalaws "github.com/influxdata/telegraf/internal/config/aws"
Expand Down Expand Up @@ -71,21 +72,20 @@ func (c *CloudWatch) Connect() error {
}
configProvider := credentialConfig.Credentials()

svc := cloudwatch.New(configProvider)
stsService := sts.New(configProvider)

params := &cloudwatch.ListMetricsInput{
Namespace: aws.String(c.Namespace),
}
params := &sts.GetSessionTokenInput{}

_, err := svc.ListMetrics(params) // Try a read-only call to test connection.
_, err := stsService.GetSessionToken(params)

if err != nil {
log.Printf("E! cloudwatch: Error in ListMetrics API call : %+v \n", err.Error())
log.Printf("E! cloudwatch: Cannot use credentials to connect to AWS : %+v \n", err.Error())
return err
}

c.svc = svc
c.svc = cloudwatch.New(configProvider)

return err
return nil
}

func (c *CloudWatch) Close() error {
Expand Down