A dependency is a relationship in which one component requires another component’s existence or attributes before it can build, change, or operate. In infrastructure, a server may depend on a network, while an application may depend on a service account and database proxy. section 4.0 section 11.2.2
Manage dependencies as explicit, changeable relationships:
- Pass identifiers and attributes as variables or injected module inputs instead of hardcoding them; this loosens coupling and supports replacement. section 2.3.5 section 10.2.3
- Map expected dependencies before changing or deleting a resource, then verify that actual dependencies match the configuration. section 11.2.2 section 12.2.1
- Test dependency boundaries with contract tests, then use integration and end-to-end tests when modules or environments communicate. section 6.7.1 section 6.7.2
- Change low-level dependencies first, test dependent resources afterward, and isolate replacements when using immutable blue-green changes. section 9.2.1
Tracing an indirect database dependency
A server appears not to need database credentials because the application connects to localhost. Tracing the runtime path reveals a database proxy using the server’s service account, which therefore requires database permissions. The dependency is indirect in application configuration but operationally essential. section 11.2.2
Listing 2.1 Parametrize the network as a variable
import json
def hello_server(name, network): #1
return {
'resource': [
{
'google_compute_instance': [ #2
{
name: [
{
'allow_stopping_for_update': True,
'zone': 'us-central1-a',
'boot_disk': [
{
'initialize_params': [
{
'image': 'ubuntu-1804-lts'
}
]
}
],
'machine_type': 'f1-micro',
'name': name,
'network_interface': [
{
'network': network #3
}
],
'labels': {
'name': name,
'purpose': 'manning-infrastructure-as-code'
}
}
]
}
]
}
]
}
if __name__ == "__main__":
config = hello_server(name='hello-world', network='default') #4
with open('main.tf.json', 'w') as outfile: #5
json.dump(config, outfile, sort_keys=True, indent=4) #5
Inspect a Python-generated Terraform configuration that accepts the network as an argument rather than embedding the dependency in the server resource. section 2.3.5


























