DEV Community

Rashmitha v
Rashmitha v

Posted on

Transforming Cloud Infrastructure with Terraform: Build, Change, Deploy

In today's cloud-centric landscape, managing infrastructure efficiently is key to scalability and reliability. Enter Terraform, an Infrastructure as Code (IaaC) tool that revolutionizes how we provision, manage, and evolve cloud resources across platforms like AWS, Azure, and Google Cloud. With Terraform, you define your infrastructure in declarative configuration files, ensuring consistency and repeatability. This process allows for swift deployment and seamless updates through its plan-apply cycle, enabling rapid iteration and ensuring infrastructure changes are predictable and reliable. Whether you're automating the setup of a new environment or orchestrating complex multi-cloud architectures, Terraform power lies in its ability to codify infrastructure as easily as software, unlocking agility and scalability while reducing operational overhead.

Building the infrastructure

1. Terraform Configuration

Each Terraform configuration must be in its own working directory. Create a directory for your configuration.

mkdir terraform-learning
Enter fullscreen mode Exit fullscreen mode

Change into the directory

cd terraform-learning
Enter fullscreen mode Exit fullscreen mode

Create a file to define your infrastructure.

code main.tf
Enter fullscreen mode Exit fullscreen mode

Open main.tf in your text editor, give the configuration below, and save the file.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-west-2"
}

resource "aws_instance" "My_app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}
Enter fullscreen mode Exit fullscreen mode

2.Initialize the Terraform

terraform init
Enter fullscreen mode Exit fullscreen mode

3. Create the infrastructure
Apply the configuration now with the terraform apply command.

terraform apply
Enter fullscreen mode Exit fullscreen mode

Changing the infrastructure
1: Configuration of new AMI

resource "aws_instance" "My_app_server" {
-  ami           = "ami-830c94e3"
+  ami           = "ami-08d70e59c07c61a3a"
   instance_type = "t2.micro"
 }
Enter fullscreen mode Exit fullscreen mode

2: Apply the changes

After changing the configuration, run terraform apply again to see how Terraform will apply this change to the existing resources.

terraform apply
Enter fullscreen mode Exit fullscreen mode

Destroy the infrastructure
Once you no longer need infrastructure, you may want to destroy it to reduce your security exposure and costs.

terraform destroy
Enter fullscreen mode Exit fullscreen mode

The terraform destroy command terminates resources managed by your Terraform project.
Defining the Input Variable

1: Set the instance name with variable

Create a new file called variables.tf with a block defining a new instance_name variable.

variable "instance_name" {
  description = "Value of the Name tag for the EC2 instance"
  type        = string
  default     = "ExampleInstance"
}
Enter fullscreen mode Exit fullscreen mode

2: Update main.tf

In main.tf, update the aws_instance resource block to use the new variable. The instance_name variable block will default to its default value ("ExampleInstance") unless you declare a different value.

resource "aws_instance" "My_app_server" {
   ami           = "ami-08d70e59c07c61a3a"
   instance_type = "t2.micro"

   tags = {
-    Name = "ExampleInstance"
+    Name = var.instance_name
   }
 }
Enter fullscreen mode Exit fullscreen mode

3: Apply Configuration_
Apply the configuration. Enter yes to confirm the cofiguration.

terraform apply
Enter fullscreen mode Exit fullscreen mode

4: Passing the variable

Now apply the configuration again, this time overriding the default instance name by passing in a variable using the -var flag.

terraform apply -var "instance_name=SecondNameForInstance"

Enter fullscreen mode Exit fullscreen mode

Query the Data
1: Output EC2 instance configuration

Create a file called outputs.tf in your learn-terraform-aws-instance directory.

code output.tf
Enter fullscreen mode Exit fullscreen mode

2: Inspect output values

Apply the configuration and enter yes to confirm it.

terraform apply

Enter fullscreen mode Exit fullscreen mode

3: Query Output value

Query the outputs with the terraform output command.

terraform output

Enter fullscreen mode Exit fullscreen mode

In conclusion, Terraform stands as a transformative tool in the realm of cloud infrastructure management. By leveraging Infrastructure as Code principles, Terraform enables organizations to streamline the deployment and management of cloud resources across various providers. Its declarative configuration approach ensures consistency and reliability, facilitating rapid iteration and scalability.

Top comments (0)