Photo by Mehmet Ali Peker on Unsplash
Streamline Your Cloud Infrastructure: How Terraform Makes EC2 Deployment a Breeze
Introduction:-
Deploying infrastructure in the cloud can be a Difficult task, especially when it comes to provisioning and managing virtual machines. However, with the help of infrastructure as code tools like Terraform, this process can be simplified and streamlined. In this blog post, we will go through the step-by-step process of deploying an EC2 instance using Terraform. We will provide a Terraform configuration file, along with input and output variables, to demonstrate how easy it is to create and manage infrastructure in AWS.
Pre-Requestites:-
Terraform should be installed
Aws account with proper s3 permissions
Aws account credentials should be configured on your machine.Step 1: Creating a Terraform Configuration File
The first step in deploying an EC2 instance using Terraform is to create a configuration file. This file specifies the resources to be created, along with their configuration options.
Here’s an example main.tf file that creates an EC2 instance:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "${var.instance_ami}"
instance_type = "${var.instance_type}"
tags = {
Name = "${var.instance_name}"
}
}
This configuration file specifies the aws_instance resource, which creates an EC2 instance in the us-west-2 region. The ami and instance _type options specify the Amazon Machine Image and instance type to use, respectively. The tags option adds a name tag to the instance for easy identification.
Step 2: Defining Input Variables
Next, we can define input variables in a variables.tf file. These variables can be used to customize the configuration file without modifying the code. Here’s an example variables.tf file:
variable "instance_ami" {
default = "ami-0c55b159cbfafe1f0"
}
variable "instance_type" {
default = "t2.micro"
}
variable "instance_name" {
default = "example-instance"
}
Step 3: Defining Output Variables
Finally, we can define output variables in an output.tf file to display the results of our Terraform configuration. Here’s an example output.tf file:
output "public_ip" {
value = aws_instance.example.public_ip
}
output "private_ip" {
value = aws_instance.example.private_ip
}
These output variables reference the aws_instance.example resource and display the public and private IP addresses of the created instance.
Step 4: Running the Terraform Configuration
With our configuration file, input variables, and output variables defined, we can now run the terraform configuration. Here are the basic steps:
Run terraform init to initialize the Terraform working directory.
Run terraform plan to see what changes will be made to the infrastructure.
Run terraform apply to apply the changes to the infrastructure.
During the apply step, Terraform will create the EC2 instance and display the output variables specified in the output.tf file.
Step 5: Verify the Ec2 instance
After Terraform applies the configuration successfully, you can verify that the Ec2 instance was created by going to the AWS Management Console and navigating to the Ec2 section. You should see a new Ec2 instance with the name that you specified in the configuration file.
Conclusion :-
In this blog post, we have gone through the step-by-step process of deploying an EC2 instance using Terraform.