In order to make sure the requests wasn't getting gzipped by default and, later on, that the custom event lister did actually gzip the request payload, i created a simple debug Node.js web server:
const { createServer } = require('http');
createServer((req, res) => {
console.log('--------');
console.log(JSON.stringify(req.headers, null, 2));
req.pipe(process.stdout);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ok:true}));
})
.listen(8000);
Then in python:
import boto3
import gzip
endpoint = "http://localhost:8000/"
cw_client = boto3.client('cloudwatch', endpoint_url=endpoint, use_ssl=False)
event_system = cw_client.meta.events
def gzip_request_body(request, **kwargs):
gzipped_body = gzip.compress(request.body)
request.headers['Content-Encoding'] = 'gzip'
request.headers['X-Test'] = 'FOOOBAAAAR' # to make sure also headers are modified
request.data = gzipped_body
return request
event_system.register('before-sign.cloudwatch.PutMetricData', gzip_request_body)
import random
cw_client.put_metric_data(
MetricData = [
{
'MetricName': 'KPIs',
'Dimensions': [
{
'Name': 'PURCHASES_SERVICE',
'Value': 'CoolService'
},
{
'Name': 'APP_VERSION',
'Value': '1.0'
},
],
'Unit': 'None',
'Value': random.randint(1, 500)
},
],
Namespace='CoolApp'
)