DEV Community

Cover image for How To Install,Create,Modify,Destroy EC2 Instance In Teeraform
Jeyaprakash Prakash
Jeyaprakash Prakash

Posted on

How To Install,Create,Modify,Destroy EC2 Instance In Teeraform

Installing Terraform

First things first, let's get Terraform installed on your machine:

  1. Download Terraform: Visit the Terraform downloads page and download the appropriate package for your operating system.

  2. Install Terraform: After downloading, follow the installation instructions for your OS. For most systems, this involves placing the Terraform binary in your PATH.

  3. Verify Installation: Open a terminal or command prompt and type terraform --version to ensure Terraform is installed correctly.

Building Infrastructure

Now that Terraform is installed, let's create your first infrastructure:

  1. Initialize a Working Directory: Create a new directory and navigate into it. Run terraform init to initialize Terraform in this directory. This step downloads any necessary plugins for providers defined in your configuration.

  2. Write Infrastructure Code: Create a .tf file (e.g., main.tf) and define your infrastructure resources using Terraform's declarative language. For example, to create an AWS EC2 instance:

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

resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}

  1. Apply Configuration: Run terraform apply to apply your configuration. Terraform will plan the changes and prompt you to confirm before making any infrastructure modifications.

Managing Variables
Variables in Terraform allow you to parameterize your configurations:

  1. Declare Variables: Define variables in a .tf file or use a variables.tf file:

hcl
variable "instance_type" {
description = "Type of instance to create"
default = "t2.micro"
}

  1. Use Variables: Reference variables in your configuration files using ${var.variable_name} syntax:

hcl
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}

Outputs

Outputs in Terraform allow you to extract and display information about your infrastructure:

  1. Define Outputs: Declare outputs in your configuration:

hcl
output "instance_ip" {
value = aws_instance.example.public_ip
}

  1. Display Outputs: After applying your configuration, view outputs with terraform output:

makefile
instance_ip = 203.0.113.10

Changing and Destroying Infrastructure

Updating and destroying infrastructure in Terraform is straightforward:

  1. Modify Configuration: Make changes to your .tf files (e.g., update instance type).

  2. Apply Changes: Run terraform apply again to apply the changes.

  3. Destroy Infrastructure: When you're finished, run terraform destroy to tear down all resources created by your configuration.

Conclusion

Congratulations! You've learned the basics of installing, building, modifying, and destroying infrastructure using Terraform. Remember to version control your configurations and always review Terraform's execution plan before applying changes to production environments. With these skills, you're ready to automate and manage your infrastructure efficiently. Happy Terraforming!

Top comments (0)