Terraform Basics

Terraform is an open-source infrastructure as code software tool created by HashiCorp. It allows users to define and provision data center infrastructure using a declarative configuration language called HashiCorp Configuration Language (HCL), or optionally JSON. Terraform manages external resources, such as public cloud infrastructure, private cloud infrastructure, network appliances, software as a service, and platform as a service, and it works with a variety of cloud providers, including Amazon Web Services, Google Cloud Platform, Microsoft Azure, and more.

With Terraform, infrastructure can be managed as code, which means that infrastructure configurations can be versioned, shared, and reused across teams. This approach enables infrastructure to be treated as a software application, with all the benefits of version control, testing, and collaboration that come with it.
Terraform works by using a declarative configuration language (HCL) to define the desired state of infrastructure resources. Users create configuration files that describe the infrastructure resources they want to create, update, or delete. Terraform then uses these configuration files to create an execution plan that outlines the actions it will take to achieve the desired state. Once the execution plan is approved, Terraform applies the plan, making the necessary changes to the infrastructure.

Terraform has several benefits:

  1. Infrastructure as Code (IaC): Terraform allows infrastructure to be defined and managed using code, which means that infrastructure configurations can be versioned, shared, and reused across teams. This approach makes it easier to manage infrastructure and ensures that changes are tracked and documented.

  2. Multi-Cloud Support*:* Terraform supports multiple cloud providers, including Amazon Web Services, Google Cloud Platform, Microsoft Azure, and more. This allows users to manage infrastructure across multiple cloud providers using a single tool.

  3. State Management*:* Terraform maintains a state file that keeps track of the current state of the infrastructure. This allows Terraform to determine what changes need to be made to achieve the desired state and ensures that only the necessary changes are applied.

  4. Modularity and Reusability*:* Terraform allows users to define infrastructure resources as reusable modules, which can be shared and reused across projects. This makes it easier to manage and maintain infrastructure configurations.

  5. Scalability*:* Terraform is designed to scale with the size of the infrastructure it manages. It can handle large and complex infrastructure configurations, making it suitable for use in enterprise environments.

  6. Community and Ecosystem*:* Terraform has a large and active community of users, which means that there are many resources, tutorials, and plugins available to help users get started and solve problems.

  7. Automation*:* Terraform can be integrated with CI/CD pipelines and other automation tools, allowing infrastructure changes to be automatically applied in response to code changes or other events.

Start with terraform practice:

Installation: First, you'll need to install Terraform on your machine. You can download the appropriate package for your operating system from the Terraform website.https://developer.hashicorp.com/terraform/install, unzip the package and add the Terraform binary to your system's PATH.

step1. go to environment variables in command prompt -> path.

then go to new and add path where terraform is installed.

step2. in VS code download the extension of terraform.

Write first terraform configuration file:

Configuration: Terraform uses a configuration file to define the infrastructure you want to create. The configuration file is written in HashiCorp Configuration Language (HCL) or JSON. Here's an example of a simple Terraform configuration file:

