Terraform Day 2:-

Terraform Consulting Services and Solution Provider: TX, USA

Greetings to all. This is my Second blog for the TerraWeek Challenge on Terraform. I hope to share with you some of my Terraform knowledge or expertise through this blog. #Terraform Shubham Londhe

Table of contents.

  • Learn about HCL blocks, parameters, and arguments

  • Explore the different types of resources and data sources available in Terraform

  • Create a variable.tf file and define a variable

  • Use the variable in a main.tf file to create a "local_file" resource

  • Add required_providers to your configuration, such as Docker or AWS

  • Test your configuration using the Terraform CLI and make any necessary adjustments

  1. Learn about HCL blocks, parameters, and arguments

To define and configure resources and modules, you work with a variety of elements, such as blocks, parameters, and arguments, in the HashiCorp Configuration Language (HCL), which is used to write Terraform configurations. Here is a summary of each of these ideas:

Blocks:

  • The primary building blocks of HCL are blocks. They specify the context for different configuration constructs like resources, modules, and provisioners. Curly brackets are typically used to label and enclose blocks.

  • Block Type: Specifies what kind of resource or setting you are defining. Examples include resource, variable, provider, etc.

  • Block Label: A label that uniquely identifies the block within its scope. It helps distinguish between multiple instances of the same block type.

  • Block Body: Contains one or more parameters and their associated values.

Parameters:

  • The general format of a parameter is: A parameter is a key-value pair that is declared within the block body and used to set various attributes, properties, or settings for the resource or setting being defined.

  • Parameter Name: Specifies the name of the parameter, which is often predefined and specific to the resource type.

  • Parameter Value: Specifies the value assigned to the parameter, which can be a literal value, a variable reference, or an expression.

Arguments:

  • Parameters are given arguments as values. To specify the real values for parameters within a block in HCL, utilize arguments. Depending on the nature and specifications of the parameter, an argument can be a literal, a variable, or an expression.

  1. Explore the different types of resources and data sources available in Terraform

Resources:

  • An infrastructure component that you want to create, maintain, or interact with is represented by a resource in Terraform. It specifies the ideal condition for a certain resource within a cloud service or infrastructure service. Resources can be utilized to build virtual machines, databases, networks, security groups, storage, and other things.

  • Compute Resources:

    • aws_instance: Represents an Amazon EC2 instance.

    • google_compute_instance: Represents a Google Compute Engine virtual machine instance.

    • azurerm_virtual_machine: Represents an Azure Virtual Machine.

  • Networking Resources:

    • aws_vpc: Represents an Amazon Virtual Private Cloud (VPC).

    • google_compute_network: Represents a Google Cloud Virtual Network.

    • azurerm_virtual_network: Represents an Azure Virtual Network.

  • Storage Resources:

    • aws_s3_bucket: Represents an Amazon S3 bucket.

    • google_storage_bucket: Represents a Google Cloud Storage bucket.

    • azurerm_storage_container: Represents an Azure Storage Blob Container.

  • Database Resources:

    • aws_db_instance: Represents an Amazon RDS database instance.

    • google_sql_database_instance: Represents a Google Cloud SQL database instance.

    • azurerm_sql_server: Represents an Azure SQL Database server.

  • Container Resources:

    • docker_container: Represents a Docker container.

    • kubernetes_deployment: Represents a Kubernetes Deployment.

  • Load Balancer Resources:

    • aws_lb: Represents an Amazon Application Load Balancer.

    • google_compute_http_health_check: Represents a Google Cloud HTTP Health Check.

    • azurerm_lb: Represents an Azure Load Balancer.

Data Sources:

  • An instrument of the Teecific infrastructure A data source in Terraform is used to retrieve data from external sources or from parts of the existing infrastructure that Terraform is not in charge of managing. You can access details about already-existing resources or services using data sources, which offer a read-only view of the data. You can use this knowledge to guide your Terraform setup decisions or as a source of reference.

  • Compute Data Sources:

    • aws_ami: Retrieves information about Amazon Machine Images (AMIs).

    • google_compute_image: Fetches details about Google Compute Engine images.

    • azurerm_virtual_machine: Retrieves information about Azure Virtual Machines.

  • Networking Data Sources:

    • aws_subnet: Retrieves details about Amazon VPC subnets.

    • google_compute_network: Fetches information about Google Cloud Virtual Networks.

    • azurerm_subnet: Retrieves details about Azure Subnets.

  • Storage Data Sources:

    • aws_s3_bucket: Retrieves information about Amazon S3 buckets.

    • google_storage_bucket: Fetches details about Google Cloud Storage buckets.

    • azurerm_storage_account: Retrieves information about Azure Storage Accounts.

  • Database Data Sources:

    • aws_db_instance: Retrieves information about Amazon RDS database instances.

    • google_sql_database_instance: Fetches details about Google Cloud SQL database instances.

    • azurerm_sql_server: Retrieves information about Azure SQL Database servers.

  • Security Data Sources:

    • aws_security_group: Retrieves information about Amazon EC2 Security Groups.

    • google_compute_firewall: Fetches details about Google Cloud Firewall Rules.

    • azurerm_network_security_group: Retrieves information about Azure Network Security Groups.

  • Monitoring Data Sources:

    • aws_cloudwatch_metric_alarm: Retrieves information about AWS CloudWatch metric alarms.

    • google_monitoring_metric_descriptor: Fetches details about Google Cloud Monitoring metric descriptors.

    • azurerm_monitor_metric_alert: Retrieves information about Azure Monitor metric alerts.

  1. Create a variable.tf file and define a variable

