Search code examples
c++amazon-web-servicesaws-sdkamazon-cloudwatchaws-sdk-cpp

Metric data goes to wrong region in AWS SDK for C++ CloudWatch example


The "AWS SDK for C++ Documentation Examples" README says:

To build and run these examples, you'll need: ... AWS credentials, either configured in a local AWS credentials file or by setting the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/cpp

Credentials file:

$ cat ~/.aws/credentials
[default]
aws_access_key_id = ...
aws_secret_access_key = ...
output = text
region = us-west-2

The AWS client command works correctly:

$ aws cloudwatch put-metric-data \
--namespace aws-sdk-cpp-test \
--metric-name "New Posts" \
--timestamp "$(date)" \
--value 0.7 \
--unit Count

However, if I run the cw_put_metric_data example, metrics go to us-east-1 instead of us-west-2.

EDIT: Aws::CloudWatch::CloudWatchClient is instantiated with the default Aws::Client:ClientConfiguration in which region is hard-wired to US_EAST_1. So the question is how to make use of ~/.aws/credentials?


Solution

  • You can use this class:

    https://github.com/aws/aws-sdk-cpp/blob/master/aws-cpp-sdk-core/include/aws/core/config/AWSProfileConfigLoader.h#L59

    Then use the values there to pass to your client configuration.