This repository contains practical examples and dedicated documentation to help you learn Terraform scripting, writing Terraform modules, and much more. Each directory contains examples to guide you through various Terraform concepts and use cases.
Terraform is an Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure using a high-level configuration language. This repository provides examples for beginners and advanced users alike, covering topics such as writing Terraform modules, using various Terraform blocks, and integrating with different cloud providers.
Before you start working with the examples, it's important to understand the basics of Terraform. Here is an overview of key concepts and blocks that you must know:
Terraform configurations are written in HCL (HashiCorp Configuration Language), a declarative language designed to be easy to read and understand. Terraform also supports JSON configuration syntax, though HCL is preferred for its readability.
- File Extensions: Terraform expects native syntax in
.tf
files and JSON syntax in.tf.json
files. The JSON syntax mirrors the native syntax, but some constructs may be more complex to represent in JSON due to its limitations.
Terraform configurations generally follow a basic structure. The typical Terraform file ends with the .tf
extension and can contain the following blocks:
A Terraform block specifies the required providers that Terraform needs to execute the script. It also defines the version of the provider and the source for downloading the provider.
Example:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}
The provider block specifies the cloud provider and API credentials required to interact with the provider's services.
Example (Azure):
provider "azurerm" {
features {}
subscription_id = "00000000-0000-0000-0000-000000000000"
tenant_id = "11111111-1111-1111-1111-111111111111"
}
A resource block represents a specific resource in the cloud provider’s services. This block defines the resource type, name, and configuration.
Example (Creating an Azure Resource Group):
resource "azurerm_resource_group" "example" {
name = "example"
location = "West Europe"
}
A data block is used to fetch data from the provider’s services that can be used in other resource blocks. It’s useful when you need information about resources that already exist.
Example (Fetching an Existing Resource Group):
data "azurerm_resource_group" "example" {
name = "existing"
}
A variable block is used to define input variables. These variables allow you to make your configuration more dynamic.
Example:
variable "resource_group_name" {
default = "myTFResourceGroup"
}
An output block is used to define output values generated by Terraform. Outputs are useful for passing values between modules or displaying information about the resources created.
Example:
output "resource_group_id" {
value = azurerm_resource_group.rg.id
}
Provisioners in Terraform allow you to execute scripts or commands on newly created resources or instances. These scripts can be used for configuring infrastructure, installing software, and running tests.
Terraform comes with built-in provisioners like:
file
: For copying files to a resource.
remote-exec
: For running commands on a remote machine.
Provisioners are triggered after a resource is created and can also be used during resource destruction.
Each directory in this repository contains a specific example with proper documentation. These examples will help you understand various concepts and use cases of Terraform.
1. Clone the repository: $ git clone <url>
2. Navigate to the example directory of your choice: $ cd example-1
3. Initialize Terraform in the directory: $ terraform init
4. use to see execution plan : $ terraform plan
5. use following cmd to provision reource : $ terraform apply
6. use following to destroy provisoned resource : $ terraform destroy
This repository provides hands-on examples that will guide you through learning Terraform. Each example includes proper documentation and explanations to help you understand how Terraform works and how you can use it to manage infrastructure.