DAY 21 – All about terraform Modules – Create & Publish your own modules – Day Twenty One
![Image tweet](https://res.cloudinary.com/practicaldev/image/fetch/s--2DQD8LHm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjzxg64r7q0i8ra6b8t.png)
![Image description](https://res.cloudinary.com/practicaldev/image/fetch/s--l9lhHbFe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqlr8epaj8jayihsfkeh.png)
100 days of Cloud on GitHub – Read On iCTPro.co.nz – Read on Dev.to
Using modules
Public Modules
- Terraform registry
- Syntax NameSpace/Name/Provider
- initiate via
terraform init
Private Modules
- terraform cloud
- Syntax Hostname/NameSpace/Name/Provider
- initiate via
terraform login
Publishing Modules
![Image apply](https://res.cloudinary.com/practicaldev/image/fetch/s--cTGqQCU5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z04ec6g43w7jw3341qbb.png)
Public Modules
Modules are published on to github. It have to be named with a specific name pattern as well.
ex:- lets say you are creating a modules for AWS VPC then module have to be named terraform-aws-vpc. You publish you module to terraform registry via GitHub account.
Some Features
- Supports versioning
- Generate document
- version history
- Show example
- available readme.md
Verified Modules
Verified modules are reviewed by HashiCorp and actively maintained by contributors to stay up-to-date and compatible with both Terraform and their respective providers.
The verified badge appears next to modules that are published by a verified source.
![Image badge](https://res.cloudinary.com/practicaldev/image/fetch/s--gRZuCb-p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wclckr7a71cg6xc679o9.png)
Standard Module Structure
Lets get an idea of the structure
- main.tf – This must exists on the root directory. In other words this file is the entry point for your module.
- variables.tf , Outputs.tf– All variables and outputs should have one or two sentence descriptions that explain their purpose. (Variables that can be passed, outputs are output values)
- readme.md – description in a readme file.
- LICENSE – The license under which this module is available
- Nested Module – Optional, must be inside the modules/ directory
Structure example
$ tree complete-module/
.
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
├── ...
├── modules/
│ ├── nestedA/
│ │ ├── README.md
│ │ ├── variables.tf
│ │ ├── main.tf
│ │ ├── outputs.tf
│ ├── nestedB/
│ ├── .../
├── examples/
│ ├── exampleA/
│ │ ├── main.tf
│ ├── exampleB/
│ ├── .../
Building your own Modueles
![Image terraform modules](https://res.cloudinary.com/practicaldev/image/fetch/s--7zNXKl-i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqoowetzbl5i4pmo7u93.png)
Lets use VSCode (Visual Studio Code) for this project
Make sure you have Terraform Visual Studio Code Extension is installed.
File Structure
- Lets create a new directory and Name it as terraform-aws-ec2
- Building the file structure.
- Lets create file main.tf
- Now create a sub folder named terraform-aws-ec2module-tutorial and create these files also outputs.tf, variables.tf, readme.md, LICENSE
- I am Using GitHub Terraform AWS modules by Anton Babenko (The 🪄 Magic 🔮Man) for reference for this project.
![Image terraform folder structure](https://res.cloudinary.com/practicaldev/image/fetch/s--R-lG6z3O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufzkcdfswqe1aabr2m0q.png)
- Check out below links for contents in files
Here we are going to build a module to create EC2 preloaded with APACHE and publish it to the terraform registry with GitHub.
Lets Build a Module
Once you created and copy pasted all the codes as above, we will create an terraform module to create an EC2 instance.
An EC2 APACHE server
Goal of the module is to create a EC2 instance with Apache installed in that.
Create a Public GitHub repo
- Name the repo as terraform-aws-ec2module-tutorial.
- On your project folder cd in to terraform-aws-ec2module-tutorial.
- Initialise, add all, commit and push the code.
- Add a version tag.
git tag v1.0.0
git push --tags
Connecting to Terraform Registry
- Go to Terraform Registry
- click on to sign-in, then click to Sign in with GitHub
- Authorize hashicrop Sign in.
- Go and select publish module
- Select our module, agree to terms of use and click Publish Module .
- 🎉Congratulations🎉 you have successfully published your module to terraform registry
✅Connect with me on Twitter
🤝🏽Connect with me on Linkedin
🧑🏼🤝🧑🏻 Read more post on dev.to or iCTPro.co.nz
💻 Connect with me on GitHub