In this post we are going to take a pragmatic approach on monitoring as code with CDK. As you might already know, you can visually design dashboards in AWS console. Nonetheless, here we want to do it using infrastructure as code. CDK offers two types of construct, level 1 and level 2. Level 1 constructs mostly begin with a cfn and are basically a wrapper for a cloud formation template. Level 2 constructs are more high level and tend to abstract a lot of the boilerplate code. In most cases, the level 2 constructs are more convenient to use, but with monitoring is a bit different. Using level 2 constructs you cannot benefit from the designer aws provides you with. And as you can imagine, it is not an easy task to design a dashboard without a designer. Depending on how you implement your dashboards and work with your team, you might be better of using level 1 constructs. Before we begin, I must mention that you need to tag all the resources that you want to monitor using this method. Having that out of the way, we start with creating a dashboard in cloud watch. Go to Cloudwatch -> Dashboards -> Create Dashboard. Give it a name and click the create button. Create a blank widget and go to the explorer pane. There you can choose the metrics and resources you want to monitor. Also, adding and aggregating based on the values of the tabs.
After defining the dashboard, you can go ahead and export it and save it for later. To do that, click on Actions, then view/edit source.
After defining that, we go ahead and define our CDK project.
cdk init --language=typescript.
Then we go to package.json to declare the dependencies for our project. You need the following dependencies:
"@aws-cdk/core"
"@aws-cdk/aws-cloudwatch"
So please either use npm to install them or use package.json to declare them.
Then import:
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
Then we use the L1 CfnDashboard construct to create the dashboard. Please paste the json code that you copied from th previous step in the dashboard body.
const cfnDashboard = new cloudwatch.CfnDashboard(this, 'MyCfnDashboard', {
dashboardBody: 'the json code you copied earlier',
dashboardName: 'dashboardName',
});
That is pretty much it. Now you have a implemented a monitoring solution on AWS and you can deploy it in other accounts.
Now we are ready to verify our cdk stack. First run ‘npm run build’ which compiles typescript to javascript. Then run cdk synth. This should give a cloud formation template. If it returns an error, go to previous steps and verify your work. Before you can deploy this application, you need to bootstrap your environment. To do that, run the following.
cdk bootstrap aws://youraccountID/yourregion
set CDK_NEW_BOOTSTRAP=1
cdk bootstrap aws://youraccountID/yourregion
Now you are ready to deploy your cdk app.
cdk deploy
Some more useful cdk commands:
cdk ls //lists the stacks in your application
cdk diff //shows the difference of your current deployed stack and your local stack
cdk doctor //*kind of* verifies your cdk stack for warnings
That is it. Thank you very much for reading this post.