liveBook · Manning

Mediator pattern

As explained in Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform

A mediator pattern organizes dependencies among infrastructure resources and includes logic to create or delete objects based on their dependencies.

The mediator pattern organizes dependencies among infrastructure resources and creates or deletes objects in dependency order. It acts as the authority for coordinating resources such as networks, servers, and firewall rules. section 4.5

Apply the mediator by making dependency order explicit:

  • Create the network before a server. section 4.5
  • Create the network and server before a firewall rule. section 4.5
  • Use dependency injection to abstract and control resource relationships. section 4.5
  • Repeat operations idempotently so existing resources converge on the same target state. section 4.5

Python resource mediator

A Python mediator checks the requested resource type, recursively creates its low-level dependencies, and then creates the requested object. A load balancer can extend the sequence after the server or firewall. section 4.5

Figure 4.12 The mediator becomes the authority on which resource to configure first.

Use the workflow diagram to inspect how the mediator becomes the authority on which infrastructure resource is configured first. section 4.5

Choose a mediator when many dependencies connect multiple modules or resources. It improves scalability and change isolation, but requires substantial implementation, testing, and troubleshooting effort; an untested mediator can create incorrect infrastructure. section 4.6

How Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform covers it

How deep it goes
Explains dependency ordering, recursive creation, idempotency, and trade-offs, but leaves complete mediator testing to implementation.
Examples
Uses networks, servers, firewall rules, Python factory modules, and an optional load balancer sequence.
What it assumes
Assumes familiarity with infrastructure resources and dependencies while demonstrating dependency injection through network, server, and firewall examples.
What you can do after
Build it from scratch by coordinating resource factories in dependency order and preserving repeatable target-state execution.
How it connects
Connects dependency injection, idempotency, composability, facade, adapter, and infrastructure as code.

Also explained in

Compare how 2 books explain mediator pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Infrastructure as Code, Patterns and Practices: With examples in Python and Terraform. The section links point to the parts of the book it draws on.