![Whats is Modules in Terraform Whats is Modules in Terraform](https://askjoechan.com/wp-content/uploads/2022/10/modules-1.1-1024x564.png)
Whats is Modules in Terraform
![](https://askjoechan.com/wp-content/uploads/2022/10/diagram-tf1-1024x568.png)
Modules in Terraform
when your terraform infrastructure grows, modules helps you.
- Easy to manage your resources and clean
- Easilier to read and understand, avoid a huge project all in tf file)
![](https://askjoechan.com/wp-content/uploads/2022/10/modules-1.1-1-1024x564.png)
![](https://askjoechan.com/wp-content/uploads/2022/10/modules-1.2.png)
![](https://askjoechan.com/wp-content/uploads/2022/10/modules-1.3.png)
![](https://askjoechan.com/wp-content/uploads/2022/10/modules1.4-1.png)
______________________________________________________________________________________________________________
https://github.com/yourlifeuk/terra_project/tree/main/Project_2
code as Link as above
If you copy the code (Git clone), then you type “terraform init”. This time, you will see a little different then before. Initializing Modules <–you will see additional 2 directory under this title.
Not only that. Under your terraform project folder, you should able to see, an json file has been added.
![](https://askjoechan.com/wp-content/uploads/2022/10/terraform-init.png)
![](https://askjoechan.com/wp-content/uploads/2022/10/terraform-init-1.2-1024x129.png)
______________________________________________________________________________________________________________
Now we use terrafomr apply
![](https://askjoechan.com/wp-content/uploads/2022/10/terraform-plan-1024x274.png)
______________________________________________________________________________________________________________
Terraform Lock
Locking state – which is prevening the tfsate file get conflict/crash. You can imagine you and your colleage working at the same terraform project. So when you are implement the terraform, terraform will put tfstate in a locking state.
I put Locking state in AWS DynamoDB. If you go to Table -> explore items. you will see that there will be LockID in the table.
![](https://askjoechan.com/wp-content/uploads/2022/10/Lockfile-1024x237.png)
![](https://askjoechan.com/wp-content/uploads/2022/10/output-instance-1024x602.png)
______________________________________________________________________________________________________________
Result
2 x EC2 instances are created. as we have put “user_data” in place.
#! /bin/bash
sudo yum update -y && sudo yum install docker -y
sudo systemctl start docker
sudo usermod -aG docker ec2-user
newgrp docker
docker run -p 8080:80 nginx
We can access to the nginx server with the public IP : 8080. Public IP has been provided by “output”
![](https://askjoechan.com/wp-content/uploads/2022/10/result-1024x548.png)
______________________________________________________________________________________________________________
Don’t forget to “terraform destroy”
![](https://askjoechan.com/wp-content/uploads/2022/10/terraform-destroy-1024x674.png)