Search code examples
amazon-web-servicescurlmetricsamazon-cloudwatch

POST custom metric with curl to cloudwatch


I want to log results of an api call to cloud watch. What I want to do is to post consume metrics to cloud watch. My architecture is designed to sit on elastic beanstalk, so I prefer to install as little as possible on it.

I am trying to understand is it possible to post the cloud watch with simple CURL POST.

I have this tutorial.

I don't really understand this example. Can I to it with post? (looks like get method). And what is the endpoint?

When I tried : curl -X POST https://monitoring.&api-domain;/doc/2010-08-01/?Action=PutMetricData&Version=2010-08-01&Namespace=TestNamespace&MetricData.member.1.MetricName=buffers&MetricData.member.1.Unit=Bytes&&MetricData.member.1.Dimensions.member.1.Name=InstanceType&MetricData.member.1.Dimensions.member.1.Value=m1.small&AUTHPARAMS

I got this error: 'api-domain' is not recognized as an internal or external command, operable program or batch file. 'Version' is not recognized as an internal or external command, operable program or batch file. 'Namespace' is not recognized as an internal or external command, operable program or batch file. 'MetricData.member.1.MetricName' is not recognized as an internal or external command, operable program or batch file. 'MetricData.member.1.Unit' is not recognized as an internal or external command, operable program or batch file. 'MetricData.member.1.Dimensions.member.1.Value' is not recognized as an internal or external command, operable program or batch file. 'AUTHPARAMS' is not recognized as an internal or external command, operable program or batch file.

Please don't tell me to use aws cli. I know I can use it. I want to try not to use it.


Solution

  • Here is the documentation explaining how to make POST requests: https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/making-api-requests.html#CloudWatch-API-requests-using-post-method

    Here is a list of endpoints per region: https://docs.aws.amazon.com/general/latest/gr/rande.html#cw_region

    Resulting curl will look something like this:

    curl -X POST \
      https://monitoring.us-east-1.amazonaws.com \
      -H 'Accept: application/json' \
      -H 'Authorization: AWS4-HMAC-SHA256 Credential=YOUR_ACCESS_KEY_GOES_HERE/20190326/us-east-1/monitoring/aws4_request, SignedHeaders=accept;content-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=SIGV4_SIGNATURE_GOES_HERE' \
      -H 'Content-Encoding: amz-1.0' \
      -H 'Content-Length: 141' \
      -H 'Content-Type: application/json' \
      -H 'X-Amz-Date: 20190326T071934Z' \
      -H 'X-Amz-Target: GraniteServiceVersion20100801.PutMetricData' \
      -H 'host: monitoring.us-east-1.amazonaws.com' \
      -d '{
        "Namespace": "StackOverflow",
        "MetricData": [
            {
                "MetricName": "TestMetric",
                "Value": 123.0
            }
        ]
    }'
    

    Note that Authorization header in the example above has two placeholders, YOUR_ACCESS_KEY_GOES_HERE and SIGV4_SIGNATURE_GOES_HERE. These are the access key from the credentials you'll be using to sign the requests and the signature you'll have to construct using this algorithm: https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html and this is one of the reasons why using the CLI or SDKs is the recommended way of making requests.