Executing AWS Cloud Watch APIs through HTTP requests

Viewed 311

I am currently trying to execute the AWS cloud watch APIs and get the logs from the cloud watch environment. I have successfully implemented this by using the SDK provided by AWS. Also in the documentations they mentioned that we can also use HTTP requests to get the logs from cloud watch. In the following documentation they have mentioned the params that we have to pass from the request.

https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_FilterLogEvents.html.

The problem is there are also some common params to be sent from the request. I need to clarify the way that we want to send them. As query params, path params or request headers?

1 Answers

The request parameters for the Cloud Watch Rest API should be sent in the JSON format inside the {}, similar to the way in the POST example given. Of these only logGroupName is required, while the other paramaters mentioned are optional:

**

endTime 
filterPattern
interleaved
limit
logGroupName
logStreamNamePrefix
logStreamNames
nextToken
startTime

**

In the context of the entire HTTP request:

POST / HTTP/1.1
Host: logs.<region>.<domain>
X-Amz-Date: <DATE>
Authorization: AWS4-HMAC-SHA256 Credential=<Credential>, SignedHeaders=content-type;date;host;user-agent;x-amz-date;x-amz-target;x-amzn-requestid, Signature=<Signature>
User-Agent: <UserAgentString>
Accept: application/json
Content-Type: application/x-amz-json-1.1
Content-Length: <PayloadSizeBytes>
Connection: Keep-Alive
X-Amz-Target: Logs_20140328.FilterLogEvents
 {
   "endTime": number,
   "filterPattern": "mystring",
   "interleaved": boolean,
   "limit": number,
   "logGroupName": "string",
   "logStreamNamePrefix": "string",
   "logStreamNames": [ "string" ],
   "nextToken": "string",
   "startTime": number
}

The Common Parameters are sent as HTTP params as seen in the example above. They are needed here for signing your requests to AWS with the proper authentication. (Authentication occurs automatically in the background when using the CLI) This is an official walkthrough of how to construct a canonical,signed HTTP request for AWS APIs

For example:

Action=ListUsers&
Version=2010-05-08&
X-Amz-Algorithm=AWS4-HMAC-SHA256&
X-Amz-Credential=AKIDEXAMPLE%2F20150830%2Fus-east-1%2Fiam%2Faws4_request&
X-Amz-Date=20150830T123600Z&
X-Amz-SignedHeaders=content-type%3Bhost%3Bx-amz-date
Related