How to create an EC2 instance using Terraform configuration files
Now that we have the networking infrastructure and security groups set up, let’s define the EC2 terraform instance itself. Here’s an example:
resource "aws_instance" "my_instance" {
ami = var.ec2_ami
instance_type = var.ec2_instance_type
subnet_id = aws_subnet.my_subnet.id
vpc_security_group_ids = [aws_security_group.my_sg.id
tags = {
Name = "my-ec2-instance"
}
}
In the above code, we define an AWS EC2 terraform instance resource with the specified AMI (Amazon Machine Image), instance type, subnet, and security group. Also, we assigned a tag to the instance for identification.
Terraform block it was describe the cloud required version and the required credentials. The following example shows that aws provider:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "us-west-2"
profile = "jack.roper"
}
resource "aws_instance" "example_server" {
ami = "ami-04e914639d0cca79a"
instance_type = "t2.micro"
tags = {
Name = "JacksBlogExample"
}
}
VPC (Virtual Private Cloud) is isolated section in aws cloud where your resources can be launched. We can create VPC by using terraform. For example to creating a VPC and Subnet, you would define a resource block like this:
resource "aws_vpc" "sada" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "siva"
}
}
In this example, we specify the vpc with cidr block, subnet ID, and tags for the EC2 instance. Terraform will use this configuration to create the specified EC2 instance when you apply the configuration.
# provider block defines the cloud provider and its configuration
provider "aws" {
region = "us-east-1"
}
# variable block allows you to define variables for reusability
variable "instance_type" {
description = "Type of EC2 instance"
default = "t2.micro"
}
variable "ami" {
description = "Amazon Machine Image ID"
default = "ami-12345678"
}
# resource block defines the AWS resources to be created
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
# other VPC configurations...
}
resource "aws_security_group" "my_security_group" {
vpc_id = aws_vpc.my_vpc.id
# other security group configurations...
}
resource "aws_instance" "my_instance" {
ami = var.ami
instance_type = var.instance_type
subnet_id = aws_subnet.my_subnet.id
security_group = [aws_security_group.my_security_group.id]
# other instance configurations...
}
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
# other subnet configurations...
}
# output block allows you to define values to be displayed after apply
output "instance_ip" {
value = aws_instance.my_instance.public_ip
}
Providers Block:
- Providers block is utilized to design the Providers, which is liable for overseeing assets in a particular cloud environment (e.g., AWS, Azure, and so on.).
- It remembers subtleties, for example, the district for which assets ought to be made.
Variable Block:
- Input variables that can be used throughout your Terraform configuration can be defined using the variable block.
- Helps in defining your design and making it more adaptable.
- You can set default values, depictions, and different qualities for factors.
Resource Block:
- Resource block is the most key structure block in Terraform.
- It proclaims an asset type (e.g., aws_instance, aws_vpc) and its particular setups.
- Every asset block makes an occurrence of the asset it depicts.
Output Block:
- Output block characterizes values that will be displayed subsequent to applying the Terraform arrangement.
- Valuable for showing data like IP addresses, DNS names, and so on.
- Gives a method for uncovering explicit data for outside use
Infrastructure as Code (IaC) is a method that permits you to oversee and arrangement foundation assets utilizing code instead of manual cycles. Terraform, being an IaC device, empowers you to characterize your framework in a definitive language and keep up with it as rendition controlled code. Let’s learn how to set up an AWS EC2 terraform instance and write infrastructure as code using Terraform.
Configuring Security Groups, Security is most important due to it acts as a virtual firewalls, its controlling inbound and outbound traffic for your AWS EC2 terraform instance.
resource "aws_security_group" "my_sg" {
name = "my-security-group"
description = "Allow inbound SSH and HTTP traffic"
inbound {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
inbound {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
outbound {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
Initialize the Terraform directory
Now it’s time to Initializing and applying the terraform configuration after writing the terraform code code by using following code:
terraform init
When using Terraform, the terraform init command is a crucial step. It initializing a working directory, setting up the fundamental parts for Terraform to collaborate with the specified backend (like AWS, Azure , or local), and download any expected supplier modules.
Run terraform plan
and apply
To apply the terraform configuration and create the EC2 terraform instance, to run the following command:
terraform apply
Cleaning up and destroying AWS resources is a fundamental stage to prevent unnecessary costs and keep a clean infrastructure. This is the way you can tidy up the assets made by your AWS EC2 terraform example.
How to Create AWS EC2 using Terraform?
AWS EC2 (Elastic Compute Cloud) is a web service provided by Amazon Web Services (AWS) that allows users to launch and oversee virtual servers, known as examples, in the cloud. It gives an adaptable and versatile foundation for running different sorts of applications and jobs. With Terraform EC2, clients have unlimited authority over their virtual servers, including the capacity to pick the sort, operating system, and storage options of each instance.
Terraform is an open-source framework and code instrument created by HashiCorp. It empowers clients to characterize and arrange framework assets using a definitive language. Terraform upholds numerous cloud suppliers, including AWS, Azure, and Google Cloud Platform, allowing users to deal with their infrastructure reliably across various stages.