Before we move forward, we need to talk about testability. AWS CDK gives us unique selling points and mechanisms related to testing. Let’s review them!
The main problem with the Feedback Loop
If you are working with the AWS CloudFormation, you probably do not need to read another paragraph at all - you already feel the pain.
When working with templates and iterating with infrastructure scripts, time is crucial. In most cases, we need to update or create a stack, wait, and we will discover errors in the runtime one by one. This is tedious, problematic, and wasteful.
With pure CloudFormation, you have to fallback to additional tools or use a totally different tool (e.g., Terraform) to achieve testability and shortening the feedback loop.
Luckily, we have exciting options related to testing tools.
Unit Testing in AWS CDK
We need to start with an explanation that the unit tests are verifying the generated output and templates. This may sound like a simplistic and crude approach, but actually, it allows for more, as we can perform the following elements:
- Evaluating differences and snapshots (which represents the frozen state of the infrastructure graph after synthesis).
- Very precise assertions about resources and their condition.
- Testing validation rules (e.g., length of the attributes - like name and similar), passed parameters, and constructs.
We will see all those techniques in practice in future blog posts!
Please bear in mind that all we talked about do not invalidate the need for integration tests. It helps with testability. It shortens the feedback loop and builds a safety net for our infrastructure scripts and infrastructure as code.