Create the variable.tf File:

  • Open your preferred text editor or integrated development environment (IDE). Name your new file variable.tf when you create it. It's vital to note that the.tf extension signifies that this file contains Terraform configuration.

Define a Variable:

  • In the "variable. tf" file, you can define one or more variables using the "variable" block. The basic structure of a variable definition looks like this

  • "variable_name": Replace this with the name you want to give your variables

  • "Description": To describe the function or use of the variable, you can write a description.

  • type: Indicate the variable's data type, such as "string," "number," "bool," "list," "map," or any other custom kinds that you have defined in your settings.

  • Standard (Optional) The variable has a default value that you can choose. If you don't supply the value for this, Terraform will ask you for it when you apply the configuration—unless you provide it by another method, such as environment variables or command-line flags.

Save the variable.tf File:

  • After defining your variables, save the variable.tf file. It should be saved in the same directory as your other Terraform configuration files.

Use the Variables in Your Terraform Configuration:

Now that you've defined your variables, you can use them in other Terraform configuration files (e.g., main.tf, provider.tf, etc.) by referencing them using the var keyword:

  1. Use the variable in a main.tf file to create a "local_file" resource

Define the Variable in variable.tf:

  • In your variable.tf file, define a variable that represents the content or path of the local file you want to create.

Use the Variable in main.tf:

  • In your main.tf file, reference the variable and use it in the local_file resource configuration.

  • In this example:

    • local_file is the resource type used to create a local file.

    • "example" is the label or name for this resource instance.

    • filename specifies the path and name of the local file you want to create. You should replace "/path/to/your/local/file.txt" with the actual path where you want to create the file.

    • content is set to var.file_content, which uses the value of the file_content variable as the content to write to the local file.

Initialize and Apply the Configuration:

  • After defining the variable and configuring the local_file resource in your main.tf file, you can initialize and apply the configuration using the following Terraform commands:

  • Initialize your Terraform working directory: "terraform init"

  • Apply the configuration to create the local file: "terraform apply"

  • During the apply process, Terraform will use the value of the file_content variable as the content for the local file specified in the filename attribute. If you want to change the content of the local file, you can do so by updating the file_content variable value or by specifying a different value when prompted during the apply process.

  1. Add required_providers to your configuration, such as Docker or AWS

Declare the necessary providers in your configuration file if you want to use external providers in your Terraform configuration, such as Docker or AWS. To add required_providers for both Docker and AWS, follow these steps:

For Docker:

  • If you haven't already, make sure you have the Docker provider installed. You can install it using the Terraform CLI:

  • In your Terraform configuration file (e.g., main.tf), specify the required_providers block for Docker. Here's an example:

    • docker is the name of the provider.

    • source specifies the provider's namespace and name on the Terraform Registry.

    • version specifies the minimum version of the provider you want to use. You can adjust the version constraint as needed.

For AWS:

  • Similarly, if you haven't already, make sure you have the AWS provider installed. You can install it using the Terraform CLI:

  • In your Terraform configuration file (e.g., main.tf), specify the required_providers block for AWS. Here's an example:

    • aws is the name of the provider.

    • source specifies the provider's namespace and name on the Terraform Registry.

    • version specifies the version constraint for the provider. Adjust the version constraint based on your requirements.

You can utilize the provided providers (in this case, Docker and AWS) in your Terraform setups after adding the required_providers blocks. Ensure that your configuration files also contain any additional resource or data source definitions required for these providers.

After making these modifications, don't forget to run terraform init once more to make sure Terraform downloads and configures the necessary providers based on your given versions.

  1. Test your configuration using the Terraform CLI and make any necessary adjustments
  • The following steps should be followed to test your Terraform configuration using the Terraform CLI and make any necessary adjustments:

  • Open the Terraform Configuration Directory.: Navigate to the directory containing the Terraform configuration files by opening a terminal or command prompt. Ensure that this directory contains your variable.tf, main.tf, and any other necessary configuration files.

  • Create the Terraform Working Directory from scratch: Run the following command to initialize your Terraform working directory. This command downloads the necessary provider plugins based on your required_providers configuration and prepares your workspace. "terraform init"

  • Verify Configuration: Before applying any changes, it's a good practice to validate your Terraform configuration to check for syntax errors and potential issues. Use the terraform validate command: "terraform validate"

  • Plan the Changes: Use the terraform plan command to create an execution plan. This step simulates what Terraform will do without actually making any changes to your infrastructure. It will show you what actions Terraform intends to perform:

    "terraform plan"

  • Apply the Changes: If the execution plan looks correct and you're ready to apply the changes, use the terraform apply command: "terraform apply"

  • Re-run Validate, Plan, and Apply: After making adjustments, repeat the following steps as needed:

    Validate your configuration: terraform validate Create a new execution plan: terraform plan Apply the changes: terraform apply

  • Destroy Resources (Optional): If you need to tear down the infrastructure you created, use the terraform destroy command:"terraform destroy"

THIS IS A SOME BASIC INFORMATION ABOUT TERRAFORM.

THANK YOU FOR WATCHING THIS BLOG AND THE NEXT BLOG COMING SOON