# 📘  Terraform Series – Day 6

### 🎯 Objective

In this hands-on, we will:

*   Generate SSH key
    
*   Create key pair using Terraform
    
*   Configure VPC & Security Group
    
*   Launch EC2 instance
    
*   Connect via SSH
    
*   Clean up resources
    

👉 This is your **first real-world Terraform task**

### 🧩 Step 1: Generate SSH Key

```shell
ssh-keygen
```

✔ This creates:

*   `terra-key-aws`→ Private key
    
*   [`terra-key-ec2.pub`](http://terra-key-ec2.pub) → Public key
    

👉 We will use this to access EC2

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/54b6780c-ca45-4254-8db3-9424d6f991fd.png align="center")

### 📄 Step 2: Create Terraform File

```shell
touch ec2.tf
```

🧱 Step 3: Add Terraform Code

```shell
# Create Key Pair
resource "aws_key_pair" "my_key" {
  key_name   = "terra-key-aws"
  public_key = file("terra-key-aws.pub")
}


# Default VPC
resource "aws_default_vpc" "default" {}

# Security Group
resource "aws_security_group" "my_groups" {
  name        = "my-group"
  description = "Security group for EC2"
  vpc_id      = aws_default_vpc.default.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow SSH"
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
    description = "Allow HTTP"
  }

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

  tags = {
    Name = "automate-sg"
  }
}

# EC2 Instance
resource "aws_instance" "my_instance" {
  ami                    = "ami-0cb91c7de36eed2cb"
  instance_type          = "t2.micro"
  key_name               = aws_key_pair.my_key.key_name
  vpc_security_group_ids = [aws_security_group.my_groups.id]

  root_block_device {
    volume_size = 10
    volume_type = "gp3"
  }

  tags = {
    Name = "terra-ec2"
  }
}
```

### ⚙️ Step 4: Initialize Terraform

```shell
terraform init
```

✔ Downloads AWS provider

✔ Prepares working directory

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/c17141c1-2782-45b6-bfdc-062e48ac3ff8.png align="center")

### ✅ Step 5: Validate Configuration

```shell
terraform validate
```

✔ Ensures syntax is correct

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/76034d58-2af8-459e-8219-6435ba6f1611.png align="center")

### 📊 Step 6: Plan Execution

```shell
terraform plan
```

✔ Shows resources to be created:

*   Key Pair
    
*   VPC
    
*   Security Group
    
*   EC2 Instance
    

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/60ce0fec-7f70-4ae4-83bf-d5a7fb4977d2.png align="center")

### 🚀 Step 7: Apply (Create Infrastructure)

```shell
terraform apply
```

👉 Type `yes` to confirm

### ❌ Common Error: Not Authorized

👉 Reason:

*   IAM user does not have required permissions
    

✔ Fix:

Go to **AWS IAM → Attach Policy**

*   `AdministratorAccess` (easy way)
    
    OR
    
*   `EC2FullAccess`
    
*   `VPCFullAccess`
    

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/eefe5fda-8649-433a-840b-d4f56916dfe9.png align="center")

### 🖥 Step 8: Verify in AWS Console

Go to EC2 Dashboard:

✔ Instance running  
✔ Security group attached  
✔ Key pair created

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/e2d2d40a-1d81-4172-b533-06b6a6c02d16.png align="center")

### 🔐 Step 9: Fix Key Permission

```shell
chmod 400 terra-key-aws
```

👉 Required before SSH

### 🔗 Step 10: Connect to EC2

```shell
ssh -i terra-key-aws ubuntu@<your-public-ip>
```

👉 Now your server is live 🚀

### 🧹 Step 11: Destroy Resources (IMPORTANT)

```shell
terraform destroy
```

👉 Prevent unnecessary AWS charges 💸

![](https://cdn.hashnode.com/uploads/covers/685cdc0d5ca95e55fac3ab09/269447f8-0cf7-4332-a1f7-36eeb4b924ea.png align="center")

## **👨‍💻 About the Author**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751797710818/123a7231-3dca-4273-ad68-7bd026f69b95.png?auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp&auto=compress,format&format=webp align="center")

“A complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.”

### **📬 Let's Stay Connected**

*   📧 **Email**: [**gujjarapurv181@gmail.com**](mailto:gujjarapurv181@gmail.com)
    
*   🐙 **GitHub**: [**github.com/ApurvGujjar07**](http://github.com/ApurvGujjar07)
    
*   💼 **LinkedIn**: [**linkedin.com/in/apurv-gujjar**](http://linkedin.com/in/apurv-gujjar)
