Should we test AWS CDK code? This is a question that many developers grapple with when working with the AWS Cloud Development Kit (CDK). The CDK allows developers to define cloud infrastructure in code and provision resources on AWS using familiar programming languages. But when it comes to testing this code, there are varying opinions.
Hatched by tfc
May 04, 2024
3 min read
11 views
Should we test AWS CDK code? This is a question that many developers grapple with when working with the AWS Cloud Development Kit (CDK). The CDK allows developers to define cloud infrastructure in code and provision resources on AWS using familiar programming languages. But when it comes to testing this code, there are varying opinions.
Broadly speaking, a CDK program can be either a library of patterns or a setup for your project cloud. In the former case, unit tests with fine-grained assertions are a must-have. These tests help ensure that the patterns, which are reusable high-level components, function as expected. By testing these patterns, developers can catch any bugs or issues before deploying them in their projects.
On the other hand, if the CDK program is a setup for your project cloud, extensive testing may not be necessary. This is because the CDK itself goes through rigorous testing and is maintained by AWS. The CDK team is responsible for ensuring that the infrastructure-as-code framework is robust and reliable. Therefore, relying on the CDK's testing and quality assurance processes may be sufficient.
However, there are scenarios where testing the business logic within the CDK program is essential. If your program contains complicated business logic, it is crucial to test that logic thoroughly. This is because the CDK may not be aware of the specific requirements and intricacies of your business logic. By writing tests for the business logic, developers can ensure that their code behaves as expected and handles edge cases correctly.
In addition to testing the CDK code itself, another important aspect to consider is onboarding and identity management. When dealing with a multi-tenant environment, it is crucial to have a robust system for onboarding new tenants and managing their identities. This includes aspects such as tenant isolation, data partitioning, tiering, and billing.
One approach to handling onboarding and identity management in a multi-tenant environment is to configure Cognito with custom claims that hold the tenant context. Cognito is a fully managed service by AWS that provides user authentication and authorization. By configuring Cognito to use custom claims, developers can ensure that tenant-specific information is embedded in the authentication tokens.
Furthermore, Cognito can be configured in a federated mode, where it uses another identity provider for authentication. This allows developers to leverage an external identity provider while still managing the custom claims through Cognito. By merging the custom claims into the tokens returned from the authentication process, developers can maintain control over the tenant context while benefiting from the features offered by an external identity provider.
In conclusion, the decision of whether to test AWS CDK code depends on the specific context of the code and the presence of complicated business logic. While extensive testing may not be necessary for CDK patterns, it is crucial to thoroughly test the business logic within the CDK program. Additionally, when working with a multi-tenant environment, configuring Cognito with custom claims and leveraging a federated identity provider can help streamline onboarding and identity management.
Actionable advice:
- When working with CDK patterns, focus on writing unit tests with fine-grained assertions to ensure their proper functionality.
- For CDK programs that involve complicated business logic, invest time in writing thorough tests to catch any bugs or issues specific to your application.
- When dealing with a multi-tenant environment, consider configuring Cognito with custom claims and leveraging a federated identity provider for efficient onboarding and identity management.
By following these actionable advice, developers can ensure the reliability and robustness of their AWS CDK code while minimizing the risk of issues in production.
Sources
Hatch New Ideas with Glasp AI 🐣
Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)
Start Hatching 🐣