Terraform Modules Tutorial Guide
TERRAFORM TUTORIAL - MODULES Ph.D. / Golden Gate Ave, San Francisco / Seoul National
Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep
Learning / Visualization
Thank you.
([Link]
- K Hong ([Link]
TUTORIALS
DOJO
Terminology Open
1. module:
A Terraform module is a set of Terraform configuration files in a single directory. Even a simple
configuration consisting of a single directory with one or more .tf files is a module. So in this
sense, every Terraform configuration is part of a module.
Free AWS Digital Courses
Pass your AWS certification exams Open
Tutorials Dojo
[Link] 1/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
2. root module:
When we run Terraform commands directly from a directory, it is considered the root module.
Terraform
3. child module:
A module that is called by another configuration is referred to as a child module. Introduction to Terraform with
AWS elb & nginx
(/DevOps/Terraform/Terraform-
4. calling module: Introduction-AWS-elb-
Terraform commands will only directly use the configuration files in one directory, which is [Link])
usually the current working directory. However, our configuration can use module blocks to call
modules in other directories. When Terraform encounters a module block, it loads and processes Terraform Tutorial - terraform
that module's configuration files. format(tf) and
interpolation(variables)
5. source argument: (/DevOps/Terraform/Terraform-
terraform-format-tf-and-
When calling a module, the source argument is required. Terraform may search for a module in
[Link])
the Terraform registry that matches the given string. We could also use a URL or local file path for
the source of our modules. Terraform Tutorial - user_data
(/DevOps/Terraform/Terraform-
6. terraform init : [Link])
When using a new module for the first time, we must run "terraform init" to install the module.
When the command is run, Terraform will install any new modules in the .terraform/modules Terraform Tutorial - variables
directory within our configuration's working directory. For local modules, Terraform will create a (/DevOps/Terraform/Terraform-
[Link])
symlink to the module's directory.
Terraform 12 Tutorial - Loops
$ terraform init
with count, for_each, and for
Initializing modules...
(/DevOps/Terraform/Terraform-
Initializing the backend... [Link])
[Link] 2/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link])
...
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
Terraform Tutorial - Creating
... AWS S3 bucket / SQS queue
} resources and notifying bucket
event to queue
module "ec2_instances" {
(/DevOps/Terraform/Terraform-
source = "terraform-aws-modules/ec2-instance/aws"
...
[Link])
}
Terraform Tutorial - AWS ASG
and Modules
(/DevOps/Terraform/Terraform-
7. [Link]/[Link]: Introduction-AWS-ASG-
These files contain our Terraform state, and are how Terraform keeps track of the relationship [Link])
between our configuration and the infrastructure provisioned by it
Terraform Tutorial - VPC,
8. .terraform: Subnets, RouteTable, ELB,
This directory contains the modules and plugins used to provision our infrastructure. Security Group, and Apache
server I
(/DevOps/Terraform/Terraform-
9. provider block:
VPC-Subnet-ELB-RouteTable-
We don't need provider block in module configuration. When Terraform processes a module SecurityGroup-Apache-Server-
block, it will inherit the provider from the enclosing configuration. Because of this, including [Link])
provider blocks in modules is not recommended.
Terraform Tutorial - VPC,
10. module outputs: Which values to add as outputs? Subnets, RouteTable, ELB,
Because outputs are the only supported way for users to get information about resources Security Group, and Apache
configured by the module. We need to add outputs to our module in the [Link] file inside the server II
(/DevOps/Terraform/Terraform-
module directory.
VPC-Subnet-ELB-RouteTable-
A module's outputs can be accessed as read-only attributes on the module object, which is SecurityGroup-Apache-Server-
available within the configuration that calls the module. We can reference these outputs in [Link])
expressions as module.<MODULE NAME>.<OUTPUT NAME>.
Modules
In this post, we're going to go over how to use Modules to organize Terraform-managed
infrastructure. In addition to that we'll learn how a child module exposes resource to a parent module
via terraform output .
$ terraform -v
Terraform v0.12.28
+ [Link] v3.34.0
[Link] 3/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Up to this point, we've been configuring Terraform by editing Terraform configurations directly. As our
infrastructure grows, this practice has a few key problems: a lack of organization, a lack of reusability,
and difficulties in management for teams.
Modules are used to create reusable components, improve organization, and to treat pieces of
infrastructure as a black box.
Ref - [Link]
([Link]
├── [Link]
├── my_modules
│ └── instance
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
├── [Link]
└── [Link]
Basically, the [Link] file will be using another terraform file in my-modules/instance/main.f. For
each of the [Link] has its own variables defined in [Link]. Here are the files:
Terraform Tutorial - Docker
./[Link]: nginx container with ALB and
dynamic autoscaling
(/DevOps/Terraform/Terraform-
provider "aws" { docker-nginx-alb-dynamic-
region = var.aws_region [Link])
}
module "my_instance_module" {
Terraform Tutorial - AWS ECS
source = "./my_modules/instance" using Fargate : Part I
ami = "ami-04169656fea786776" (/DevOps/Terraform/Terraform-
instance_type = "[Link]" [Link])
instance_name = "myvm01"
}
Hashicorp Vault
(/DevOps/Terraform/Hashicorp-
[Link])
./[Link]:
HashiCorp Vault Agent
(/DevOps/Terraform/Hashicorp-
variable "aws_region" { [Link])
description = "AWS region"
type = string
default = "us-east-1" HashiCorp Vault and Consul on
} AWS with Terraform
(/DevOps/Terraform/Hashicorp-
Vault-and-Consul-on-AWS-with-
Free AWS Digital Courses [Link])
./[Link]:
Pass your AWS certification exams Open
Ansible with Terraform
Tutorials Dojo (/DevOps/Ansible/Ansible-
[Link] 4/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Terraform-null_resource-local-
output "instance_ip_addr" {
[Link])
value = module.my_instance_module.instance_ip_addr
description = "The public IP address of the main instance."
} AWS IAM user, group, role, and
policies - part 1
(/DevOps/Terraform/Terraform_I
Note that we're accessing output values by referring to [Link] [Link] NAME.
AWS IAM user, group, role, and
policies - part 2
(/DevOps/Terraform/Terraform_I
The files in the module are the following.
Delegate Access Across AWS
./my_modules/instance/[Link]: Accounts Using IAM Roles
(/DevOps/Terraform/Terraform_A
Terraform Cloud
variable "ami" { (/DevOps/Terraform/Terraform-
type = string [Link])
default = "ami-04169656fea786776"
}
Terraform 14
variable "instance_type" {
(/DevOps/Terraform/Terraform14
type = string
default = "t2-nano" Creating Private TLS Certs
} (/DevOps/Terraform/Terraform-
[Link])
variable "instance_name" {
description = "Value of the Name tag for the EC2 instance"
type = string
default = "ExampleInstance"
}
variable "key_name" {
type = string
default = "einsteinish"
Sponsor Open Source development activities and free
}
contents for everyone.
- K Hong ([Link]
output "instance_ip_addr" {
Free AWS Digital Courses
value = aws_instance.my_instance.*.public_ip
description = "The public IP address of the main instance." Open
} Pass your AWS certification exams
Tutorials Dojo
[Link] 5/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Now that we are ready, just run terraform commands within the project root directory where the top-
level [Link] is located:
DevOps
Phases of Continuous
Integration
(/DevOps/Continuous_Integration
Software development
methodology
(/DesignPatterns/software_develo
Introduction to DevOps
(/DevOps/DevOps_Jenkins_Chef_
Samples of Continuous
Integration (CI) / Continuous
Delivery (CD) - Use cases
(/DevOps/DevOps_CI_CD_Pipeline
[Link] 6/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 7/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 8/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 9/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
(/DevOps/DevOps-Sys-Admin-
$ ssh -i ~/.ssh/[Link] ubuntu@[Link]
Interview-Questions-Web-
The authenticity of host '[Link] ([Link])' can't be established.
ECDSA key fingerprint is SHA256:DZ7zjNX1Ab6cZt/PpCzCYH4dLiNKNGB/To64jNkDyLM.
[Link])
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[Link]' (ECDSA) to the list of known hosts. (8) - Database
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-1065-aws x86_64) (/DevOps/DevOps-Sys-Admin-
Interview-Questions-
* Documentation: [Link]
[Link])
* Management: [Link]
* Support: [Link]
(9) - Linux System / Application
Get cloud support with Ubuntu Advantage Cloud Guest:
Monitoring, Performance
[Link] Tuning, Profiling Methods &
Tools (/DevOps/DevOps-Sys-
0 packages can be updated. Admin-Interview-Questions-
0 updates are security updates.
Linux-Monitoring-System-
Application-Performance-
[Link])
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the (10) - Trouble Shooting: Load,
individual files in /usr/share/doc/*/copyright. Throughput, Response time
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
and Leaks (/DevOps/DevOps-
applicable law. Sys-Admin-Interview-
Questions-Trouble-Shooting-
To run a command as administrator (user "root"), use "sudo ". Slow-Application-Performance-
See "man sudo_root" for details. [Link])
ubuntu@ip-172-31-41-218:~$
(11) - SSH key pairs, SSL
Certificate, and SSL Handshake
(/DevOps/DevOps-Sys-Admin-
Clean up: Interview-Questions-SSH-
Connection-SSL-
[Link])
$ terraform destroy
...
Plan: 0 to add, 0 to change, 1 to destroy.
(12) - Why is the database slow?
(/DevOps/DevOps-Sys-Admin-
Do you really want to destroy all resources? Interview-Questions-Why-is-
Terraform will destroy all your managed infrastructure, as shown above. [Link])
There is no undo. Only 'yes' will be accepted to confirm.
[Link] 10/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Let's look into how our module is defined in ./[Link]: (17) - Linux startup process
(/DevOps/DevOps-Sys-Admin-
Interview-Questions-Linux-
module "my_instance_module" {
[Link])
source = "./my_modules/instance"
ami = "ami-04169656fea786776"
instance_type = "[Link]" (18) - phpMyAdmin with Nginx
instance_name = "myvm01" virtual host as a subdomain
} (/DevOps/DevOps_phpMyAdmin_
How can we access the resource (public_ip) from the root [Link]? (22) - lsof (/DevOps/DevOps-
Sys-Admin-Interview-
Now any module that references our module can use this value in expressions as [Link])
module.module_name.output_name, inFree AWS
our case, Digital Courses
module.my_instance_module.instance_ip_addr,
where my_instance_module is the namePass
we've used in the corresponding (23) - Wireshark
Open introduction
your AWS certification exams module declaration.
(/DevOps/DevOps-WireShark-
Tutorials Dojo [Link])
[Link] 11/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 12/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
├── LICENSE
├── [Link]
Ansible 2.0
├── [Link]
├── modules
│ └── aws-s3-static-website-bucket
│ ├── LICENSE
│ ├── [Link]
│ ├── [Link]
│ ├── [Link]
What is Ansible?
│ ├── [Link] (/DevOps/Ansible/Ansible_What_
│ └── www
│ ├── [Link] Quick Preview - Setting up web
│ └── [Link] servers with Nginx, configure
├── [Link]
environments, and deploy an
└── [Link]
App
(/DevOps/Ansible/Ansible_Setting
Handlers
(/DevOps/Ansible/Ansible-
[Link])
[Link]:
Roles
(/DevOps/Ansible/Ansible-
[Link])
[Link] 13/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
tags = [Link]
}
Jenkins
modules/aws-s3-static-website-bucket/[Link]:
Install
(/DevOps/Jenkins/Jenkins_Install.
Free AWS Digital Courses Configuration - Manage Jenkins
Pass your AWS certification exams Open
- security setup
Tutorials Dojo (/DevOps/Jenkins/Jenkins_Configu
[Link] 14/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 15/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
(/DevOps/Jenkins/Jenkins_on_EC2
[Link] 16/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
(/DevOps/Puppet/puppet_amazo
# Terraform configuration
provider "aws" {
Puppet Code Basics -
region = "us-east-1" Terminology
} (/DevOps/Puppet/puppet_basics_
module "vpc" {
Puppet with Amazon AWS on
source = "terraform-aws-modules/vpc/aws"
CentOS 7 (I) - Master setup on
version = "2.21.0" EC2
(/DevOps/Puppet/puppet_amazo
name = var.vpc_name
cidr = var.vpc_cidr
Puppet with Amazon AWS on
azs = var.vpc_azs CentOS 7 (II) - Configuring a
private_subnets = var.vpc_private_subnets Puppet Master Server with
public_subnets = var.vpc_public_subnets
Passenger and Apache
enable_nat_gateway = var.vpc_enable_nat_gateway
(/DevOps/Puppet/puppet_amazo
[Link] 17/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Puppet Express
(/DevOps/Puppet/puppet_expres
Puppet Express 2
(/DevOps/Puppet/puppet_expres
Puppet 4 : Changes
(/DevOps/Puppet/puppet4_chang
Puppet --configprint
(/DevOps/Puppet/puppet_configp
[Link] 18/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 19/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Machine
resource "aws_s3_bucket" "s3_bucket" {
(/DevOps/Docker/Docker_Contain
bucket = var.bucket_name
[Link] 20/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Introduction to Terraform with AWS elb & nginx (/DevOps/Terraform/Terraform-Introduction-AWS- Docker Compose - NodeJS with
[Link]) MongoDB
Terraform Tutorial - terraform format(tf) and interpolation(variables) (/DevOps/Docker/Docker-
(/DevOps/Terraform/[Link]) [Link])
Terraform Tutorial - user_data (/DevOps/Terraform/[Link])
Terraform Tutorial - variables (/DevOps/Terraform/[Link]) Docker - Prometheus and
Terraform Tutorial - creating multiple instances (count, list type and element() function) Grafana with Docker-compose
(/DevOps/Terraform/[Link]) (/DevOps/Docker/Docker_Prome
Terraform 12 Tutorial - Loops with count, for_each, and for (/DevOps/Terraform/Terraform-
[Link]) Docker -
Terraform Tutorial - State ([Link]) & terraform import (/DevOps/Terraform/Terraform- StatsD/Graphite/Grafana
[Link]) (/DevOps/Docker/Docker_StatsD_
Terraform Tutorial - Output variables (/DevOps/Terraform/[Link])
Terraform Tutorial - Destroy (/DevOps/Terraform/[Link]) Docker - Deploying a Java EE
Terraform Tutorial - Modules (/DevOps/Terraform/[Link]) JBoss/WildFly Application on
Terraform Tutorial - Creating AWS S3 bucket / SQS queue resources and notifying bucket event to AWS Elastic Beanstalk Using
queue (/DevOps/Terraform/[Link]) Docker Containers
Terraform Tutorial - AWS ASG and Modules (/DevOps/Terraform/Terraform-Introduction-AWS-ASG- (/DevOps/Docker/Docker_Contain
[Link])
Terraform Tutorial - VPC, Subnets, RouteTable, ELB, Security Group, and Apache server I Docker : NodeJS with GCP
(/DevOps/Terraform/[Link]) Kubernetes Engine
Terraform Tutorial - VPC, Subnets, RouteTable, ELB, Security Group, and Apache server II (/DevOps/Docker/Docker-
(/DevOps/Terraform/[Link]) NodeJS-GCP-Kubernetes-
Terraform Tutorial - Docker nginx container with ALB and dynamic autoscaling [Link])
(/DevOps/Terraform/[Link])
Terraform Tutorial - AWS ECS using Fargate : Part I (/DevOps/Terraform/[Link]) Docker : Jenkins Multibranch
Free AWS Digital Courses
Hashicorp Vault (/DevOps/Terraform/[Link]) Pipeline with Jenkinsfile and
Open
Pass your AWS certification exams
HashiCorp Vault Agent (/DevOps/Terraform/[Link]) Github
Tutorials Dojo (/DevOps/Docker/Docker-
[Link] 21/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 22/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 23/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Docker Packer
(/DevOps/Docker/Docker-
[Link])
Docker Q & A
(/DevOps/Docker/Docker_Q_and_
[Link] 24/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link])
[Link] 25/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 26/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
ConfigMap
(/DevOps/Docker/Docker_Kubern
[Link] 27/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Kubernetes-Horizontal-Pod-
[Link])
[Link] 28/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Environments-GCP-Kubernetes-
[Link])
[Link] 29/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
StatefulSets on minikube
(/DevOps/Docker/Docker_Kubern
[Link] 30/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
(/DevOps/Docker/Docker_Helm3_
[Link] 31/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 32/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Elasticsearch
search
engine,
Logstash, and
Kibana
Elasticsearch, search engine
(/Hadoop/ELK/ELK_Elastic_Search
Elasticsearch indexing
performance
(/Hadoop/ELK/ELK_Elastic_Search
Vagrant
VirtualBox & Vagrant install on
Ubuntu 14.04
(/DevOps/Vagrant/Vagrant_Virtua
[Link] 33/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Provisioning
(/DevOps/Vagrant/Vagrant_Provis
Vagrant Share
(/DevOps/Vagrant/Vagrant_Share
Hadoop - Ecosystem
(/Hadoop/BigData_hadoop_Ecosy
[Link] 34/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
OLTP vs OLAP
(/Hadoop/BigData_hadoop_OLTP
[Link] 35/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
(/Hadoop/BigData_hadoop_CDH5
[Link] 36/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 37/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Redis In-
Memory
Database
Redis vs Memcached
(/DevOps/Redis/Redis_vs_Memca
[Link] 38/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Platform)
GCP: Creating an Instance
(/DevOps/GCP/gcp_Creating_an_I
AWS (Amazon
Web Services)
AWS : EKS (Elastic Container
Service for Kubernetes)
(/DevOps/AWS/aws-EKS-Elastic-
Container-Service-
[Link])
[Link] 39/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 40/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Destination-Buckets-Owned-by-
the-Same-AWS-Account-How-
to-Copy-or-Move-Objects-from-
[Link])
[Link] 41/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
(/DevOps/AWS/aws-Amazon-
[Link])
[Link] 42/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 43/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
(/DevOps/AWS/aws_detecting_sto
[Link] 44/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
AWS : CloudFormation -
templates, change sets, and CLI
(/DevOps/AWS/aws-
CloudFormation-
[Link])
[Link] 45/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link])
AWS : CloudFormation -
Creating an ASG with rolling
update (/DevOps/AWS/aws-
CloudFormation-Autoscaling-
Group-ASG-Application-Load-
Balancer-ALB-with-Update-
[Link])
AWS : OpsWorks
(/DevOps/AWS/aws-
[Link])
[Link] 46/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link])
Amazon DynamoDB
(/DevOps/AWS/aws-Amazon-
[Link])
[Link] 47/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
AWS : Q & A
(/DevOps/AWS/[Link])
AWS : Security
(/DevOps/AWS/aws-
[Link])
AWS : Scaling-Up
(/DevOps/AWS/aws-Scaling-
[Link])
Free AWS Digital Courses
AWS : Networking
Open
Pass your AWS certification exams
(/DevOps/AWS/aws-
Tutorials Dojo [Link])
[Link] 48/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Powershell 4
Tutorial
Powersehll : Introduction
([Link]
Powersehll : Running
commands
([Link]
Powersehll : Providers
([Link]
Powersehll : Pipeline
([Link]
Powersehll : Objects
([Link]
Windows Management
Instrumentation (WMI)
([Link]
[Link] 49/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Git/GitHub
Tutorial
One page express tutorial for
GIT and GitHub
(/cplusplus/Git/Git_GitHub_Expre
Installation
(/cplusplus/Git/Git_GitHub_Instal
add/status/log
(/cplusplus/Git/Git_GitHub_status
Reverting commit
(/cplusplus/Git/Git_GitHub_Rever
[Link] 50/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Fast-forward merge
(/cplusplus/Git/Git_GitHub_Fast-
Forward_Merge.php)
Uploading to GitHub
(/cplusplus/Git/GitHub_Uploading
GUI
(/cplusplus/Git/GitHub_GUI.php)
Merging conflicts
(/cplusplus/Git/Git_Branching_Me
Git/GitHub Terminologies
(/cplusplus/Git/Git_Terminologies
[Link] 51/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
Subversion
Subversion Install On Ubuntu
14.04
(/cplusplus/Subversion/Subversio
CONTACT
BogoToBogo
contactus@[Link] ([Link]
FOLLOW BOGOTOBOGO
([Link]
([Link]
ABOUT US (/ABOUT_US.PHP)
contactus@[Link] ([Link]
[Link] 52/53
2/15/22, 12:43 PM Terraform Tutorial - Modules - 2021
[Link] 53/53
The terraform.tfstate file is critical for Terraform's operation as it keeps track of the real-world infrastructure state that corresponds to the configurations defined. This file ensures that Terraform can update, manage, or destroy resources accurately across environments. The tfstate file acts as a single source of truth, mapping what exists in the cloud with the configuration code. The .terraform directory stores essential modules and plugins required for infrastructure provisioning, ensuring that Terraform has access to all necessary components to execute its tasks properly. Proper management of these elements is vital for maintaining consistency and accuracy in infrastructure operations .
Including provider blocks in Terraform module configurations is not recommended because when Terraform processes a module block, it automatically inherits the provider from the enclosing configuration. This eliminates the need for specifying the provider within each module and helps in reducing redundancy and potential configuration errors. Users should rely on this inheritance mechanism for cleaner and more maintainable configurations .
Terraform modules are an effective solution to scalability and maintainability challenges in infrastructure management. By encapsulating complex configurations into reusable modules, they enable teams to scale infrastructure efficiently without duplicating effort. Modules can be versioned, tested, and maintained separately, allowing organizations to manage changes systematically and ensure compatibility across environments. This modular structure enhances maintainability by providing clear interfaces and documentation, enabling easier updates and integration with existing systems. Modules support large-scale deployments by promoting consistent practices and minimizing errors, thus addressing the core issues of infrastructure scalability and maintainability .
In Terraform, the outputs.tf file defines output values, which are used to expose information from child modules to parent modules or other configurations. These output values serve as a communication mechanism by exporting specific data, like resource attributes, that can be consumed externally. For instance, a module might output an instance's IP address, allowing other modules to reference it using an expression such as module.<MODULE_NAME>.<OUTPUT_NAME>. This facilitates integration and interaction between different modules by making it easy to access necessary resource information, promoting modularity and reusability .
In Terraform-managed infrastructure, SSH keys facilitate secure remote connections by allowing secure shell access without needing passwords, employing public-private key cryptography. Known host entries help ensure a server's identity is verified before initiating a connection, protecting users from man-in-the-middle attacks. When a new server is accessed via SSH, its host key is added to the known hosts file, and future connections verify this key to authenticate the server. This security measure maintains the integrity and confidentiality of communications in automated infrastructure processes .
Terraform modules improve infrastructure management by enabling users to define reusable components, which enhances the organization of the configuration files. They help treat pieces of infrastructure as a 'black box', allowing teams to standardize configurations and improve collaboration. Modules enable the encapsulation of complex logic and allow users to share and reuse configurations across different projects. This leads to more streamlined processes, reduced error occurrences, and facilitates easier scaling and management of infrastructure .
Terraform's module system supports the concept of treating infrastructure as code by organizing infrastructure components into modular, reusable units that can be version-controlled and shared. This aligns with infrastructure as code principles, where infrastructure specifications are written in code, stored in repositories, and manipulated using version control systems. By using modules, teams can encapsulate complexity, standardize deployments, and reduce duplication of effort. This modular approach enhances the collaboration between development and operations teams, streamlines provisioning processes, and ensures consistent and repeatable deployments across different environments, thus significantly improving DevOps workflows .
Best practices for referencing module outputs in Terraform involve clearly defining outputs in the module's outputs.tf file and using these outputs in expressions as module.<MODULE_NAME>.<OUTPUT_NAME>. This approach promotes effective infrastructure management by providing a standardized way to access resource information across different modules and configurations, ensuring data consistency and reducing errors. By consistently using outputs, teams can create decoupled modules that focus on individual tasks but still integrate seamlessly with the larger infrastructure. This promotes reusability, simplifies debugging, and facilitates the maintenance and scaling of infrastructure setups .
The 'terraform destroy' command in Terraform is used to remove all the resources defined in the Terraform configurations. During the destroy process, Terraform identifies all managed infrastructure that matches the current tfstate configuration and plans the resources to be destroyed. Users are prompted to confirm the destructive action, and upon confirmation, Terraform proceeds to de-provision the resources in a sequential manner, ensuring dependencies are respected. This command is irreversible and will remove all managed infrastructure, leaving no resources provisioned in the cloud unless manually recreated .
The outputs.tf file is integral to a Terraform module as it defines the outputs that other modules or configurations can use. By specifying outputs, modules provide a clear interface that describes what information they make available. This affects module reusability by allowing different modules to communicate and interact effectively, passing critical data such as resource IDs, IP addresses, or other attributes needed by other parts of the infrastructure. This modular communication is crucial for building flexible and reusable Terraform configurations, allowing teams to refactor modules for different use cases without changing their underlying implementations .









