Table of contents
- Task 1 - Familiarize yourself with the HCL syntax used in Terraform
- Learn about HCL blocks, parameters, and arguments
- Parameters:
- Arguments:
- Explore the different types of resources and data sources available in Terraform
- Resources:
- Type of Resources:
- Data Source
- Task2: Understand variables, data types, and expressions in HCL
- Variables:
Task 1 - Familiarize yourself with the HCL syntax used in Terraform
Learn about HCL blocks, parameters, and arguments
HCL BLOCK:
In Terraform "HCL" stands for HashiCorp Configuration Language is used to define and configure infrastructure resources. HCL files typically have a .tf
extension and contain the configuration for a Terraform project. HCL uses blocks to define resources. Within each block, you can specify attributes and their corresponding values to configure the desired behavior of the resource.
A block is a container for other content. Blocks have a type
that can have zero or more required labels followed by { }
brackets that contain the block's body. Blocks can be nested inside each other. This a general representation of a block
type "<block>" "<parameters>" {
argument_1 = value_1
argument_2 = value_2
}
There are several block types used to define different aspects of infrastructure configuration.
Provider Block:
Specifies the cloud or service provider that Terraform will interact with.
Contains configuration settings such as access credentials and API endpoints.
Resource Block:
Defines a specific infrastructure resource that Terraform will manage.
Specifies the resource type, such as EC2 instances, S3 buckets, or Dynamodb table. Contains attributes that configure the behavior and properties of the resource.
Data Block:
Retrieves and imports data from an external source into the Terraform configuration.
Variable Block:
1. Declares input variables that can be passed into the Terraform configuration.
2. Specifies the variable name, type, default value, and other optional properties. Enables parameterization and dynamic configuration of the infrastructure.
Output Block:
Defines values that Terraform will display as outputs after successful resource provisioning.
Module Block:
Encapsulates a reusable set of Terraform configurations into a single module.
Defines input variables and output values specific to the module. It enables code reuse and modularity by grouping related resources and configurations.
Provisioner Block:
Specifies actions to be taken on a resource after it is created or updated.
Allows for running scripts or executing commands on the resource. It is commonly used for tasks like initialization, configuration, or software installation.
Locals Block:
Declares local values or expressions that can be used within the Terraform configuration.
Enables the creation of intermediate variables or complex expressions for reuse.
Terraform Block
Contains global configuration settings for Terraform itself and includes backend configuration, required provider versions, and other global settings.
Parameters:
Parameters are variables that are defined within HCL blocks.
They allow you to customize how resources are created.
Arguments:
Arguments are the specific values assigned to the parameters within HCL blocks.
They provide concrete values to the parameters and influence the behavior of resources or components.
Explore the different types of resources and data sources available in Terraform
Resources:
Resources are the most important part of Terraform. Resources are defined by resource
blocks. A resource can define one or more infrastructure resource objects, such as EC2, S3 or DNS records, key-value pair data, etc.
resource "aws_instance" "web_server" {
ami = "ami-0f5ee92e2d63afc18"
instance_type = "t2.micro"
}
Type of Resources:
Compute Resources:
Instances: Virtual machines or server instances, such as AWS EC2 instances, Google Compute Engine instances, or Azure virtual machines.
Containers: Container resources, like AWS ECS tasks, Kubernetes pods, or Azure Container Instances.
Serverless Functions: Resources for serverless computing, such as AWS Lambda functions, Google Cloud Functions, or Azure Functions.
Networking Resources:
Virtual Networks: Network components like virtual private clouds (VPCs), subnets, and routing tables, such as AWS VPC, Google VPC, or Azure VNet.
Load Balancers: Resources for load balancing traffic, like AWS ELB/ALB/NLB, Google Load Balancer, or Azure Load Balancer.
DNS: Domain Name System resources, such as AWS Route 53, Google Cloud DNS, or Azure DNS.
Storage Resources:
Block Storage: Resources for block-level storage, such as AWS EBS volumes, Google Persistent Disks, or Azure Managed Disks.
Object Storage: Resources for object storage, like AWS S3 buckets, Google Cloud Storage buckets, or Azure Blob Storage.
File Storage: Resources for file storage, such as AWS EFS, Google Cloud Filestore, or Azure Files.
Database Resources:
Relational Databases: Managed relational databases, like AWS RDS, Google Cloud SQL, or Azure Database for MySQL/PostgreSQL/SQL Server.
NoSQL Databases: Managed NoSQL databases, such as AWS DynamoDB, Google Cloud Firestore, or Azure Cosmos DB.
Data Warehouses: Resources for data warehousing, like AWS Redshift, Google BigQuery, or Azure Synapse Analytics.
Security Resources:
Identity and Access Management: Resources for managing identities, permissions, and access control, such as AWS IAM, Google Cloud IAM, or Azure AD.
Encryption and Key Management: Resources for encryption and key management, like AWS KMS, Google Cloud KMS, or Azure Key Vault.
Security Groups: Resources for network security groups or firewalls, such as AWS Security Groups, Google VPC Firewall Rules, or Azure Network Security Groups.
Data Source
Data sources provide dynamic information about entities that are not managed by the current Terraform and configuration.
It also allow Terraform to fetch information about existing infrastructure, like an existing AWS VPC.
Type of Data Source:
Infrastructure Data Sources
"aws_vpc" - Retrieves information about an existing Amazon VPC.
"google_compute_network" - Retrieves information about a Google Cloud VPC network.
"azurerm_subnet" - Retrieves information about an Azure subnet.
Cloud Service Data Sources
"aws_s3_bucket" - Retrieves information about an existing Amazon S3 bucket.
"google_storage_bucket" - Retrieves information about a Google Cloud Storage bucket.
"azurerm_cosmosdb_account" - Retrieves information about an Azure Cosmos DB account.
DNS Data Sources
"aws_route53_zone" - Retrieves information about an Amazon Route 53 DNS zone.
"google_dns_managed_zone" - Retrieves information about a Google Cloud DNS managed zone.
"azurerm_dns_zone" - Retrieves information about an Azure DNS zone.
Security Data Sources
"aws_iam_policy" - Retrieves information about an AWS IAM policy.
"google_service_account" - Retrieves information about a Google Cloud service account.
"azurerm_key_vault" - Retrieves information about an Azure Key Vault.
Database Data Sources
"aws_db_instance" - Retrieves information about an Amazon RDS database instance.
"google_sql_database_instance" - Retrieves information about a Google Cloud SQL database instance.
"azurerm_mariadb_server" - Retrieves information about an Azure Database for MariaDB server.
Task2: Understand variables, data types, and expressions in HCL
Variables:
In Terraform, variables are a way to parameterizing and sharing values within your configurations and modules. They allow you to make your configurations more dynamic, reusable, and flexible. There are two types of variables Input and output variables in Terraform
Input Variables:
Input variables are used to parameterize your Terraform configurations. They allow you to pass values into your modules or configurations from the outside. Input variables can be defined within a module or at the root level of your configuration. Here's how you define an input variable:
for ex: create one file variables.tf
variable "ami_id" {
description = "AMI value of ec2 instance"
type = string
default = "ami-03f4878755434977f"
}
variable "instance_type" {
description = "AMI value of ec2 instance"
type = string
default = "t2.micro"
}
Output Variables:
Output variables allow you to expose values from your module or configuration, making them available for use in other parts of your Terraform setup. Here's how you define an output variable:
output "example_output" {
description = "An example output variable"
value = resource.example_resource.example.id
}
Use the variable in a main.tf file to create a resource "EC2"
Task3: writing Terraform configurations using HCL syntax:
Add required_providers to your configuration, such as Docker, AWS, or Azure
In the below block, we added the provider for AWS
Now we will create one resource "EC2" in AWS and test the configurations.
Initialize Terraform: using terraform init
Terraform plan:
Terraform Apply:
After Enter value: Yes, Resource will be created: