We have many modules that we use to create aws resources and all of them are used independently for deployments. For common patterns, I want the user to only have to call one module for their deployment. I don’t want to duplicate the code in the modules we have already created so I was thinking of doing something like creating a serverless api module that would call the individual modules to create the pattern.
Is there a better way of doing this? Would this be a bad idea?
I think maybe I didn’t explain the use case very well. I can not use publicly available modules that are in the terraform registry, I have to use internally created modules for this. Currently we have something like this that the user has to do:
I don’t want to copy my code from the other modules into one because that would be hard to maintain since if I make a change in one of the modules then I have to do the same in the serverless module.
Could I have the main.tf of the serverless-api module reference all the other modules that are needed or is that against best practices?
It’s certainly possible, but it comes with some drawbacks:
You have to write a lot of repetitive code to pass through all the input variables through the serverless-api module to the lambda, apigateway, alb modules.
The user cannot individually specify the versions of the lambda, apigateway, alb modules, so you’ll frequently need to re-release the serverless-api module just to increment the version numbers of the contained modules.
If these seem acceptable to you, then yes, you should go ahead with this approach.
The first issue I am not really concerned with, it is the versioning on the serverless API module that concerns me because to your point, it could be versioned often.
Instead of using modules stored in Git publish them to a registry.
With git modules you can only reference versions using git tags & branches (as that is all git provides), so generally you’d point at a fixed version tag.
If you publish to a Terraform registry you are able to use a more fully featured version constraints system, such as equal to, greater than, less than, and clauses or pinning to major/minor versions.
So for example you could publish your module that pins each sub-module usage to a given major version number, and then you’d only need to publish a new master module if there are breaking changes (which you’d likely have to do anyway, as other code would likely have to change at that point too)