TABLE OF CONTENTS
Task 1: Familiarize yourself with HCL syntax used in Terraform
Task 2: Understand variables, data types, and expressions in HCL
Task 3: Practice writing Terraform configurations using HCL syntax
Task 1: Familiarize yourself with HCL syntax used in Terraform
HCL (HashiCorp Configuration Language)
a configuration language designed to be both human and machine-readable for use in infrastructure automation. It is a data serialization language designed to help developers write and deploy cloud resources across multiple platforms.
Learn about HCL blocks, parameters, and arguments
Blocks
A block is a container for other content, Blocks are used to define resources, data sources, variables, and providers. They are enclosed in curly braces {}
. Blocks are hierarchical and can be nested within each other.
resource "aws_instance" "example" {
ami = "abc123"
network_interface {
# ...
}
}
some examples of blocks, that we will use in our deployments:
Resource Block
Terraform block
Provider block
Basic format of writing terraform code
block "parameter" "parameter" {
argument 1
argument 2
}
Parameters: Parameters define the characteristics or behaviour of the element. Each block has its own specific set of parameters. Blocks have settings or attributes called parameters. They control a resource's or configuration's behavior. Block-defined parameters are key-value pairs
Arguments: Arguments are the actual values or information assigned to specific parameters within a block using the "=" sign.
resource "aws_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
In the above example,
resource is a block
'aws_instance is resouce_type, which also a resource parameter
'ami' and 'instance_type' are arguments of the aws_instance resource block.
Explore the different types of resources and data sources available in Terraform
Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components such as DNS records. different types of resources are as follows:
Compute Resources like, aws_instance
Networking Resources like, aws_vpc
Database Resources like, aws_db_instance
Storage Resources like, aws_s3_bucket
Security Resources like, aws_security_group
Data Sources:
Data sources in Terraform give details on resources or configurations that are already in use or that are not part of the current Terraform setup. They are normally read-only and are defined using the data block. Following is the format for defining a data source.
Terraform State Data Sources: It can access information stored in the Terraform state file, such as the IP address of a created resource.
External Data Sources: These enable to fetching of data from external systems or APIs and use it in configuration.
Task 2: Understand variables, data types, and expressions in HCL
- Create a variable.tf file and define a variable.
variable "ec2-ubuntu-ami"{
default="ami-053b0d53c279acc90"
}
variable "ec2-amazon_linux-ami"{
default="ami-01c647eace872fc02"
}
Use this variable in a main.tf file to create a "local_file" resource
provider "aws" {
region = "us-east-1"
resource "aws_instance" "terra-server"{
ami = "var.ec2-ubuntu-ami"
}
Task 3: Practice writing Terraform configurations using HCL syntax
- Add required_providers to your configuration, such as Docker or AWS
Here's the terraform provider block...
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}
Now we will write main.tf file for docker image & container
provider "docker" {
}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = true
}
resource "docker_container" "nginx" {
name = "nginx"
image = docker_image.nginx.name
ports {
external = 8080
internal = 80
}
}
Give the terraform init command to initialize the plugins
terraform init
terraform plan
Terraform plan command to preview the changes that Terraform will make to your infrastructure before actually applying those changes.
terraform apply
Terraform will then create or modify the resources as specified in your configuration.
Final Output:
Here's the end of day #2, see you tomorrow!
Happy Learning!