DEV Community

Mohana Priya
Mohana Priya

Posted on

Transforming Cloud Infrastructure with Terraform: Build, Change, Deploy

Intoduction:
In today's fast-paced tech world, infrastructure as code (IaC) has become essential for managing and automating your cloud resources. Recently, I had the opportunity to dive into Terraform, an open-source IaC tool that allows you to define and provision your infrastructure using a simple, declarative programming language. In this blog, I'll walk you through my journey of building, changing, and deploying infrastructure, as well as querying data outputs using Terraform.

Pre-requisites:

Before we dive into the steps, let's ensure you have the following prerequisites in place:

  1. AWS Account: If you don't have one, sign up for an AWS account.
  2. Terraform Installed: Download and install Terraform from the official website.
  3. AWS CLI Installed: Install the AWS CLI by following the instructions here.
  4. AWS Credentials Configured: Configure your AWS CLI with your credentials by running aws configure.

Building the infrastructure

Step-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

Step-2: Initialize the Terraform

Initializing a configuration directory download and installs the providers defined in the configuration, which in this case is the aws provider.

terraform init
Enter fullscreen mode Exit fullscreen mode

Step-3: Create the infrastructure

Apply the configuration now with the terraform apply command.

terraform apply
Enter fullscreen mode Exit fullscreen mode

Enter Yes to apply the cofiguration.
You have now created infrastructure using Terraform! Visit the EC2 console and find your new EC2 instance.

Changing the infrastructure

Step-1: Configuration of new ami

Now update the ami of your instance. Change the aws_instance.My_app_server resource under the provider block in main.tf by replacing the current AMI ID with a new one.

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

Enter fullscreen mode Exit fullscreen mode

Step-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

As included before terraform destroys the existing instance first and the create the new instance in place of it.

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

Step-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

Step-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

Step-3: Apply Configuration

Apply the configuration. Enter yes to confirm the cofiguration.

terraform apply
Enter fullscreen mode Exit fullscreen mode

Step-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

Terraform will update the instance's Name tag with the new name.

Query the Data

Step-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

Step-2: Inspect output values

Apply the configuration and enter yes to confirm it.

terraform apply
Enter fullscreen mode Exit fullscreen mode

Step-3: Query Output value

Query the outputs with the terraform output command.

terraform output
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Working with Terraform has been an enlightening experience. Its simplicity and power make managing infrastructure a breeze. Whether you're setting up a static website or managing complex cloud environments, Terraform's declarative approach and extensive provider support have got you covered.
Happy Coding!

Top comments (0)