DEV Community

Cover image for 7.Create EC2 Instance Using Terraform
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

7.Create EC2 Instance Using Terraform

Lab Information

The Nautilus DevOps team is strategizing the migration of a portion of their infrastructure to the AWS cloud. Recognizing the scale of this undertaking, they have opted to approach the migration in incremental steps rather than as a single massive transition. To achieve this, they have segmented large tasks into smaller, more manageable units.

For this task, create an EC2 instance using Terraform with the following requirements:

The name of the instance must be xfusion-ec2.

Use the Amazon Linux ami-0c101f26f147fa7fd to launch this instance.

The Instance type must be t2.micro.

Create a new RSA key named xfusion-kp.

Attach the default (available by default) security group.

The Terraform working directory is /home/bob/terraform. Create the main.tf file (do not create a different .tf file) to provision the instance.

Note: Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
Enter fullscreen mode Exit fullscreen mode

Lab Solutions

Create main.tf

# main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_key_pair" "xfusion_kp" {
  key_name   = "xfusion-kp"
  public_key = tls_private_key.xfusion_rsa.public_key_openssh
}

resource "tls_private_key" "xfusion_rsa" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

resource "aws_instance" "xfusion_ec2" {
  ami           = "ami-0c101f26f147fa7fd"
  instance_type = "t2.micro"
  key_name      = aws_key_pair.xfusion_kp.key_name

  tags = {
    Name = "xfusion-ec2"
  }
}

resource "local_file" "private_key" {
  content  = tls_private_key.xfusion_rsa.private_key_pem
  filename = "${path.module}/xfusion-kp.pem"
}
Enter fullscreen mode Exit fullscreen mode

To deploy this configuration:

Navigate to the Terraform directory:

cd /home/bob/terraform
Enter fullscreen mode Exit fullscreen mode

Initialize Terraform:

terraform init
Enter fullscreen mode Exit fullscreen mode

Plan the deployment to verify the configuration:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Apply the configuration:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Explanation

This configuration:

    Provider Configuration: Sets up the AWS provider for the us-east-1 region.

    RSA Key Generation: Creates a new RSA key pair using the tls_private_key resource with 4096 bits.

    Key Pair Resource: Creates the AWS key pair named xfusion-kp using the generated public key.

    EC2 Instance: Creates the instance with:

        Name: xfusion-ec2

        AMI: ami-0c101f26f147fa7fd (Amazon Linux)

        Instance type: t2.micro

        Key pair: xfusion-kp

        Security group: Uses the default security group (automatically used when not specified)

    Local File: Saves the private key to a file named xfusion-kp.pem in the Terraform directory for SSH access.

Important Notes:

    The instance will use the default VPC and default security group

    The private key will be saved locally as xfusion-kp.pem - make sure to keep this secure!

    You'll need to set proper permissions on the private key file: chmod 400 xfusion-kp.pem
Enter fullscreen mode Exit fullscreen mode

Resources & Next Steps
πŸ“¦ Full Code Repository: KodeKloud Learning Labs
πŸ“– More Deep Dives: Whispering Cloud Insights - Read other technical articles
πŸ’¬ Join Discussion: DEV Community - Share your thoughts and questions
πŸ’Ό Let's Connect: LinkedIn - I'd love to connect with you

Credits
β€’ All labs are from: KodeKloud
β€’ I sincerely appreciate your provision of these valuable resources.

Top comments (0)