HashiCorp Configuration Language (HCL) is a structured configuration language developed by HashiCorp, a company known for creating various infrastructure automation tools like Terraform, Consul, Vault, and Nomad. HCL is designed to be a simple and intuitive language for writing configuration files that define infrastructure, service configurations, and other settings required for managing distributed systems and cloud-based resources. It has a JSON-like syntax with .tf
extension and supports various data types such as booleans, integers, strings, lists, maps, and objects.
HCL offers a number of features that make it well-suited for describing infrastructure:
Declarative: HCL describes the desired state of infrastructure, rather than the steps to reach that state. This makes it easier to read and understand, and it also makes it easier to make changes to the configuration.
Extensible: HCL is extensible, so you can define your own resources, which are the building blocks of infrastructure components like virtual machines, networks, databases, etc. Each resource is associated with a provider, which is responsible for managing resources of a specific infrastructure platform (e.g., AWS, Azure, Google Cloud). This makes it possible to manage a wide variety of infrastructure resources with Terraform.
Dependency management: HCL automatically manages dependencies between resources. It understands the relationships between resources and ensures they are created in the correct order.
State management: HCL keeps track of the state of the infrastructure in a state file. This file is used to determine what changes need to be applied on subsequent runs and helps maintain the desired state.
HCL uses a declarative syntax to specify the desired state of the infrastructure. Here's an example:
# Define variables for better reusability and flexibilityvariable "ami_id" {default = "ami-0c55b159cbfafe1f0" # Default AMI ID for non-production environments}variable "is_production" {default = false # Set this to true for production environment, false for non-production}# AWS provider configurationprovider "aws" {region = "us-west-2" # Specify the AWS region where resources will be created}# AWS EC2 instance resourceresource "aws_instance" "example" {ami = var.is_production ? "ami-prod" : var.ami_id # Use different AMI ID for production or the default for non-productioninstance_type = "t2.micro" # Specify the instance typekey_name = "my-key-pair" # Specify the SSH key pair for authenticationtags = {Name = "Example EC2 Instance" # Tags help identify and organize resources}}
Note: In this example, we declare an AWS EC2 instance named
example_instance
with a specific Amazon Machine Image (AMI) and instance type.
Below are the features of HCL used in the code given above.
Comments: Lines starting with a hash #
symbol are treated as comments and are ignored by the interpreter.
Blocks: HCL uses blocks to define configurations. A block is a collection of key-value pairs that represent a specific resource or configuration element. It is denoted by curly braces {}
. The content inside the block should be indented. Example:
block_type "block_name" {# Block content, which includes configuration options and nested blocks}
block_type
: This represents the type of block you want to define, such asprovider
,resource
,variable
,data
, etc.
"block_name"
: This is the name given to the block, and it is used to identify the block within the configuration.
Resource: A resource is a block used to define and manage infrastructure components within a cloud provider or other infrastructure service.
resource "provider_type" "resource_name" {# Configuration options for the resourceattribute1 = value1attribute2 = value2# ...}
provider_type
: This specifies the resource provider (e.g.,aws
,azurerm
,
resource_name
: A local name given to the resource block, which can be used to reference this resource within the Terraform configuration.
attribute1
,attribute2
, etc.: These are the various attributes specific to the resource being created. The available attributes depend on the resource type and the provider being used.
Keywords: HCL uses keywords to define different types of configurations, such as resource
, variable
, provider
, etc.
Assignments: To set a value to a particular key, use the =
symbol.
Strings: Strings are written within double quotes ""
.
Numeric values: Numbers can be integers or floats.
Lists: Lists are used to define arrays of values. They are represented using square brackets []
, and individual elements are separated by commas. For example:
variable "<VAR_NAME>" {type = list[<ELEMENT_TYPE>]default = [<DEFAULT_ELEMENTS>]}
Variables: HCL allows defining variables that can be used throughout the configuration. They can be defined in separate variable blocks or using environment variables.
variable "<VAR_NAME>" {[type = <TYPE>][default = <DEFAULT_VALUE>][description = "<DESCRIPTION>"]}
<VAR_NAME>
: This is the name given to the variable, and it is used to reference the variable throughout the configuration.
[type = <TYPE>]
: This is an optional argument that specifies the data type of the variable Common types includestring,
number
,bool
,list
,map
, etc.
[default = <DEFAULT_VALUE>]
: This is an optional argument that sets a default value for the variable.
[description = "<DESCRIPTION>"]
: This is an optional argument that allows you to provide a description or documentation for the variable.
Conditional expressions: HCL allows using conditional expressions to make decisions in the configuration.
condition ? true_val : false_val
condition
: This is the expression or condition that you want to evaluate. It can be any valid expression that results in a boolean value (true
orfalse
).
true_val
: This is the value that will be returned if thecondition
evaluates totrue
.
false_val
: This is the value that will be returned if thecondition
evaluates tofalse
.
Provider: In HCL, a provider is a plugin that allows Terraform to interact with various cloud or infrastructure platforms, following is syntax to define provider:
provider "<PROVIDER_NAME>" {# Provider-specific configuration options}
Here,
<PROVIDER_NAME>
should be replaced with the name of the provider you want to use.
Free Resources