Here is a step-by-step approach to using Terraform Modules to Deploy multiple web servers running on aws_ec2_instances. A similar system could be used to deploy other cloud resources.
-
Create a new directory for your Terraform project and create a main.tf file.
-
Create a new directory named modules in your project directory, and inside it, create a folder named ec2_instance
-
Create the following files inside the modules/ec2_instance directory.
-
Edit modules/ec2_instance/variable.tf to define input variables for the module.
-
Edit modules/ec2_instance/main.ft to create the EC2 instances
-
Edit modules/ec2_instance/output.ft to define the module’s output variables
-
Edit the main.tf file in your project directory to use the EC2 module
-
Initialize Terraform
-
Apply the Terraform configuration
-
End the session