Understanding Basis of Terraform #2

#terraformchallengeday2

I want to explain the overview of infrastructure as code with Terraform

  1. A Provider is a plugin that helps Terraform to interact with the API of the remote system. Terraform uses a provider plugin to manage resources. Provider help terraform to understand the cloud provider that the infrastructure will land. It is a block of code on its own. Below is an example of how a provider block looks like for AWS, GCP

    provider "aws" {

    region = "us-east-1"

    }

    provider "google" {

    project = "acme-app"

    region = "us-central1"

    }

    The region in the provider block is the region in that you want your infrastructure to be provisioned.

  2. Data sources are used to get information about resources or entities that is external or are not managed by the current terraform configuration. It is majorly a means of getting resources or data available for terraform provisioning. Below is an example of data sources and resources block for AWS and Azure

     data "aws_vpc" "selected" {
       id = var.vpc_id
     }
    
     resource "aws_instance" "terraform-example" {
       vpc_id            = data.aws_vpc.selected.id
       ami           = "ami-830c94e3"
       instance_type = "t2.micro"
    
       tags = {
         Name = "ExampleAppServerInstance"
       }
     }
    
     resource "azurerm_resource_group" "azure-sample" {
       name     = "thh-terraform-state-rg"
       location = "westeurope"
    
       tags = {
         generator = "Terraform"
       }
     }
    

    When using data sources, it is important to use them the way you understand them.

  3. Terraform state is the log or the position terraform is taking at a particular moment, it shows the state of our infrastructure. It contains the details of the resources in terraform code that is managed and deployed.

  4. The Output tells us about the infrastructure that we have available in our cloud provider. Output is used to get direct information about the infrastructure. An example is when we tell Terraform to give us the IP address of the infrastructure provisioned or vpc id of an infrastructure.

  5. Environmental variables and variables are configuration arguments. They make terraform code neat and easy to understand. This variable is used in a file called variable.tf. The argument is used in every other file in terraform code.

  6. The Module contains multiple data and resources that is used to provision infrastructure. The module is when you bring together all .tf files in our folder to main.tf within the configuration. It helps to group many resources into one unified resource.