Task 1: Familiarize yourself with HCL syntax used in Terraform
Terraform HCL (HashiCorp Configuration Language) is the language used to write Terraform configurations. It consists of:
Blocks: Blocks are used to define different types of configurations, such as providers, resources, data sources, and modules. Each block has a specific syntax and purpose.
Parameters: Parameters are settings within blocks that configure the behaviour of those blocks. For example, the
region
parameter in an AWS provider block specifies the AWS region to use.Arguments: Arguments are key-value pairs within parameters that set specific values for the configuration. In an AWS EC2 instance resource block, the
ami
argument specifies the Amazon Machine Image ID for the instance.
Terraform provides various types of resources and data sources for interacting with different infrastructure platforms:
Resources:
Compute Resources:
aws_instance
: Represents an AWS EC2 instance.google_compute_instance
: Represents a GCP Compute Engine instance.azurerm_virtual_machine
: Represents an Azure virtual machine.
Networking Resources:
aws_vpc
: Represents an AWS Virtual Private Cloud.google_compute_network
: Represents a GCP VPC network.azurerm_virtual_network
: Represents an Azure virtual network.
Database Resources:
aws_db_instance
: Represents an AWS RDS database instance.google_sql_database_instance
: Represents a GCP Cloud SQL database instance.azurerm_sql_server
: Represents an Azure SQL Server.
Storage Resources:
aws_s3_bucket
: Represents an AWS S3 bucket.google_storage_bucket
: Represents a GCP Cloud Storage bucket.azurerm_storage_container
: Represents an Azure Blob Storage container.
Container Resources:
aws_ecs_cluster
: Represents an AWS ECS cluster.google_container_cluster
: Represents a GCP Kubernetes Engine cluster.azurerm_container_group
: Represents an Azure Container Group.
Data Sources:
Compute Data Sources:
aws_ami
: Retrieves information about an AWS AMI.google_compute_image
: Retrieves information about a GCP Compute Engine image.azurerm_virtual_machine_image
: Retrieves information about an Azure VM image.
Networking Data Sources:
aws_subnet
: Retrieves information about an AWS subnet.google_compute_network
: Retrieves information about a GCP VPC network.azurerm_subnet
: Retrieves information about an Azure subnet.
Database Data Sources:
aws_db_instance
: Retrieves information about an AWS RDS database instance.google_sql_database_instance
: Retrieves information about a GCP Cloud SQL database instance.azurerm_sql_server
: Retrieves information about an Azure SQL Server.
Storage Data Sources:
aws_s3_bucket
: Retrieves information about an AWS S3 bucket.google_storage_bucket
: Retrieves information about a GCP Cloud Storage bucket.azurerm_storage_container
: Retrieves information about an Azure Blob Storage container.
Container Data Sources:
aws_eks_cluster
: Retrieves information about an AWS EKS cluster.google_container_cluster
: Retrieves information about a GCP Kubernetes Engine cluster.azurerm_container_group
: Retrieves information about an Azure Container Group.
Task 2: Understand variables, data types, and expressions in HCL
How to create variables.tf file and define a variable
Use the variable in the main.tf file.
Below is a minimal example of a variables.tf file defining a variable in Terraform:
# variables.tf
variable "filename" {
default = "/home/ubuntu/terra/automate.txt"
}
variable "content" {
default = "This is the automated file"
}
You can use this variable in your Terraform configuration files (e.g., main.tf
) like this:
#main.tf
#This block is normal file creation with contents in it.
resource "local_file" "myfile" {
filename = "/home/amin/terraform/TerraVariables/devops.txt"
content = "This is my file with updated comments"
}
#This block will access the value from the file variables.tf
resource "local_file" "DevOpsFile" {
filename = var.filename
content = var.content
}
Here, we use the "var.filename" and "var.content" references to access the value of the "filename" and "content" variables defined in variables.tf to create the file "DevOpsFile" and add contents to it.
We will first Initialize Terraform using the command:
terraform init
Once done we can now plan what actions will be taken using
terraform plan
And after running the plan command, this is how it will look like
We can now run the command
terraform apply
We have now successfully created the files and their contents using Terraform
Task 3: Practice writing Terraform configurations using HCL syntax
Add required_providers to your configuration, such as Docker or AWS
Test your configuration using the Terraform CLI and make any necessary adjustments
To add required_providers
to your configuration, you'll need to specify the required provider, version, and source. In your case, you want to use the "local" provider, which doesn't require a version, as it's a built-in provider. Here's how you can update your Terraform configuration to include the required_providers
block:
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.0.0"
}
}
}
provider "local" {}
resource "local_file" "myfile" {
filename = "/home/amin/terraform/TerraVariables/devops.txt"
content = "This is my file with updated comments"
}
resource "local_file" "DevOpsFile" {
filename = var.filename
content = var.content
}
In this updated main.tf
:
We've added the
terraform
block, which contains therequired_providers
block.Inside the
required_providers
block, we specify the AWS provider with its source and an optional version. Make sure to use the correct source and version for the AWS provider you intend to use.
Once we Initialize terraform again we get the below output:
Hope you like my post. Don't forget to like, comment, and share.