Introduction to Terraform #TerraWeek Day01

What is an Infrastructure?

Infrastructure is the foundation that supports the operation of organizations and systems. In the context of technology and IT, it encompasses the hardware, software, networks, and facilities necessary for delivering IT services.

Physical components like servers, data centers, and networking equipment, as well as virtual resources such as virtual machines and cloud platforms, make up the infrastructure. Its purpose is to ensure the availability, reliability, and scalability of IT resources.

Infrastructure can be managed on-premises or outsourced to cloud service providers. Cloud-based infrastructure services like IaaS and PaaS offer scalable resources without the need for extensive physical infrastructure.

In summary, infrastructure forms the backbone of technology systems, enabling organizations to deploy and manage their applications and services effectively.

Challenges of an IT Infrastructure

1. Slow Deployment: Traditional IT infrastructure often involves time-consuming manual processes, hindering the agility and responsiveness required in today's fast-paced business environment.

2. Expensive: Maintaining and expanding physical infrastructure can be costly, requiring significant upfront investments in hardware, software licenses, maintenance, and dedicated personnel.

3. Limited Automation: Lack of automation in infrastructure provisioning and management leads to inefficiencies, increased complexity, and potential errors, impeding scalability and agility.

4. Human Error: Manual configuration and maintenance of infrastructure increase the risk of human error, leading to system failures, security vulnerabilities, and downtime.

5. Wasted resources: Inefficient resource allocation and underutilization of hardware and software resources can result in wasted energy, excessive costs, and reduced environmental sustainability.

These challenges highlight the need for modernizing IT infrastructure through automation, cloud adoption, and embracing DevOps practices to overcome limitations and unlock the full potential of technology resources.

What is DevOps and why it is needed?

DevOps is a software development approach that combines software development (Dev) and IT operations (Ops) to enhance collaboration, communication, and integration between development teams and operations teams. It aims to streamline the software delivery process, improve deployment speed, and ensure the stability and reliability of systems.

DevOps is needed to address the challenges of traditional software development and operations, such as siloed teams, slow release cycles, and manual processes. It promotes a culture of collaboration, automation, and continuous improvement, enabling organizations to deliver high-quality software faster, respond quickly to customer feedback, and achieve greater business agility and competitiveness.

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define and provision infrastructure resources, such as virtual machines, networks, storage, and more, in a declarative way using configuration files. Terraform supports multiple cloud providers and infrastructure technologies, providing a consistent and unified approach to manage and orchestrate infrastructure across different environments. It automates the provisioning process, handles resource dependencies, and enables infrastructure to be managed as code, facilitating scalability, reproducibility, and collaboration in infrastructure management.

Installation

