Search⌘ K
AI Features

Using the Cloudwatch SDK

Explore how to integrate AWS Cloudwatch SDK within a TypeScript functional programming context. Learn to retrieve Lambda function metrics using promises, organize pure and impure code, and apply transformations without monads to handle statistical data efficiently.

Retrieving the metric information

The most important call for our project will be getMetricStatistics from the AWS Node SDK, which “gets statistics for the specified metric,” according to AWS documentation. Given the right parameters, we can retrieve our three relevant metrics for a function using this call. Let’s start by writing one big function for retrieving the metric information.

TypeScript 3.3.4
const getMetrics = (functionName: string, start: Date, end: Date) => {
const params = {
// build parameters here
};
cloudwatch.getMetricStatistics(params).promise()
.then(data => {
// do stuff with data
})
.catch(err => {
// do stuff with the error
});
}
  • Line 8: By default, the AWS SDK uses callbacks. But, if we add promise(), it uses promises instead (in v3, promises have become the default). Promises are often better than callbacks and, in our case, they’ll also play nice with some of the fp-ts async stuff we’ll be using.
  • Lines 9 and 12: We’re using arrow style for this function, and will do so frequently in upcoming lessons.

This method is fine for writing a proof of concept. It has some negatives, though. You might’ve already guessed a few. The most important downside is that this function is impure because we’re performing I/O. Furthermore, there are many pure parts within the function, which we could’ve extracted with little effort, reducing the ...