Issue
I tried to use the AWS SDK for JavaScript in Appsmith, but I received an error message saying "XMLHttpRequest is not a constructor." Upon further research, I found out that XHR is disabled on the Appsmith platform for security reasons, which may cause issues with certain methods in the AWS library. As a workaround, I can try using the AWS APIs for methods that rely on XHR.
Resolution
The issue with the AWS SDK for JavaScript and Appsmith is that Appsmith disables XHR (XMLHttpRequest) for security reasons, and some methods in the AWS library rely on it. A workaround for this is to use the AWS APIs for methods that depend on XHR. For example, instead of using cw.listMetrics()
as in the previous code, you could use cw.getMetricData()
which does not rely on XHR.
A modified version of the code using getMetricData()
instead of listMetrics()
would look like this:
metrics: async () => {
AWS.config.region = 'eu-west-3';
AWS.config.credentials = new AWS.Credentials("SECRET","SECRET");
var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'});
var params = {
StartTime: new Date(new Date().setMinutes(new Date().getMinutes() - 5)), // replace with your own timestamp
EndTime: new Date(), // replace with your own timestamp
MetricDataQueries: [
{
Id: 'm1',
MetricStat: {
Metric: {
Namespace: 'AWS/Logs',
MetricName: 'IncomingLogEvents',
Dimensions: [
{
Name: 'LogGroupName',
Value: 'your-log-group-name' // replace with your own
}
]
},
Period: 300, // 5 minutes
Stat: 'Sum', // Replace with a valid statistic
},
},
],
ScanBy: 'TimestampDescending',
};
cw.getMetricData(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Metrics", JSON.stringify(data.MetricDataResults));
}
});
}
Note that there are additional parameters and optional values that can be set for getMetricData()
that are not included in this example. Be sure to consult the AWS SDK documentation to tailor the method for your specific use case.