1. Visit the official Terraform website (https://www.terraform.io/) and go to the downloads page.

2. Choose the appropriate package for your operating system. Terraform supports various operating systems, including Windows, macOS, and Linux.

3. Download the package file for your operating system. It will typically be a compressed archive file (e.g., .zip for Windows, .tar.gz for Linux).

4. Extract the contents of the package to a directory of your choice.

5. Add the Terraform executable to your system's PATH environment variable. This step allows you to run Terraform commands from any directory in your command-line interface.

  • For Windows:

    • Open the Start menu and search for "Environment Variables."

    • Click on "Edit the system environment variables."

    • In the System Properties window, click the "Environment Variables" button.

    • In the "System Variables" section, select the "Path" variable and click "Edit."

    • Add the path to the directory where you extracted the Terraform executable (e.g., C:\terraform) and click "OK" to save the changes.

  • For Linux/macOS:

    • Open a terminal window.

    • Edit the .bashrc or .bash_profile file in your home directory using a text editor (e.g., nano ~/.bashrc or nano ~/.bash_profile).

    • Add the following line at the end of the file:

    ```

    export PATH="$PATH:/path/to/terraform/directory"

    ```

    Replace "/path/to/terraform/directory" with the actual path to the directory where you extracted the Terraform executable.

    • Save the changes and close the text editor.

    • Run the command source ~/.bashrc or source ~/.bash_profile to apply the changes to the current terminal session.

6. Verify the Terraform installation by opening a new terminal or command prompt window and running the command terraform version. If the installation was successful, it will display the version of Terraform installed on your system.

In our case, we are installing Terraform in Ubuntu.

/

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

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

sudo apt update && sudo apt install terraform

/

Check Terraform version

Terraform Hashicorp Configuration Language

Terraform HCL (HashiCorp Configuration Language) is a declarative language specifically designed for defining infrastructure as code in Terraform. HCL provides a concise and human-readable syntax for writing Terraform configuration files.

Key features of Terraform HCL include:

1. Declarative Syntax: HCL allows you to describe the desired state of your infrastructure without specifying the detailed steps to achieve that state. You define the resources, their properties, and relationships, and Terraform takes care of provisioning and managing the infrastructure.

2. Resource Blocks: HCL uses resource blocks to define infrastructure resources such as virtual machines, networks, storage, and more. Each resource block begins with a resource type and is followed by a unique resource name and a set of configuration properties.

3. Variables and Expressions: HCL supports variables that enable parameterization of configurations. You can define variables and use them to make your configurations more flexible and reusable. HCL also provides expressions for performing computations and referencing values from other parts of your configuration.

4. Interpolation and Functions: HCL supports interpolation, allowing you to reference variables or perform dynamic calculations within your configuration. It also provides a range of built-in functions that can be used for string manipulation, mathematical operations, and more.

5. Modules and Reusability: HCL allows you to define reusable modules, which are self-contained Terraform configurations that can be used as building blocks for creating larger infrastructure setups. Modules enable code reuse, promote consistency, and simplify the management of complex infrastructures.

6. Provider Configuration: With HCL, you can specify the configuration for different providers (e.g., AWS, Azure, Google Cloud) within your Terraform files. This allows you to seamlessly switch between providers or manage resources from multiple providers within the same configuration.

Terraform HCL provides a powerful yet approachable language for defining infrastructure as code. It enables collaboration, version control, and automation, making it easier to provision and manage infrastructure across different cloud platforms and services.

Eg:

Vim local.tf

Based on the Terraform HCL code provided:

- Resources:

  1. local_file resource:

  2. Name: devops

  3. Type: local_file

  4. Attributes:

    • filename (attribute) - Specifies the file path and name for the local file to be created.

    • content (attribute) - Specifies the content to be written in the local file.

  5. random_string resource:

  6. Name: rand-str

  7. Type: random_string

  8. Attributes:

    • length (attribute) - Specifies the length of the random string to be generated.

    • special (attribute) - Specifies whether the random string should include special characters.

    • override_special (attribute) - Specifies a set of special characters to be included in the random string.

- Outputs:

  • Output Name: rand-str

  • Value: random_string.rand-str[*].result

The code includes two resource blocks (`local_file` and random_string) and one output block (`output`). The resources define the desired state of the infrastructure, while the output block specifies the value to be displayed as output after running Terraform commands.

Blocks and Arguments

1. Blocks: Blocks in Terraform are used to define and encapsulate different sections of configuration. Each block represents a specific resource or construct within the Terraform configuration. For example, resource blocks define individual infrastructure resources, variable blocks define input variables, and provider blocks define the configuration for a specific provider. Blocks have a specific syntax and structure depending on their purpose.

2. Arguments: Arguments are the key-value pairs or properties within a block that configure the behavior of the associated resource or construct. Each block can have one or more arguments, which define the specific settings for that block. For instance, within a resource block, arguments can include properties like the resource name, image, size, or any other configuration settings required by that resource. Arguments are used to specify the desired state of the infrastructure resources you are managing with Terraform.

Execution of Infrastructure:

- terraform init: Initializes a Terraform working directory. It downloads and installs the necessary provider plugins and sets up the backend configuration. It should be run once before starting a new Terraform project (`terraform init`).

- terraform plan: Creates an execution plan by comparing the current state of the infrastructure with the desired state defined in the Terraform configuration files. It displays the changes that Terraform will make to achieve the desired state (`terraform plan`).

- terraform validate: Validates the configuration files in the current directory. It checks for syntax errors, correct resource references, and required input variables (`terraform validate`).

- terraform apply: Applies the changes described in the execution plan. It creates, modifies, or deletes resources to match the desired state defined in the configuration files. It prompts for confirmation before making any changes (`terraform apply`).

These commands are essential steps in the Terraform workflow, allowing you to initialize the project, validate the configuration, plan the changes, and apply those changes to provision or modify the infrastructure.

Terraform state

Terraform state is a vital component that tracks the current state of your infrastructure, enabling Terraform to plan and apply changes. Here are the highlights:

- Representation of Infrastructure: Terraform state is a representation of the declared resources and their current state as managed by Terraform.

- Resource Metadata: The state file contains resource metadata, attribute values, dependencies, and relationships.

- Source of Truth: It serves as the source of truth for Terraform, enabling it to determine the actions required to reach the desired state.

- Planning and Applying Changes: Terraform uses the state file to plan and apply changes to your infrastructure.

- Detecting Drift: By comparing the desired state with the current state, Terraform can detect any drift in the infrastructure.

- Backend Options: Terraform offers various backend options for storing and retrieving the state file, such as local file or remote storage services like AWS S3 or HashiCorp Terraform Cloud.

- Security Considerations: The state file may contain sensitive information, so it should be stored securely and treated as a sensitive artifact.

- Collaboration and Sharing: Effective management of the state file is crucial for collaboration, sharing, and maintaining infrastructure as code across teams.

/ ubuntu@ip-172-31-28-144:~/terraform-course/terraform-local$ cat terraform.tfstate

{

"version": 4,

"terraform_version": "1.5.2",

"serial": 3,

"lineage": "ba54ded6-8aac-ec08-0da1-a36cd64d00b3",

"outputs": {

"rand-str": {

"value": [

"=1\u0026siFxlXCY)j2X}"

],

"type": [

"tuple",

[

"string"

]

]

}

},

"resources": [

{

"mode": "managed",

"type": "local_file",

"name": "devops",

"provider": "provider[\"registry.terraform.io/hashicorp/local\"]",

"instances": [

{

"schema_version": 0,

"attributes": {

"content": "I want to become the DevOps Engineer who knows Terraform",

"content_base64": null,

"content_base64sha256": "B5Bu4hGzkA6GGIykLWYjpCUm+6plkBoR5fJkGj385j0=",

"content_base64sha512": "l1lmv8f0J1AhoyNR7l7DUQm4IgpkiJRwXhCQe3iyuVw4ssJ3Cp5efP/9J54ZvQaO6Lu04aZ0f/YYfTQVBGCYfA==",

"content_md5": "424a270c4ebfeb8d06d4975de55af931",

"content_sha1": "80a2b22dbb82a00e3508c99431d7f11734fb5e04",

"content_sha256": "07906ee211b3900e86188ca42d6623a42526fbaa65901a11e5f2641a3dfce63d",

"content_sha512": "975966bfc7f4275021a32351ee5ec35109b8220a648894705e10907b78b2b95c38b2c2770a9e5e7cfffd279e19bd068ee8bbb4e1a6747ff6187d34150460987c",

"directory_permission": "0777",

"file_permission": "0777",

"filename": "/home/ubuntu/terraform-course/terraform-local/devops_automated.txt",

"id": "80a2b22dbb82a00e3508c99431d7f11734fb5e04",

"sensitive_content": null,

"source": null

},

"sensitive_attributes": []

}

]

},

{

"mode": "managed",

"type": "random_string",

"name": "rand-str",

"provider": "provider[\"registry.terraform.io/hashicorp/random\"]",

"instances": [

{

"schema_version": 2,

"attributes": {

"id": "=1\u0026siFxlXCY)j2X}",

"keepers": null,

"length": 16,

"lower": true,

"min_lower": 0,

"min_numeric": 0,

"min_special": 0,

"min_upper": 0,

"number": true,

"numeric": true,

"override_special": "!#$%\u0026*()-_=+[]{}\u003c\u003e:?",

"result": "=1\u0026siFxlXCY)j2X}",

"special": true,

"upper": true

},

"sensitive_attributes": []

}

]

}

],

"check_results": null

} /

What is nginx?

Nginx is a high-performance, open-source web server and reverse proxy server software known for its scalability and efficiency. It is widely used to serve static and dynamic content, handle concurrent connections, and improve web application performance. Nginx supports multiple protocols, offers features like load balancing and SSL/TLS termination, and is highly regarded for its reliability in modern web infrastructure.

What is docker?

Docker is an open-source platform that enables developers to automate the deployment and management of applications within isolated containers. It provides a lightweight and portable environment for running software, allowing applications to be packaged with their dependencies into a single unit called a container. Docker offers benefits such as increased scalability, improved efficiency, and simplified software deployment across different environments. It has revolutionized the way applications are built, shipped, and run, making it easier to develop and deploy software consistently across various platforms.

Terraform with Docker

/Mkdir terraform-docker

Cd terraform-docker/

/Vim main.tf/

/terraform {

required_version = ">= 0.15.0"

required_providers {

docker = {

source = "kreuzwerker/docker"

version = ">= 3.0.2"

}

}

}

provider "docker" {}

resource "docker_image" "nginx" {

name = "nginx:latest"

keep_locally = false

}

resource "docker_container" "nginx" {

image = docker_image.nginx.name

name = "nginx-tf"

ports {

internal = 80

external = 80

}

}

/

Terraform init

Access the deployed nginx using the public IP