TerraWeek Day 2

Raj Kumar
3 min readJun 6, 2023

--

Practice writing Terraform configurations using HCL syntax

1. What are blocks, parameters, and arguments?

Blocks:

  • 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.

Parameters:

  • Parameters: Parameters are attributes or properties of a resource or configuration element. They define the characteristics or behavior of the element. Each block has its own specific set of parameters.

Arguments:

  • Arguments: Arguments are the values assigned to the parameters. They provide the actual values or information for a specific parameter. Arguments are assigned to parameters using the = sign within the block.
provider "aws" {
region = var.region
access_key = "<your_access_key>"
secret_access_key = "<your_secret_access_key>"
}

In the above code;

provider is the block keyword used to define a provider.

"aws"Provider type

region, access_key, and secret_access_key — Configuration Parameters

<your_access_key> , <your_secret_access_key>Arguments

2. Different types of resources and data sources available in Terraform

In Terraform, there are different types of resources and data sources available that you can use to manage and retrieve information from various infrastructure providers. Here are some common types:

Resources:

Resources are the core building blocks in Terraform that represent infrastructure objects that you want to manage. They are typically used to create, update, or delete resources in your infrastructure provider.

Examples of resource types:

  • aws_instance: Represents an EC2 instance in AWS.
  • aws_s3_bucket: Represents an S3 bucket in AWS.
  • google_compute_instance: Represents a Compute Engine instance in Google Cloud Platform (GCP).
  • azurerm_virtual_network: Represents a virtual network in Microsoft Azure.

Data Sources:

Data sources allow you to fetch information from external systems or existing resources that are already created in your infrastructure provider. Data sources are read-only and provide information that can be used elsewhere in your Terraform configuration.

Examples of data source types:

  • aws_ami: Retrieves information about an Amazon Machine Image (AMI) in AWS.
  • aws_vpc: Retrieves information about a virtual private cloud (VPC) in AWS.
  • google_storage_bucket: Retrieves information about a storage bucket in GCP.
  • azurerm_resource_group: Retrieves information about a resource group in Azure.

Data sources are useful when you need to reference existing resources or retrieve information for dynamic configurations.

3. Create variables.tf and main.tf file on AWS.

STEP 1: Create an Instance

Create Ubuntu Machine on AWS and Connect it with the Terminal

STEP 2: Install Terraform

Install Terraform by using the commands given in https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

Terraform Version 1.4.6

STEP 3: Create a working directory and variables.tf , main.tffile.

sudo su
mkdir terraform
cd terraform/
pwd #copy_the_path
variable "filename" {
default = "/home/ubuntu/terraform/devops-automate.txt"

}

variable "aws_ec2_object" {
type = object({
name = string
instances = number
keys = list(string)
ami = string
})
default = {
name = "test_ec2_instance"
instances = 4
keys = ["key1.pem","key2.pem"]
ami = "ubuntu-afed34"
}
}

In main.tf file,

resource "local_file" "sample" {
filename = "/home/ubuntu/terraform/devops-automate.txt"
content = "Sample_Terraform_Code"
}

output "aws_ec2_instances" {
value = var.aws_ec2_object.instances
}
terraform init
terraform validate
terraform plan
terraform apply

Thank you shubhamlondhe , for the wonderful session

#devops #terraweek #terraform #hcl #aws

--

--

Raj Kumar
Raj Kumar

Written by Raj Kumar

AWS Certified Solution Architect

No responses yet