Best practices for developing cloud applications with AWS CDK

To Nha Notes | April 1, 2022, 2:39 p.m.

Every application starts with a single package in a single repository

Though possible, we generally don’t recommend having multiple applications in the same repository, especially when using automated deployment pipelines, because this increases the blast radius of changes during deployment. If multiple applications are in a repository, the following occurs:

  • Changes to one application trigger deployment of the other ones, even though nothing changed
  • If changes to one application break the build, the other application can no longer be deployed either
Configure with APIs (properties, methods), not environment variables

One of the common anti-patterns that we see is environment variable lookups inside constructs and stacks. Both of these should accept a properties object in the constructor that allows for full configurability, rather than relying on an environment variable on the target machine. If you reference any environment variables, they should be limited to the very top level of your application, and even there these lookups should be limited to the configuration of local development stacks.

Separate your application stage into multiple stacks when it’s dictated by deployment requirements
  • It’s typically easiest to keep as many resources in the same stack as possible, so keep them together unless you know you want them separated.
  • It’s a good idea to keep stateful resources (like databases) separated from the stateless resources. You can turn on termination protection on the stack with stateful resources, and can freely destroy or create multiple copies of the stack with stateless resources without risk of data loss.
  • Stateful resources are also more sensitive to construct renaming—renaming leads to resource replacement—so it makes sense not to nest them too much into other constructs that are likely to be moved around or renamed (unless, of course, the state is a temporary state that can be rebuilt if lost, like a cache).

 

References

https://aws.amazon.com/blogs/devops/best-practices-for-developing-cloud-applications-with-aws-cdk/

https://aws.amazon.com/blogs/devops/align-with-best-practices-while-creating-infrastructure-using-cdk-aspects/