Skip to main content

Lab 08: Create EC2 Instance Using Terraform

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:

  1. The name of the instance must be nautilus-ec2.

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

  3. The Instance type must be t2.micro.

  4. Create a new RSA key named nautilus-kp.

  5. 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.

Create main.tf

resource "tls_private_key" "nautilus_kp" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "nautilus_kp" {
key_name = "nautilus-kp"
public_key = tls_private_key.nautilus_kp.public_key_openssh
}
data "aws_security_group" "default" {
name = "default"
}
resource "aws_instance" "nautilus_ec2" {
ami = "ami-0c101f26f147fa7fd"
instance_type = "t2.micro"
key_name = aws_key_pair.nautilus_kp.key_name
vpc_security_group_ids = [data.aws_security_group.default.id]
tags = {
Name = "nautilus-ec2"
}
}
terraform init
terraform plan -out kke.plan && terraform apply kke.plan
# or apply forcefully without creating plan and applying it
terraform apply -auto-approve