Planning projects with Terraform

Jigar R
2 min readJan 28, 2025

--

Terraform Cloud allows one to Manage multiple organizations’ cloud infrastructure. The following image demonstrates various important terminologies.

The cloud infrastructure can be sorted into multiple projects. For example, ProjectA needs 5 virtual machines in production but 1 for the staging environment. In this case, consider creating 2 workspaces: Prod-A and Dev-A. The number of virtual machines can be specified as a variable specific to a workspace.

Terraform needs permission to access your cloud infrastructure to create resources. One can store authentication-related data as Environment Variables or hard-code them in a file. What if you can store these tokens and allow different workspaces to access them? This is where Variable Sets are extremely helpful.

Using Terraform, one can define the infrastructure needed to support projectA. The specifications for the Production and Staging environment would be slightly different. The variable part of the infrastructure can be defined as variables. By doing this, we avoid unnecessary repetition and reuse the code. In the following section, I will cover Terraform’s Inputs and Outputs.

Inputs to Terraform

using Variables

We can define variables in a dedicated file “variables.tf” file as follows

### In variables.tf file
variable "instance_name" {
description = "Value of the Name tag for the EC2 instance"
type = string
}

### To use variable; use following synatax
Name = var.instance_name


## if the default value is not specified then Terraform will ask for one
➜ terraform plan
var.instance_name
instance_name

Enter a value:

### Use -var flag to specify values

Overriding Variable Sets (Terraform Cloud)

using Data source

Data Source allows one to find

Terraform to use information defined outside of Terraform

### Example from AWS provider
### https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#example-usage
data "aws_ami" "ubuntu" {
most_recent = true

filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

owners = ["099720109477"]
}

Outputs from Terraform

Once you run, the “terraform apply” command, it would create infrastructure resources. Sometimes, you want to save information like

  • IP addresses of virtual machines
  • IDs of certain resources

By using the “output” block, the information gets stored in the Terraform state file. Use the “terraform output” command to view saved information.

### Defining output in outputs.tf file
output "route_table_ID" {
value = aws_route_table.AppRouteTable.route_table_ID
}


### To see outputs
➜ terraform output
route_table_ID = "XXXXXX"

--

--

Jigar R
Jigar R

Written by Jigar R

DevOps Engineer | feel free to reach out to me | LinkedIn — https://www.linkedin.com/in/jigarrathod/

No responses yet