In this article we are going to learn the steps to Launch EC2 Instance through Terraform in Windows.

What is Terraform?


Terraform is an open-source infrastructure as code (IaC) tool used for provisioning and managing cloud resources. It allows you to define your infrastructure in code, automating the creation, modification, and deletion of servers, databases, and other resources across various cloud providers. Terraform’s declarative approach ensures consistency and scalability, making it a popular choice for DevOps and cloud automation.

Prerequisites

  • Ensure you have Terraform installed.
  • Ensure you have an AWS account and have set up AWS CLI with the necessary credentials or have your AWS credentials ready.

Steps to Launch EC2 Instance through Terraform

Note: For an Example I am taking “eu-north-1” Region to do the task.

Step #1: Initialize a New Terraform Configuration

Step #2: Create a new directory for your Terraform configuration

mkdir tf-ec2-eu-north-1 && cd tf-ec2-eu-north-1

Step #3: Create a new file named ‘EC2.tf ‘ in this directory

Step #4: Terraform Configuration

Edit EC2.tf and add the following configuration:

provider "aws" {
  region  = "eu-north-1"
  access_key = "YOUR_ACCESS_KEY"
  secret_key = "YOUR_SECRET_KEY"
}

resource "aws_instance" "my_ec2" {
  ami           = "AMI_ID" # Replace with your desired AMI ID, e.g., for Amazon Linux 2
  instance_type = "t3.micro"

  key_name = "YOUR_KEY_PAIR_NAME" # Replace with your key pair name if you want SSH access

}

Replace the placeholders:

  • YOUR_ACCESS_KEY and YOUR_SECRET_KEY with your AWS credentials. For better security, consider using AWS CLI configuration or environment variables.
  • AMI_ID with the specific AMI ID you want to use (e.g., Amazon Linux 2’s AMI ID for eu-north-1).
  • YOUR_KEY_PAIR_NAME with the name of your EC2 key pair if you want SSH access.
  1. Initialize and Apply Configuration:
  2. Initialize the Terraform directory:
terraform init
  • Apply the Terraform configuration:
terraform apply
  • You’ll be prompted to confirm that you want to create the resources defined in your configuration. Type yes and press enter.
  • After these steps, Terraform will create a t3.micro EC2 instance in the eu-north-1 region. Ensure you manage and monitor your resources to avoid any unintended costs, and always shut down or terminate instances that aren’t in use.

Security Note: Storing AWS credentials directly in Terraform files isn’t recommended due to security concerns. Consider using the AWS CLI configuration, environment variables, or IAM roles when working in a real-world or production scenario.

Conclusion

Launching an EC2 instance using Terraform on a Windows environment offers a powerful and efficient way to automate the provisioning of cloud resources. With Terraform, you can define your infrastructure as code, enabling easy replication, modification, and scaling of your EC2 instances and associated resources. This approach streamlines the deployment process, enhances reproducibility, and integrates well with Windows-based DevOps workflows, making it a valuable tool for managing cloud infrastructure.

Reference:

Please visit the official website of Terraform

Any queries pls contact

@Rushi-InfoTech

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *