Task 1: Familiarize yourself with HCL syntax used in Terraform
HCL --> HCL (HashiCorp Configuration Language) is a declarative configuration language developed by HashiCorp, a company specializing in infrastructure automation. It is designed to be easy to read and write while allowing users to describe infrastructure and configuration details for various tools and platforms.
Learn about HCL blocks, parameters, and arguments
HCL Blocks:
In HCL, configuration is organized into blocks. A block is a collection of configurations enclosed within curly braces
{}
. Each block has a block type and zero or more labels.Block types define the kind of configuration being described, for example, "resource", "provider", etc.
Labels are used to uniquely identify a block within its scope. They are defined within the block type and are typically used to specify the name or type of the configuration. Labels are optional in some cases.
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" }
In this example:
Block type:
resource
.Label:
"aws_instance.example"
.Configuration settings (
ami
andinstance_type
) are defined within the block.
Parameters:
Parameters are key-value pairs used to configure the behavior of a block or resource within an HCL configuration.
Parameters are specified within a block using the syntax
parameter_name = value
.Different block types support different parameters depending on their purpose.
In the previous example, ami
and instance_type
are parameters of the aws_instance
block.
Arguments:
Arguments are values assigned to parameters within a block.
They provide specific values or configurations to parameters.
In the previous example:
"ami-0c55b159cbfafe1f0"
is the argument for theami
parameter."t2.micro"
is the argument for theinstance_type
parameter.
Explore the different types of resources and data sources available in Terraform
In Terraform, resources and data sources are essential components used to define and interact with infrastructure providers such as cloud services, databases, networking components, and more.
Resources:
Resources in Terraform represent infrastructure components that you want to manage, create, update, or delete.
Each resource corresponds to a specific entity provided by a particular infrastructure provider, such as an AWS EC2 instance, a Google Cloud Storage bucket, or an Azure virtual network.
Resources are defined using a resource block in Terraform configuration files, specifying the resource type, its attributes, and any required or optional arguments.
resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" }
Data Sources:
Data sources in Terraform allow you to fetch information about existing resources or data from external systems or services.
They provide a way to query and retrieve attributes or metadata of existing infrastructure components without managing their lifecycle.
Data sources are defined using a data block in Terraform configuration files, specifying the data source type, its configuration, and any required or optional arguments.
Task 2: Understand variables, data types, and expressions in HCL
Variables:
Variables in HCL are used to store and reuse values across configurations.
They provide a way to make configurations more dynamic and reusable by parameterizing values.
Variables can be defined at various levels, including global (root module), module-level, or within resource blocks.
They can be assigned default values and marked as required or optional.
Data Types:
HCL supports several data types, including:
String: A sequence of characters enclosed in double quotes.
Number: Numeric values, including integers and floating-point numbers.
Bool: Boolean values
true
orfalse
.List: An ordered collection of elements of the same or different types.
Map: A collection of key-value pairs where keys are strings and values can be of any type.
HCL also supports complex data types such as tuples and objects.
Expressions:
Expressions in HCL allow you to perform computations, transformations, and evaluations within configurations.
They can be used for interpolation, arithmetic operations, string manipulation, and more.
HCL supports various functions and operators for working with expressions.
Create a variables.tf file and define a variable
Use the variable in a main.tffile to create a "local_file" resource
now using this variable in the main.tffile, make sure to make the main.tf in the same directory
-
terraform plan
-
terraform apply
-
and the output is --
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
We define three variables (
instance_type
,ami_id
, andinstance_name
) to parameterize the EC2 instance's configuration.The AWS provider is configured to specify the AWS region.
An EC2 instance resource is provisioned using the
aws_instance
block, referencing the variables for the AMI ID, instance type, and instance_name.Tags are assigned to the EC2 instance for identification and organization.
Make sure to replace the default values of the variables (
ami_id
) with appropriate values according to your AWS environment.
now ,
terraform init
# Initialize Terraform in the directory
terraform plan
# View the execution plan to verify the changes
terraform apply
# Apply the configuration to provision resources
output :- ec-2 instance is up and running
Thank you😛...