Terraform - Infrastructure as a Code Get started!

Pranav Bakare - Sep 18 - - Dev Community

Terraform is an open-source tool that allows you to define and provision infrastructure using a high-level configuration language (HCL - HashiCorp Configuration Language). It enables infrastructure as code (IaC) to manage both cloud and on-premises environments.

Here's a sample Terraform example for deploying an infrastructure on AWS that includes an EC2 instance and a security group.

Sample Example: Provisioning an EC2 Instance

  1. Install Terraform: Make sure Terraform is installed on your system. You can download it from Terraform's official site.

  2. Create Terraform Files: Terraform configurations are written in .tf files. For this example, we will create three files:

main.tf

variables.tf

outputs.tf


  1. main.tf (Main Configuration)

Specify the provider and region

provider "aws" {
region = "us-east-1" # Change to your desired AWS region
}

Create a Security Group to allow SSH and HTTP traffic

resource "aws_security_group" "allow_ssh_http" {
name = "allow_ssh_http"
description = "Allow SSH and HTTP inbound traffic"

# Allow incoming SSH
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

# Allow incoming HTTP
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

# Allow all outgoing traffic
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

Create an EC2 instance

resource "aws_instance" "my_ec2" {
ami = var.ami_id
instance_type = var.instance_type
security_groups = [aws_security_group.allow_ssh_http.name]

# Tagging the instance
tags = {
Name = "MyTerraformEC2"
}
}

  1. variables.tf (Input Variables)

Define the variables

variable "ami_id" {
description = "AMI ID for the EC2 instance"
type = string
default = "ami-0c55b159cbfafe1f0" # Example AMI ID, change based on region
}

variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}

  1. outputs.tf (Output Values)

Output the public IP of the EC2 instance

output "instance_public_ip" {
description = "Public IP of the EC2 instance"
value = aws_instance.my_ec2.public_ip
}

Output the instance ID

output "instance_id" {
description = "The ID of the EC2 instance"
value = aws_instance.my_ec2.id
}


Steps to Run:

  1. Initialize Terraform: Initialize your project and download the necessary provider plugins.

terraform init

  1. Plan the Deployment: See what changes will be made without making any changes.

terraform plan

  1. Apply the Changes: Deploy the infrastructure.

terraform apply

  1. Destroy the Infrastructure: When you're done, clean up by destroying the resources.

terraform destroy

Explanation:

provider "aws": Specifies the AWS provider and the region where the resources will be created.

resource "aws_security_group": Creates a security group allowing SSH (port 22) and HTTP (port 80) traffic.

resource "aws_instance": Provisions an EC2 instance with a specific Amazon Machine Image (AMI) and instance type.

variables.tf: Defines input variables to make the configuration dynamic.

outputs.tf: Outputs useful information (like public IP and instance ID) after the infrastructure is created.

You can customize this configuration by changing variables or adding more resources like RDS instances, VPC, or load balancers as needed.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player