Terraform: Launching the first infrastructure in AWS

Terraform: Launching the first infrastructure in AWS

In this blog, we will deep dive into the Terraform infrastructure deployment in the AWS cloud. Terraform has emerged as one of the top IaC tools to deploy infrastructures in the cloud. Let's learn more about the Terraform along with the hands-on experience.

Terraform

Terraform is an open-source infrastructure as code (IaC) tool that allows us to define and provision infrastructure in a declarative configuration language. Terraform is a simple and superfast infrastructure deploying the tool. It supports different cloud providers like AWS, GCP, Azure, and so on.

Advantages of Terraform

  • Infrastructure as Code

  • Multi-cloud support

  • Scalability

  • Community driven

Terraform lifecycle

Terraform has a lifecycle that defines the stages involved in managing infrastructure using Terraform configurations. Here are the key stages in the Terraform lifecycle:

  • Terraform init - Terraform downloads the required provider plugins and sets up the working directory in this stage.

  • Terraform Plan - In this stage, Terraform analyzes the configuration files and the current state of the infrastructure to determine what changes need to be made

  • Terraform Apply - In this stage Terraform executes the planned changes to the infrastructure and creates the infrastructure.

  • Terraform Destroy - In this stage the Terraform deletes all the infrastructures that were created/deployed.

In Terraform HCL language is used. We can not remember the codes to create any resource so we will take the references from the official documentation.

Create an ec2 instance in the AWS using Terraform as IaC.

main.tf

provider "aws" {
    region = "us-east-1"

}
resource "aws_instance" "ubuntu" {
    ami = "ami-0c7217cdde317cfec"
    instance_type = "t2.micro"
    key_name = "nod.pem"

}

Provider block specifies the provider for which we are configuring resources, in this case, it's the AWS provider and the region specifies at which location we are creating the resources.

In the resource block, aws_instance is the type of resource and the instance will be referred as "ubuntu". The remaining attributes like ami, instance_type are the mandatory attributes so we had mentioned in the code.

Since we are creating the infrastructure in AWS, we will configure the AWS credentials in our terminal first.

 aws configure

We have our main.tf file and AWS is configured. Let's use the Terraform command.

Terraform init

Terraform is initialized successfully.

terraform plan

terraform apply

Once we hit the command, we need to enter a value "yes" to start the creation of resources. If not entered the resources will not be created.
Our resource is created and we will verify in AWS.

The ec2 instance is created successfully.

Destroy the resource

terraform destroy

The resource is destroyed successfully.

Case2: Creating resources using variables and output
But if we look at the code sensitive information like ami id, and instance type are exposed which should be kept secured. For this case, we will use the variables and if we want to get some information after the resources are created we will get that via the output.tf.

Let's create the resource using the variables and output.

main.tf

provider "aws" {
    region = var.aws_region

}
resource "aws_instance" "ubuntu" {
    ami = var.ami_value
    instance_type = var.instance_type_value

}

variables.tf

variable "aws_region" {
    description = "AWS region where the resources will be created"

}
variable "ami_value" {
    description = "value for the ami"


}

variable "instance_type_value" {
    description = "value for the instance type"

}

terraform.tfvars

aws_region = "us-east-1"
ami_value = "ami-0c7217cdde317cfec"
instance_type_value = "t2.micro"

terraform.tfvars is a file that allows us to store and organize variable values separately from our main Terraform configuration files. This file typically contains values for variables used in our Terraform configurations, and it provides a way to keep sensitive or environment-specific information separate from the main code.

output.tf

output "public-ip-address" {
 value = aws_instance.ubuntu.public_ip 
}

This output definition allows us to easily access the public IP address once the Terraform configuration is applied. Output.tf files are useful for retrieving information about the provisioned infrastructure.

Our configuration is completed and let's deploy it.

Terraform commands

terraform init
terraform plan
terraform apply

Once these commands are run one by one we will get the following output at the end.

As we mentioned the public IP address to be exposed in the output file, the public ip address is displayed. Let's verify it in the AWS.

We can verify the public IP is the same i.e. 3.88.201.167. We have successfully created the resource and exposed the public IP.

Destroy the resources

terraform destroy

We had created the two ec2 instances using different approaches are now terminated using the terraform command.

Through this hands-on experience, we demonstrated the creation of an EC2 instance in the AWS cloud, incorporating variables and outputs for enhanced security and information retrieval.

Happy Learning!!