All about terraform Modules - Create & Publish your own modules
Tags: devops, terraform, productivity, cloud
Published: 2022-03-15 01:54:47

## DAY 21 - All about terraform Modules - Create & Publish your own modules - Day Twenty One

[![Image tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjzxg64r7q0i8ra6b8t.png)![Image Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ni1ut3598m6a6gif80mq.png)](https://twitter.com/intent/tweet?text=All%20about%20terraform%20Modules%20-%20Create%20%26%20Publish%20your%20own%20modules%20%2C%20a%20blog%20by%20%40anuvindhs%0A%23devops%20%23IAC%20%23aws%20%23cloud%20%23azure%20%23googlecloud%0A%0Ahttps%3A%2F%2Fdev.to%2Faws-builders%2Fall-about-terraform-modules-create-publish-your-own-modules-502b%20)


[100 days of Cloud on GitHub](https://github.com/anuvindhs/100daysofcloud) - [Read On iCTPro.co.nz](https://ictpro.co.nz) - [Read on Dev.to](https://dev.to/anuvindhs)



------

## 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://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://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://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**.(this file is exactly same as the code in the README.md, you can use this initially to test the IAC before we publish to registry )
- 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](https://github.com/terraform-aws-modules) by [Anton Babenko](https://github.com/antonbabenko) (The 🪄 Magic 🔮Man) for reference for this project.

![Image terraform folder structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufzkcdfswqe1aabr2m0q.png)
 
 
  
- Check out below links for contents in files 
 - [LICENSE](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/LICENSE)
 - [main.tf](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/main.tf)
 - [outputs.tf](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/outputs.tf)
 - [README.md](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/README.md)
 - [userdata.yaml](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/userdata.yaml)
 - [variables.tf](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/variables.tf)
 - [versions.tf](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/versions.tf)
 - [.gitignore](https://raw.githubusercontent.com/anuvindhs/terraform-aws-ec2module-tutorial/main/.gitignore)

**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
git tag v1.0.0
git push --tags
```
 

### Connecting to Terraform Registry
-  Go to [Terraform Registry](https://registry.terraform.io/)
- click on to **sign-in**, then click to **Sign in with GitHub** ![Image GitHub sign in terraform registry](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plynsa0ovvn3xae05vdp.png)
- **Authorize hashicrop** Sign in.![Image Authorize hashicrop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk45qd1zm9m7pjkxh4tx.png)
- Go and select **publish module** ![Image Publish module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gfe3r8fbd9x5gu55ot1.png)
- Select our module, agree to terms of use and click **Publish Module** . ![Image publish module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hnre4hdsoarfbqg4v7ei.png)
- 🎉**Congratulations**🎉 you have successfully published your module to terraform registry ![Image publish](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/olmisslrxvobdqh9484e.png)
 


  ----------

**✅Connect with me on [Twitter](https://twitter.com/intent/follow?ref_src=anuvindhs&screen_name=anuvindhs)
 🤝🏽Connect with me on [Linkedin](https://www.linkedin.com/in/anuvindhs)
🧑🏼‍🤝‍🧑🏻 Read more post on [dev.to](https://dev.to/anuvindhs) or  [iCTPro.co.nz](https://ictpro.co.nz)
💻 Connect with me on [GitHub](https://github.com/anuvindhs)**


{% user anuvindhs %}