Skip to content

L2 CDK Constructs

0

How to create level 2 constructs in the CDK? What are the key steps involved, and are there any best practices or tips to keep in mind when developing them?

asked 2 years ago461 views

2 Answers
1

Creating Level 2 (L2) constructs in the AWS Cloud Development Kit (CDK) involves several key steps and best practices:

  1. Extend the Construct class: L2 constructs should directly extend the Construct class from the CDK library.

  2. Abstract L1 constructs: L2 constructs are abstractions of L1 constructs, providing a higher-level, more intuitive API. They should call their corresponding L1 constructs in their constructors upon instantiation.

  3. Provide default properties: Implement common property settings as defaults, often conditional in nature, to simplify the configuration process for users.

  4. Use TypeScript types, interfaces, and enums: Leverage TypeScript's type system to define the structure of data and configurations, making the construct more robust and easier to use.

  5. Implement helper methods: Create methods that allow for easier manipulation of resource configurations after instantiation.

  6. Consider static methods: Use static methods to provide easier ways to reference imported resources.

  7. Create helper classes: For more complex use cases, develop helper classes that offer the full capabilities of a TypeScript class.

  8. Implement best practices: Ensure your L2 construct implements AWS best practices and common patterns for the specific service or resource it represents.

  9. Document thoroughly: Provide clear and comprehensive documentation for your construct, including usage examples and explanations of any default behaviors.

  10. Test extensively: Write unit tests and integration tests to ensure your construct behaves as expected in various scenarios.

  11. Consider reusability: Design your L2 construct to be as reusable as possible, allowing for easy customization without sacrificing simplicity.

  12. Follow CDK conventions: Adhere to the naming conventions and coding standards used in the official CDK library for consistency.

When developing L2 constructs, it's beneficial to study existing L2 constructs in the CDK library to understand common patterns and best practices. Additionally, consider contributing your L2 constructs back to the CDK community if they solve a common use case.

Remember that L2 constructs should provide a balance between abstraction and flexibility, making it easier for users to work with AWS resources while still allowing for customization when needed.

Sources
Developing CDK Constructs | AWS re:Post
Layer 2 constructs - AWS Prescriptive Guidance
Best practices - AWS Prescriptive Guidance

answered 2 years ago

EXPERT

reviewed 2 years ago

0
Accepted Answer

You can follow below guidance for creating L2 Contructs: https://docs.aws.amazon.com/prescriptive-guidance/latest/aws-cdk-layers/layer-2.html

The contributing document may be helpful to keep in mind when developing. https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md It is a good idea to see what other developers are doing in terms of development. https://constructs.dev/search?q=&cdk=aws-cdk&cdkver=2&sort=downloadsDesc&offset=0&tags=community

AWS

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.