Launch an EC2 instance using Terraform.

Launch an EC2 instance using Terraform.

Launching an EC2 instance using Terraform involves several steps, including setting up your environment, creating a Terraform configuration file, and applying the configuration to provision the EC2 instance.

Below is a step-by-step guide to help you through this process.

Prerequisites

  1. Install Terraform: Ensure Terraform is installed on your local machine. You can download it from the Terraform website.

  2. AWS Account: You need an AWS account to create and manage EC2 instances.

  3. AWS CLI: Install the AWS CLI and configure it with your AWS credentials by running aws configure. You will need your Access Key ID, Secret Access Key, default region, and output format.

  4. IAM Permissions: Ensure your AWS user has permissions to create EC2 instances.

Step 1: Create a Directory for Your Terraform Project

Open your terminal and create a new directory for your Terraform project:

mkdir my-ec2-instance
cd my-ec2-instance

Step 2: Create a Terraform Configuration File

Create a file named main.tf in your project directory. This file will contain the Terraform configuration to launch an EC2 instance. Here’s an example configuration:

# Specify the provider
provider "aws" {
  region = "us-east-1" # Change this to your preferred region
}

# Create a key pair for SSH access
resource "aws_key_pair" "my_key" {
  key_name   = "my-key" # Change this to your desired key name
  public_key = file("~/.ssh/id_rsa.pub") # Path to your public SSH key
}

# Define the security group
resource "aws_security_group" "allow_ssh" {
  name        = "allow_ssh"
  description = "Allow SSH inbound traffic"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"] # Change this to a specific IP for better security
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# Launch the EC2 instance
resource "aws_instance" "my_ec2_instance" {
  ami           = "ami-0c55b159cbfafe1f0" # Change this to your desired AMI ID
  instance_type = "t2.micro" # Change this to your desired instance type
  key_name      = aws_key_pair.my_key.key_name
  security_groups = [aws_security_group.allow_ssh.name]

  tags = {
    Name = "MyEC2Instance"
  }
}

Step 3: Initialize Terraform

In your terminal, navigate to your project directory and run the following command to initialize Terraform:

terraform init

This command will download the necessary provider plugins.

Step 4: Review the Configuration

You can run the following command to see the execution plan and verify the resources that Terraform will create:

terraform plan

Step 5: Apply the Configuration

Once you’re satisfied with the plan, apply the configuration to launch the EC2 instance:

terraform apply

You will be prompted to confirm the action. Type yes and hit Enter.

Step 6: Access Your EC2 Instance

After the instance is created, you can access it using SSH. Use the following command, replacing my-key and your-ec2-public-ip with your key name and the public IP address of the EC2 instance:

ssh -i ~/.ssh/my-key.pem ec2-user@your-ec2-public-ip

Step 7: Clean Up Resources

To avoid incurring charges for the resources created, you should terminate the instance and remove the resources:

terraform destroy

Again, you will be prompted to confirm the destruction. Type yes to proceed.

Conclusion

Using Terraform to launch an EC2 instance simplifies the infrastructure provisioning process, allowing you to manage your cloud resources as code. This approach not only enhances reproducibility and version control but also streamlines collaboration among teams.