# Launching an EC2 instance using Terraform

Terraform is a powerful tool for automating the creation and management of cloud infrastructure. In this guide, we'll walk through the process of launching an EC2 instance on AWS using Terraform, including setting up necessary IAM roles and configuring resources.

**<mark>Step 1: Create an IAM Role with Required Permissions</mark>**

Navigate to the IAM Dashboard &gt; Click on **IAM &gt;** Click **Create role**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723720780469/8c82d7b3-0302-4212-8baf-e163cf35da15.png align="center")

Choose **AWS service**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723720828584/5effd67f-3e04-4d5a-a73a-7606a4d0672d.png align="center")

Select **EC2** as the service that will use this role &gt; Click **Next**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723720998544/69b05ff0-1395-4577-81cf-9989f53dbe5a.png align="center")

In the **Attach permissions policies** section, search for vpc &gt; Select `AmazonVPCFullAccess`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723721142656/5313dfcf-5dac-428e-bee1-b0f4345b46ba.png align="center")

Search for ec2 &gt; Select `AmazonEC2FullAccess`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723721191224/036fa26a-f23b-4f0b-b206-95dc7cdebcf9.png align="center")

Click **Next**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723721234108/e26a26db-dd1d-4635-87a0-3b4a4cdd08c6.png align="center")

Provide a **Role name &gt;** `Terraform-ec2-vpc-role`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723721321937/f321c957-9fbd-4d21-a3ea-2a80ce19a2e8.png align="center")

Click **Create role**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723721346183/11647cde-685d-47b6-ab90-ce3c67bd93a2.png align="center")

The IAM role is now created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723721558002/8ff2d0d0-2cb2-49f6-ac0b-55371624550e.png align="center")

Select the EC2 instance to which you want to attach the IAM role &gt; click on the **Actions** button &gt; Under the **Security** section, select **Modify IAM role**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723722922778/1789492c-4e56-4123-98a8-53a58fd237b9.png align="center")

In the **Modify IAM role** dialog, select the IAM role you created from the drop-down menu. &gt; Click on **Update IAM role** to attach the role to the instance

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723722026080/e061c6ce-be87-460b-a076-3d6806b971c2.png align="center")

**<mark>Step 2: Set Up Your Terraform Project</mark>**

Create a directory for your Terraform project

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723752062536/704b0f86-21ad-4d5b-9ca2-e79e21b42f1d.png align="center")

Inside this directory, create a file named [`main.tf`](http://main.tf). This file will contain the configuration for your AWS EC2 instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723752201093/7bf5a390-3d54-4513-83ce-5534fcf0d6db.png align="center")

Copy the blow content and paste it in to the `main.tf` file

```plaintext
vi main.tf
```

```plaintext
# Configure the AWS Provider
provider "aws" {
  region = "ap-south-1"  # Choose your desired region
}

# Create a VPC
resource "aws_vpc" "my_vpc" {
  cidr_block = "10.0.0.0/16"
}

# Create a Subnet
resource "aws_subnet" "my_subnet" {
  vpc_id     = aws_vpc.my_vpc.id
  cidr_block = "10.0.1.0/24"
}

# Create an Internet Gateway
resource "aws_internet_gateway" "my_igw" {
  vpc_id = aws_vpc.my_vpc.id
}

# Create a Route Table
resource "aws_route_table" "my_route_table" {
  vpc_id = aws_vpc.my_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.my_igw.id
  }
}

# Associate the Route Table with the Subnet
resource "aws_route_table_association" "my_route_table_assoc" {
  subnet_id      = aws_subnet.my_subnet.id
  route_table_id = aws_route_table.my_route_table.id
}

# Create a Security Group
resource "aws_security_group" "my_sg" {
  vpc_id = aws_vpc.my_vpc.id

  ingress {
    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"]
  }
}

# Create an EC2 Instance
resource "aws_instance" "Ansible" {
  ami           = "ami-0c2af51e265bd5e0e" # Replace with a valid AMI ID for ap-south-1
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.my_subnet.id
  security_groups = [aws_security_group.my_sg.id]

  tags = {
    Name = "Ansible VM"
  }

  # Optional: Add an SSH key pair for access
  key_name = "key"  # Replace with your actual key pair name
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723751846999/d012eabd-84a8-4c0d-a194-80ab8a116634.png align="center")

**<mark>Step 3: Initialize and Apply Terraform Configuration</mark>**

Before applying your configuration, need to initialize the Terraform project:

Run the following command to initialize your Terraform project:

```plaintext
terraform init
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723752551184/4ac04244-0c8b-4a47-83ed-b024a3727afe.png align="center")

**Preview Infrastructure Changes:**

Use the `terraform plan` command to preview the changes Terraform will make:

```plaintext
terraform plan
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723752880431/e09fa625-b599-460f-9f82-637e3e77d8f7.png align="center")

Apply the configuration to create the resources, run the following command:

```plaintext
terraform apply
```

Terraform will show you a plan of the resources it will create.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753453728/d15c947a-fcb3-44ef-a825-aa30e1abc264.png align="center")

If everything looks good, type `yes` to proceed

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753021635/514cff26-5194-4834-979d-870db3963137.png align="center")

Terraform will then provision the resources

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753252949/6332830a-72f7-48a3-8a9b-505a2fd668bf.png align="center")

**<mark>Step 4: Verify Resource Creation</mark>**

After the apply process is complete, verify the new EC2 instance and VPC in the AWS Management Console.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753597982/b3b24b65-c11a-4031-9e5a-b76586c383a6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753853157/2b365229-842c-49ca-97ad-dd141ee7317b.png align="center")

**<mark>Step 5: Clean Up Resources</mark>**

To remove the resources created by Terraform, use the `terraform destroy` command:

```plaintext
terraform destroy 
```

All the resources created by Terraform are now deleted.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723756432307/b6487973-a382-4827-9870-9067392f832b.png align="center")
