What is Terraform?
Terraform is an open-source tool for managing and automating your infrastructure by defining it in code. It simplifies the provisioning and management of cloud resources, servers, and other infrastructure components across different providers using a declarative approach. With Terraform, you describe your desired infrastructure, and it takes care of creating, modifying, or deleting resources to match that description. It's a powerful tool for DevOps and infrastructure automation.
Task 1: Install Terraform on AWS EC2 Instance
Step 1: Create an Amazon EC2 instance.
Step 2: Now let's install Terraform on our EC2 instance and to ensure of system is up to date let's first install gnupg,software-properties-common, and curl.
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
Step 3: Now let's add the HashiCorp GPG key.
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
Step 4: Verify the key's fingerprint.
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
Step 5: Now let's add the HashiCorp repository.
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
Step 6: Now let's update the apt repository.
sudo apt update
- Step 7: Now let's install Terraform.
sudo apt-get install terraform
Step 8: Now let's verify the Terraform installation.
terraform --version
Step 9: To enable the tab completion and then install the autocomplete package.
touch ~/.bashrc
terraform -install-autocomplete
Task 2: Need of Terraform
Why do we use terraform?
Infrastructure Automation: Terraform automates the provisioning and management of infrastructure resources, reducing manual effort and increasing efficiency.
Declarative Language: It uses declarative language (HCL) to define infrastructure as code, making it easy to read, write, and understand.
Multi-Cloud Support: Terraform supports multiple cloud providers and on-premises infrastructure, enabling multi-cloud and hybrid cloud deployments.
Resource Abstraction: Terraform abstracts infrastructure resources into reusable modules, facilitating code reuse and maintainability.
State Management: It maintains a state file to track the current infrastructure state, enabling Terraform to understand and manage desired state changes.
Version Control: Infrastructure code can be version-controlled using tools like Git, allowing for collaboration, history tracking, and code reviews.
Immutable Infrastructure: Terraform promotes the concept of immutable infrastructure, where changes are made by replacing resources, enhancing predictability and rollback capabilities.
Dependency Management: Terraform handles resource dependencies, ensuring resources are provisioned in the correct order.
Plan and Apply: It provides a "plan" phase to preview changes before applying them, reducing the risk of unexpected disruptions during deployments.
Community and Ecosystem: Terraform boasts a robust community and ecosystem with a wealth of pre-built modules and plugins, simplifying the adoption of best practices and solutions.
What is Infrastructure as Code(IaC)?
Infrastructure as Code (IaC) is the managing and provisioning of infrastructure through code instead of through manual processe configuration files are created that contain your infrastructure specifications, which makes it easier to edit and distribute configurations.
It also ensures that you provision the same environment every time. By codifying and documenting your configuration specifications, IaC aids configuration management and helps you to avoid undocumented, ad-hoc configuration changes.
Version control is an important part of IaC, and your configuration files should be under source control just like any other software source code file. Deploying your infrastructure as code also means that you can divide your infrastructure into modular components that can then be combined in different ways through automation.
What is Provider?
A "provider" refers to a component or plugin that allows you to interact with and manage resources in a specific cloud or infrastructure platform. Providers are essential in Terraform as they act as connectors between your IaC code and the target infrastructure where you want to create, configure, or manage resources.
# Define the AWS provider with your credentials
provider "aws" {
region = "us-east-1"
}
What is Resource?
A "resource" refers to a specific infrastructure component or entity that you want to create, configure, or manage. Resources can represent a wide range of infrastructure elements, such as virtual machines, databases, networks, storage, load balancers, and more, depending on the cloud provider or technology being used.
# Define an AWS EC2 instance resource
resource "aws_instance" "example_instance" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI ID
instance_type = "t2.micro"
key_name = "my-keypair"
}
What is a State file in Terraform?
The Terraform state file is a JSON or binary file that contains information about the resources you've defined in your Terraform configuration. It includes details such as resource IDs, attributes, dependencies, and other metadata necessary for Terraform to understand the current state of your infrastructure.
This state file is typically named terraform.tfstate, but it can be customized to have different names or be stored remotely in services like AWS S3 or HashiCorp Consul. It is essential to ensure that this file is managed and secured properly because it contains sensitive information about your infrastructure.
What’s the importance of it?
Resource Tracking: The state file keeps a record of the current state of your infrastructure, including the status and attributes of resources. This tracking is essential for Terraform to understand the existing infrastructure and make necessary changes to achieve the desired configuration.
Dependency Resolution: Terraform uses the state file to determine the dependency relationships between resources. This ensures that resources are created, updated, or destroyed in the correct order, preventing issues related to resource dependencies.
Change Detection: During a Terraform apply operation, the state file is compared to the desired configuration defined in your code. It helps Terraform identify what changes are required to align the infrastructure with the specified configuration.
Resource Deletion: The state file helps Terraform identify resources that are no longer part of your configuration. When you remove a resource from your Terraform code, Terraform can safely delete the corresponding resource in the infrastructure.
Concurrency Control: In team environments, the state file facilitates concurrency control. It prevents multiple team members from making conflicting changes to the same resources simultaneously, reducing the risk of data corruption or conflicts.
What is the Desired and Current State?
Desired State:
Definition: The desired state is a representation of how you want your infrastructure to be configured based on your IaC code. It's a set of declarative instructions that specify what resources should exist, how they should be configured, and how they should be interconnected.
Purpose: The desired state is the ideal configuration you aim to achieve when you apply your IaC code using tools like Terraform. It serves as the source of truth for your infrastructure's intended state.
For example, in a Terraform configuration, the desired state might specify that you want to create an AWS EC2 instance with specific attributes, such as a particular instance type, operating system, and security group.
Current State:
Definition: The current state represents the actual state of your infrastructure as it exists at a given point in time. It includes information about the resources that currently exist, their attributes, and their current configurations.
Purpose: The current state is determined by inspecting the infrastructure itself or by referencing a state file that tracks the state of resources. It is used to understand the current state of your infrastructure and to determine what actions need to be taken to bring it in line with the desired state.
For example, if you have a running EC2 instance in your AWS account with specific attributes, the current state would reflect the attributes and configuration of that running instance.