Using the Cloudwatch SDK
Get familiar with the cloudwatch SDK to retrieve metric statistics about our application.
We'll cover the following...
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.
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 thefp-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 function with the AWS SDK call to the bare minimum. Splitting up this function into pure and impure parts ...