• jcnetworking
  • LONDON
AWS
Whats is Modules in Terraform

Whats is Modules in Terraform

Modules

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)
Main – variable
Websever – variable
Subnet – variable

______________________________________________________________________________________________________________

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.

terraform init
Json file

______________________________________________________________________________________________________________

Now we use terrafomr apply

terraform apply

______________________________________________________________________________________________________________

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.

______________________________________________________________________________________________________________

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”

2 EC2 instance created.

______________________________________________________________________________________________________________

Don’t forget to “terraform destroy”

terraform destroy

Leave a Reply

Your email address will not be published. Required fields are marked *