DEV Community

Kishore Suzil
Kishore Suzil

Posted on

AWS EC2 Instance Management with Terraform

Introduction :

Managing infrastructure as code is essential for modern DevOps practices, and Terraform by HashiCorp is a powerful tool for this purpose. In this blog, we will walk through the steps to install Terraform using Chocolatey, and then use Terraform to create, rename, and delete an
AWS EC2 instance. We will also cover using variables and outputs in Terraform.

Table of Contents

  1. [Installing Terraform with Chocolatey]
  2. [Setting Up Terraform Configuration]
  3. [Creating an EC2 Instance]
  4. [Renaming an EC2 Instance]
  5. [Deleting an EC2 Instance]
  6. [Using Variables in Terraform]
  7. [Using Outputs in Terraform]
  8. [Conclusion]

Installing Terraform with Chocolatey

Chocolatey is a package manager for Windows that simplifies the installation of software. Follow these steps to install Terraform using Chocolatey:
1. Install Chocolatey:
Open an administrative PowerShell prompt and run the following command:
‘Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.ServicePointManager]::SecurityProtocol =[System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object
System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))’

2. Install Terraform
Once Chocolatey is installed, run: powershell choco install terraform

  1. Verify the Installation: Confirm that Terraform is installed by running: powershell “terraform –version” Creating an EC2 Instance Add the following code to main.tf to define an AWS provider and create an EC2 instance:

provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI
instance_type = "t2.micro"
tags = {
Name = "ExampleInstance"
}
}
Initialize Terraform and apply the configuration:
terraform init
terraform apply
Type yes to confirm the apply action. This will create an EC2 instance.

Renaming an EC2 Instance
Renaming an EC2 instance involves changing the Name tag. Modify the tags block in

your main.tf:
tags = {
Name = "RenamedInstance"
}
_Apply the changes:_
_terraform apply_
Confirm the changes by typing
yes`.

Deleting an EC2 Instance
To delete the EC2 instance, use the destroy command:
terraform destroy
Confirm the destruction by typing yes.
Using Variables in Terraform
Variables allow you to make your Terraform configuration more flexible. Create a variables.tf file:

variable "instance_name" {
description = "Name of the EC2 instance"
type = string
default = "MyInstance"
}
variable "instance_type" {
description = "Type of the EC2 instance"
type = string
default = "t2.micro"
}
Modify main.tf to use these variables:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
tags = {
Name = var.instance_name
}
}

Using Outputs in Terraform
Outputs allow you to extract information from your Terraform state. Add the following to

main.tf:
output "instance_id" {
value = aws_instance.example.id
}
output "instance_public_ip" {
value = aws_instance.example.public_ip
}

Apply the configuration:
terraform apply
After the apply is complete, you will see the instance ID and public IP in the output.

Conclusion

In this blog post, we covered how to install Terraform using Chocolatey, and how to use Terraform to create, rename, and delete an EC2 instance. We also explored how to use variables and outputs in Terraform to make your configurations more flexible and extract useful information.By following these steps, you can automate the management of your AWS
infrastructure, making your operations more efficient and reliable. Happy Terraforming.This blog provides a comprehensive guide to getting started with Terraform for managing AWS EC2 instances, with practical steps and code examples

Top comments (0)