1- What are Terraform Modules:
A Terraform module is a self-contained package of Terraform configurations that are managed as a group. Modules allow you to share your infrastructure code with others, and to reuse your own code across multiple projects.
Modules are made up of two files: main.tf file that contains the configuration for the module, and variables.tf file that contains variables that can be used to customize the module.
To use a module, you first need to create a terraform.tf file in your project directory. This file tells Terraform where to find the module.
Once you have created the terraform.tf file, you can use the terraform init command to download the module and its dependencies.
To apply the module, you can use the terraform apply command. This will create or update the infrastructure that is defined in the module.
Modules are a powerful way to manage your infrastructure with Terraform. They allow you to share your code with others, reuse your own code across multiple projects, and make your infrastructure more consistent and reliable.
Here are some of the benefits of using Terraform modules:
Reusability: Modules can be reused across multiple projects, which can save you time and effort.
Scalability: Modules can be scaled up or down to meet the needs of your infrastructure.
Flexibility: Modules can be customized to meet your specific requirements.
Consistency: Modules can help to ensure that your infrastructure is consistent across multiple environments.
Reliability: Modules can help to make your infrastructure more reliable by reducing the risk of human error.
Let us Build Terraform Module For creating AWS Services:
1- Open VS Code and create an AWS-module-project folder and inside the same folder create another folder for modules.
Note: if you are using VS Code then remember to download Terraform extension, as it will help you to write code efficiently.
We will create a separate terraform file for each module. Because it is easier to manage and the code looks clean And any changes can be made easily.
First, we create providers.tf file and specify the AWS region where we will create our services
Next, we create a backend.tf file and specify the variables for the S3 bucket name, Dynamodb table name, and AWS region and add a block to create backend resources such as, DynamoDB table, also add resources for creating the S3 bucket for storing terraform.tfstate
file
In terraform.tf file we add terraform providers which is AWS configuration and also add backend for creating S3 bucket for dynamodb to store terraform.tf state file but, for now we will comment out the backend syntax because at first we have to create an dynamodb and then, we can push state file to dynamodb table with the state file
In main.tf file we add modules for the dev, prod and staging environment
Now in the same folder, we create another folder for modules configurations = my_app_infra_module
first we create bucket.tf for the S3 bucket
Next, we create server.tf for the AWS EC2 instance and also added a replica (count) for 2 instance creation
next, we create table.tf for dynamodb table resource
Next, is the Variable.tf where we add variables for the environment, instance type and ami
Now, open the Terminal in the working directory and run the command, terraform init
Next, if we do terraform plan it shows the backend initialization required which we commented out in the terraform.tf file.
now, we remove the comment out of the backend resources because the S3 bucket for dynamodb is been initialized
Now we run the command terraform plan to check the execution plan
If everything checks out then we run the command terraform apply
to execute the whole code.
As it shows 12 resources have been created, which would be, 6 - EC2 instances, 3 - S3 buckets for each environment, and 3 - Dynamodb table
All the resources have been created.
At last, to delete every resources that have been created by Terraform we just need to run a single command which is terraform destroy
which will delete every resource that has been created by terrafrom apply
Thank you for taking your valuable time to read my blog!!!!