Skip to content

Answer: What Happens Without the Preprocessor?

The Short Answer

The deferred binding pattern with parent adoption is so robust that code works even WITHOUT explicit binding calls!

If a developer writes:

@autoscope
class MyStack(Stack):
    def __init__(self):
        super().__init__()
        self.vpc = Resource(type="aws_vpc", cidr_block="10.0.0.0/16")
        #         ^ No (nf_id) call - resource is not yet bound

This actually WORKS because: 1. The deferred resource detects the stack template as its parent (via @autoscope) 2. It registers itself in the Stack's deferred-children list 3. When the Stack is bound, it automatically finds self.vpc and binds it with ID "vpc" 4. The attribute name becomes the resource ID!

Test Results

✅ Parent Adoption Works

# Without explicit binding - still works!
self.vpc = Resource(type="aws_vpc", cidr_block="10.0.0.0/16")
# After stack binding: vpc.nf_id == "vpc" (from attribute name)

❌ What Doesn't Work

# Not assigned to self - resource is lost!
vpc = Resource(type="aws_vpc", cidr_block="10.0.0.0/16")
# This resource won't be accessible as stack.vpc

The Preprocessor's Role

The preprocessor with Bind annotation is actually optional for basic cases:

Without Preprocessor (Works!)

self.vpc = Resource(type="aws_vpc", cidr_block="10.0.0.0/16")
# Parent adoption handles binding automatically

With Preprocessor (Also Works!)

self.vpc: Bind = Resource(type="aws_vpc", cidr_block="10.0.0.0/16")
# Transforms to: self.vpc = Resource(...)("vpc")
# More explicit but not strictly necessary

When You NEED Explicit Binding

  1. Custom IDs: When you want an ID different from the attribute name

    self.primary = Resource(...)("main-vpc")  # ID is "main-vpc", not "primary"
    

  2. Dynamic Binding: When binding after the parent is bound

    def add_resource(self):
        resource = Resource(...)("custom-id", scope=self)
    

  3. Non-Attribute Resources: Resources not stored as attributes

    resources = [Resource(...)("vpc-{}".format(i)) for i in range(3)]
    

Conclusion

The deferred binding pattern with parent adoption is remarkably forgiving:

  • Without preprocessor: Code works as long as resources are assigned to self.attribute
  • With preprocessor: Adds explicit binding for clarity and edge cases
  • Synthesis works: Parent adoption ensures all resources are bound before synthesis

The system is designed to "just work" with natural Python patterns. The preprocessor enhances the experience but isn't strictly required for basic use cases.

Important Note

While the parent adoption mechanism makes the code work without explicit binding, using the preprocessor with Bind annotations is still recommended because:

  1. Explicit is better than implicit (Zen of Python)
  2. Custom IDs are easier to specify
  3. Type hints work better with Bind
  4. Consistency across the codebase
  5. Future-proofing for more complex patterns