L1, L2, and L3 Constructs in AWS CDK - krdheeraj51/aws-labs GitHub Wiki

Introduction

AWS Cloud Development Kit (CDK) provides three levels of constructs:
L1 (Low-Level Constructs), L2 (High-Level Constructs), and L3 (Patterns).
These constructs define different abstraction levels for AWS infrastructure provisioning using CDK.


L1 Constructs (Low-Level Constructs)

L1 constructs are direct CloudFormation resource representations in AWS CDK.
They provide a low-level abstraction and require detailed configuration.

Characteristics:

  • Directly maps to AWS CloudFormation resources.
  • Uses Cfn prefix (e.g., CfnBucket for S3).
  • Requires explicit definitions for properties.

Example:

from aws_cdk import core
from aws_cdk import aws_s3 as s3

class MyStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs):
        super().__init__(scope, id, **kwargs)

        bucket = s3.CfnBucket(self, "MyL1Bucket",
            bucket_name="my-l1-bucket",
            versioning_configuration={"status": "Enabled"}
        )
  • L1 constructs are low-level constructs that directly map to AWS CloudFormation resources.

Characteristics:

  • L1 is more verbose: Requires explicit property definitions.
  • Example: A VPC in L1 may require 282 lines of code.

L2 Constructs (High-Level Constructs)

L2 constructs offer a higher-level abstraction over L1 constructs, providing intelligent defaults to simplify resource creation.

Characteristics:

  • Simplified API with sensible defaults.
  • More concise and easier to manage than L1.
  • Reduces complexity in defining AWS resources.

Example:

from aws_cdk import aws_s3 as s3

class MyStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs):
        super().__init__(scope, id, **kwargs)

        bucket = s3.Bucket(self, "MyL2Bucket",
            bucket_name="my-l2-bucket",
            versioned=True
        )
  • Same S3 bucket, but with a much simpler syntax.
  • More concise: The same VPC in L2 may need just 3 lines of code instead of 282.

L3 Constructs (Patterns)

  • L3 constructs, also called patterns, are predefined solutions for common use cases. They combine multiple L2 constructs into ready-to-use architecture patterns.

Characteristics:

  • Combines multiple L2 constructs into one pattern.
  • Even higher-level abstraction than L2.
  • Useful for common AWS architectural patterns.

Same S3 bucket, but with a much simpler syntax

  • More concise: The same VPC in L2 may need just 3 lines of code instead of 282.

L3 Constructs (Patterns)

L3 constructs, also called patterns, are predefined solutions for common use cases. They combine multiple L2 constructs into ready-to-use architecture patterns.

Characteristics:

  • Combines multiple L2 constructs into one pattern.
  • Even higher-level abstraction than L2.
  • Useful for common AWS architectural patterns.

Example:

from aws_cdk import aws_apigateway as apigateway
from aws_cdk import aws_lambda as lambda_

class MyStack(core.Stack):
    def __init__(self, scope: core.Construct, id: str, **kwargs):
        super().__init__(scope, id, **kwargs)

        handler = lambda_.Function(self, "MyLambda",
            runtime=lambda_.Runtime.PYTHON_3_9,
            code=lambda_.Code.from_asset("lambda"),
            handler="index.handler"
        )

        api = apigateway.LambdaRestApi(self, "MyApi",
            handler=handler
        )
  • Preconfigured API Gateway with Lambda.
  • Combines multiple resources (API Gateway, Lambda, IAM Roles).
  • Other examples:
    • aws_cdk.aws_ecs_patterns.ApplicationLoadBalancedFargateService
    • aws_cdk.aws_apigateway.LambdaRestAPI

Key Differences Between L1, L2, and L3 Constructs

Feature L1 Constructs L2 Constructs L3 Constructs
Abstraction Level Low Medium High
Mapping 1:1 with CloudFormation CloudFormation with built-in defaults Predefined patterns for use cases
Naming Convention Uses Cfn prefix (e.g., CfnBucket) Uses direct AWS SDK-like naming (e.g., Bucket) Pattern-based (e.g., LambdaRestAPI)
Code Complexity High (detailed configuration required) Medium (intelligent defaults) Low (fully managed patterns)
Use Case Full control over AWS resources Simplified configuration with defaults Pre-built solutions for common patterns

When to Use Each Construct?

  • L1 Constructs: When you need full control and a 1:1 CloudFormation mapping.
  • L2 Constructs: When you want simplicity with best-practice defaults.
  • L3 Constructs: When you need predefined architectures for common workloads.

Conclusion

Understanding L1, L2, and L3 constructs helps in choosing the right abstraction level for AWS CDK projects.

  • L1 offers full control but is verbose.
  • L2 simplifies configurations with defaults.
  • L3 provides reusable patterns to speed up deployments.

By using the appropriate construct level, developers can reduce complexity, improve maintainability, and accelerate cloud infrastructure deployment.