Terraform Module | S3 bucket with Terraform Module
In this post, I would love to discuss why you should start using Terraform module. A lot of beginners skip over Terraform modules for the sake of simplicity, or so they think, Even me :) . I realized my mistake going through hundreds of lines of configuration code. We first go through some features of Terraform features followed by an example module.
Here are some of the ways that modules help solve the problems listed above:
- Organize configuration
Modules make it easier to navigate, understand, and update your configuration by keeping related parts of your configuration together. Even moderately complex infrastructure can require hundreds or thousands of lines of configuration to implement. By using modules, you can organize your configuration into logical components.
2. Encapsulate configuration
Another benefit of using modules is to encapsulate configuration into distinct logical components. Encapsulation can help prevent unintended consequences, such as a change to one part of your configuration accidentally causing changes to other infrastructure, and reduce the chances of simple errors like using the same name for two different resources.
3. Re-use configuration
Writing all of your configurations from scratch can be time-consuming and error-prone. Using modules can save time and reduce costly errors by re-using configuration written either by yourself, other members of your team, or other Terraform practitioners who have published modules for you to use. You can also share modules that you have written with your team or the general public, giving them the benefit of your hard work.
4. Provide consistency and ensure best practices
Modules also help to provide consistency in your configurations. Not only does consistency make complex configurations easier to understand, but it also helps to ensure that best practices are applied across all of your configurations. For instance, cloud providers give many options for configuring object storage services, such as Amazon S3 or Google Cloud Storage buckets. There have been many high-profile security incidents involving incorrectly secured object storage, and given the number of complex configuration options involved, it’s easy to accidentally misconfigure these services.
Using modules can help reduce these errors. For example, you might create a module to describe how all of your organization’s public website buckets will be configured, and another module for private buckets used for logging applications. Also, if a configuration for a type of resource needs to be updated, using modules allows you to make that update in a single place and have it be applied to all cases where you use that module.
Why Terraform Modules
In my earlier stages, I used to script the entire thing in one file main.tf, which I later regretted as it is worse at troubleshooting and reusing. So I thought to separate based on resources it creates and made files like vpc.tf, s3bucket.tf, codebuild.tf ..etc in a single folder. This method was ok but resulted in too many files and hard to track things. With Terraform modules files are organized also very much reusable.
Create S3 bucket with Terraform Module
Now let's try creating a private S3 bucket in 2 environments with Terraform modules
The s3-private folder is the module with Terraform resource to create an S3 bucket, it contains the following files.
The main.tf in the s3-private folder is creating the s3 bucket with variables passed from variables.tf . The values to the variables.tf file is passed when we call this module. outputs.tf is an optional file that outputs the S3 bucket arn. This output can be later referred in the parent module
now lets see how env files is separated
Here we separate prod and dev environments as respective folders. This helps in modifications and troubleshooting. Now with this setup in case, we plan to create a new bucket in the same or another env just add a new block in with source, name, and environment. With this, we can organize our code and it will be better for reusability and future reference. Now lets see main.tf and provider.tf
It's good to use profiles rather than access keys directly in provider.tf and in main.tf we declare our module with file location. Keep in mind to initialize Terraform while adding modules. To initialize Terraform modules run
terraform init
Once terraform initialize completes you can run plan and apply it accordingly
terraform planterraform apply
Conclusion
I hope, I was able to show you the importance of Terraform modules. It was hard for me to start using Terraform modules, but once I started, now I always reuse my code, this helps you save time and effort. Each time there is some change we can edit that part and keep it for future reference. In projects with multiple parallel env, we have to create so many resources with the same config, Terraform has been a blessing during those time. With Terraform module, (may be) the first module is hard as we need to find those files but, once the first module is clear it will be much easier to deploy other env.
So If this post helped you know terraform module do clap and follow for more 🙂