Creating an EC2 Instance Using Terraform

Nivas DevSecOps
3 min readSep 2, 2024

Terraform is a powerful infrastructure as code (IaC) tool that allows you to provision and manage your cloud resources in a declarative manner. In this tutorial, we’ll walk you through the process of creating an EC2 instance on AWS using Terraform.

Prerequisites

  1. AWS Account: You’ll need an active AWS account with appropriate permissions to create EC2 instances.
  2. Terraform Installed: Make sure you have Terraform installed on your machine. Refer to the previous blog post on how to install Terraform on Windows 10.

3. Access Keys: You’ll need to create Access keys and Secret access keys to allow terraform to interact with AWS.

4. TextEditor: A text editor is crucial in Terraform for crafting and editing configuration files, enabling precise resource definitions. It provides syntax highlighting and error checking, enhancing code readability and reducing mistakes.

In my case I’m using Visual Studio Code.

Terraform & AWS CLI Install

WHAT IS A PROVIDER ?

In Terraform, a provider is a plugin that allows you to interact with different cloud providers, platforms, or services. It serves as the bridge between Terraform and the API of the platform you want to manage. Providers are what enable Terraform to create, modify, and delete resources in your chosen infrastructure.

What is a resource ?

In Terraform, a resource is a specific entity or component within a cloud provider or infrastructure service that you want to manage. It can represent a wide range of things, such as virtual machines, databases, networks, or even individual files. Resources are defined in your Terraform configuration files and correspond to real-world elements in your infrastructure.

Once installation completed Lets start to create ec2 -instance

  1. Provider.tf
provider "aws" {
region = "ap-south-1"
access_key = "" #pass your keys
secret_key = "" #pass your keys
}

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}

required_version = ">= 1.2.0"
}

2. ec2.tf

resource "aws_instance" "newec2" {
ami = "" #use your own AMI-id
instance_type = "t2.micro"
key_name = "" #use your key pair
user_data = file("${path.module}/index.sh")
tags = {
"Name" = "Ec2instance"
}

}

3. Index.sh

#! /bin/bash

sudo yum update -y
sudo yum install -y httpd
sudo systemctl enable httpd
sudo service httpd start
sudo echo '<h1>Welcome to CloudDevSecOps </h1>' | sudo tee /var/www/html/index.html

4. sg.tf

resource "aws_security_group" "security_group_react" {
name = "demo-sg"
description = "allow http port"

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

ingress {
description = "allow http"
from_port = 22
to_port = 22
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"]
}

tags = {
Name = "demo-sg"
}
}

5. Execute Terraform Commands

terraform init
terraform validate
terraform plan
terraform apply or terrafrom apply -auto-approve #this helps to automate the 'yes' value
terraform destroy or terraform destroy -auto-approve

Conclusion

Congratulations! You’ve successfully created an EC2 instance using Terraform. This example demonstrates the power and simplicity of using Terraform to manage your cloud resources in an automated and declarative manner.

--

--

Nivas DevSecOps
Nivas DevSecOps

Written by Nivas DevSecOps

Cloud | DevSecOps| AWS ⭐Passionate Cloud and DevOps . 🎯 Like to stay up-to-date with the latest trends and insights.

No responses yet