Writing flexible modules
Note
The code for this section is under the chap04/flexible-module
directory in the GitHub repo of this book.
Well-written modules should provide a layer of abstraction and be easy to use. It is best to avoid simply writing a module to provide a thin wrapper to existing resource definitions. Modules should provide additional functionality and abstraction.
For example, unless you are an experienced Google Cloud architect, machine types such as N2, E2, and N2D don’t have much meaning. For the casual user, it would be much better to provide T-shirt sizes for servers, for example, small
, medium
, and large
, and let the module decide which machine type to use.
Using our previous example, we can specify default variable values in the variable declaration blocks of the child module. Any variable with a default value becomes optional to the calling module. We declare default values for all variables in the module except the name; thus, all variables...