This configuration file defines an AWS provider. The provider block specifies the AWS region. it install the required plugin for aws so that we can use that plugins in order to config the infrastructure of aws.

  1. Initialization: Before you can use Terraform to create infrastructure, you need to initialize the working directory. Run terraform init in the directory containing your configuration file. This command downloads the necessary provider plugins and initializes the Terraform state.

    after running init command .terraform and .terraform.lock.hcl folders created. .terraform is provider plugin and .terraform.lock.hcl has hashed and version of provider.

    Terraform uses a backend to store the state of your infrastructure. By default, Terraform uses a local backend, which stores the state file on your local machine. This is convenient for getting started, but it has limitations. For example, if you're working in a team, it's difficult to share the state file and keep it in sync across different machines.

    To address this, Terraform supports remote backends, which store the state file remotely. There are several types of remote backends available, including Amazon S3, Azure Blob Storage, Google Cloud Storage, and HashiCorp Consul. Remote backends allow you to store the state file in a central location that all team members can access, making it easier to collaborate on infrastructure changes.

  2. Plan: After initialization, you can run terraform plan to create an execution plan. Terraform compares the current state of the infrastructure with the desired state defined in the configuration file and generates a plan for how to achieve the desired state.

    step1. write a resource of local file.

    step 2.-> terraform init -upgrade The -upgrade flag is used to upgrade the installed plugins to the latest available versions. This can be useful if you want to ensure that you are using the latest versions of the plugins. However, it's important to note that upgrading plugins can sometimes introduce compatibility issues, so it's a good idea to test the upgrade in a non-production environment first.

    So, the command terraform init -upgrade will initialize the Terraform configuration and upgrade any installed plugins to the latest available versions. If you have an existing Terraform configuration, it will also update the backend configuration if necessary.

    here, the command alone initializes the configuration with a local backend. The local backend stores the state file on your local machine, which is convenient for getting started but has limitations when working in a team or across different environments.

    step 3. terraform plan

  3. Apply: Once you have reviewed the plan and are ready to proceed, you can apply the changes by running terraform apply. This command creates or updates the infrastructure according to the plan.

    I made some changes in file name and content of the file . terrafom plan:The output is divided into sections:

    1. Resource Actions: This section lists the actions that Terraform will take on each resource. Each action is indicated by a symbol:

      • + indicates that Terraform will create a new resource.

      • - indicates that Terraform will destroy a resource.

      • ~ indicates that Terraform will update a resource.

      • => indicates that Terraform will import an existing resource.

    2. Resource Details: This section provides details about each resource, including the resource type, name, and any changes that will be made. In your case, the resource is a local file, and Terraform will replace it with a new file that has a different content and filename.

    3. Changes: This section provides a summary of the changes that will be made. In your case, Terraform will destroy the existing local file and create a new one with the specified content and filename.

    4. Plan: This section provides a summary of the overall plan, including the number of resources that will be created, updated, and destroyed.

In your case, the output indicates that Terraform will destroy the existing local file and create a new one with the specified content and filename. The forces replacement message indicates that Terraform will replace the existing file even if it has the same content, because the filename has changed.

Now, terrform apply

after terraform apply. terraform.tfstate has been created. and deep_file2.txt file is also created.

The terraform.tfstate file is a JSON file that Terraform uses to store the state of your infrastructure. It contains information about the resources that Terraform manages, such as their IDs, attributes, and dependencies. The terraform.tfstate file is created when you run terraform apply for the first time, and it is updated each time you make changes to your infrastructure.

The terraform.tfstate file is important because it allows Terraform to keep track of the current state of your infrastructure. This allows Terraform to determine what changes need to be made to achieve the desired state and ensures that only the necessary changes are applied.

The terraform.tfstate file should be treated as sensitive information, as it contains information about your infrastructure. It is recommended to store the terraform.tfstate file in a secure location, such as a version control system or a secure storage service like Amazon S3 or Azure Blob Storage. Additionally, you can use remote backends to store the terraform.tfstate file remotely, which provides better collaboration and scalability.

  1. Outputs: Terraform can also output values from your configuration files, which can be useful for passing information between different parts of your infrastructure or for displaying information to the user.

  2. Variables: Terraform supports variables, which allow you to parameterize your configuration files. This makes it easier to reuse configurations across different environments or customize configurations for specific use cases.

    In Terraform, if you define a variable with a default value in a variables.tf file, Terraform will use that default value if you don't provide a value for the variable when running terraform plan or terraform apply. This is useful when you want to provide a default value for a variable but still allow users to override it if necessary.

    If you run terraform plan without providing a value for the costum_content variable, Terraform will use the default value of "demo"

    However, if you want to provide a different value for the costum_content variable, you can do so by passing the value via the command line:

    To use variables in Terraform in a non-interactive form, you can define variables in a variable.tf file or pass them directly via the command line. Here's how you can do it:

    terraform plan --var "costum_content= this is non interactive way to pass variables"

  3. Destroy: If you want to remove the infrastructure, you can run terraform destroy. This command removes all the resources defined in the configuration file.

  4. State Management: Terraform maintains a state file that keeps track of the current state of the infrastructure. This file is important for Terraform to know what resources exist and how they are configured. By default, Terraform stores the state file locally, but you can also use remote backends like Amazon S3 or HashiCorp Consul to store the state file.

  5. Modules: Terraform allows you to define reusable modules that encapsulate a set of resources. This makes it easier to manage and maintain infrastructure configurations. You can use modules to create reusable components that can be shared across different projects.