Navrangpura, Ahmedabad, Gujarat
Follow us :

What is Terraform

HashiCorp’s Terraform is an open-source infrastructure as code (IaC) tool. It enables you to use a declarative configuration language to define and manage your cloud infrastructure resources and services. You may use Terraform to automatically supply and manage a variety of infrastructure parts, including virtual machines, networks, storage, and more, across numerous cloud providers or on-premises environments.

1 Declarative Configuration: You identify the resources you require, their configurations, and relationships in a declarative configuration file that is often written in the HashiCorp Configuration Language, or HCL.

2 Provider Support: Numerous cloud service providers (such as AWS, Azure, Google Cloud, etc.) and other infrastructure elements (such as Docker, Kubernetes, etc.) are supported by Terraform. Terraform can be used to manage the resources and configurations that each supplier has to provide.

3 Versioning and Collaboration: Versioning and storing Terraform configurations in version control platforms like Git allows for team collaboration and preserves an audit trail of modifications.

4 Idempotency: Terraform operates under the idempotency principle, allowing you to apply the same configuration repeatedly without experiencing unintended consequences. To get the infrastructure to the desired state, Terraform will only perform the required adjustments.

5 Plan and Apply: When you modify your configuration file, Terraform can provide an execution plan that outlines the changes that will be performed to your infrastructure. After reviewing the strategy, you implemented it to bring about the desired improvements.

6 State Management: Your infrastructure’s current state is recorded by Terraform in a state file. This file aids Terraform in comprehending the configurations and resources that are currently deployed. It is crucial for updating and maintaining your infrastructure.

Compared to manual intervention, Terraform substantially simplifies the provisioning and management of infrastructure. It makes it possible to use infrastructure as code techniques, which facilitate the replication of environments, the management of modifications, and the maintenance of consistency throughout various stages of development and deployment.

Create ec2 instance

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.8.0"
}
}
}

provider "aws" {
region = "ap-south-1"
access_key = "Access_Key"
secret_key = "Secret_Key"

}

resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"

tags = {
name = "MyVPC"
}

}

resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "ap-south-1a"

tags = {
Name = "public1"
}
}

resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.2.0/24"

availability_zone = "ap-south-1a"

tags = {
Name = "private1"
}

}

resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my_vpc.id

}


resource "aws_route_table" "PublicRT" {
vpc_id = aws_vpc.my_vpc.id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
}

resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.PublicRT.id
}

resource "aws_security_group" "my_nsg" {
name = "my_nsg"
description = "Allow all inbound traffic"
vpc_id = aws_vpc.my_vpc.id

ingress {
description = "ssh from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]

}

ingress {
description = "http"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [ "0.0.0.0/0" ]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]

}
}

resource "aws_instance" "instance" {
ami = "ami-072ec8f4ea4a6f2cf"
vpc_security_group_ids = [aws_security_group.my_nsg.id]
instance_type = "t2.micro"
subnet_id = aws_subnet.public_subnet.id
key_name = aws_key_pair.sky_key.key_name
tags = {
Name = "highsky instace"
}

}


resource "aws_key_pair" "sky_key" {
  key_name = "sky_key"
  public_key = tls_private_key.rsa.public_key_openssh
}


resource "local_file" "tf_key" {
  content = tls_private_key.rsa.private_key_pem
  filename = "tfkey.pem"
}

resource "tls_private_key" "rsa" {
  algorithm = "RSA"
  rsa_bits = 4096

}



output "publicip" {
  value = aws_instance.instance.public_ip
}

1 The HashiCorp Terraform infrastructure as code (IaC) tool uses the terraform init command to initialize new or existing Terraform configurations in directories. Terraform creates the environment, downloads provider plugins, and gets the directory ready for controlling your infrastructure when you run Terraform init.

terraform init

2 In HashiCorp Terraform, the terraform plan command generates an execution plan outlining the modifications Terraform will make to your infrastructure based on your existing configuration. Without actually making the changes, it demonstrates to you what steps Terraform will take to create, update, or remove resources, for example. By doing so, you may examine and confirm the modifications before implementing them in your infrastructure.

terraform plan

3 In HashiCorp Terraform, the changes specified in your configuration are applied to your infrastructure using the Terraform apply command. This command executes the operations required to create, update, or delete resources in accordance with your settings using the execution plan produced by Terraform plan.

terraform apply

yes

 Output On AWS Infra

Author

by admin

bg

Subscribe to our Newslatter

Sign Up to Our Newsletter to Get Latest Updates & Services

mail box
logo-footer
HighSky IT Solutions Pvt. Ltd. is a leading IT training and certification programs in Red hat, RSCSA Cloud, Open-source, AWS, Devops and various domains.
Contact Info

2nd floor, Rohera Arcade, opp SOTC office, Near Navrangpura police station, Navrangpura, Ahmedabad -380009 Gujarat

Copyright ©2024 Design & Developed by HighSky IT