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.
Let’s create the main.tf file copy paste the contents from this terraform file. In our main.tf file, we have used some resource block to generate ssh key, create key pair in aws and launch ec2 instance.
tls_private_key resource block to generate an RSA private keyaws_key_pair resource block to create key pair at aws cloud.aws_instance to launch the ec2 instanceFor this main.tf file, we need some variables. Let’s create variables.tf and add following variables:
variable "prefix" {
default = "xfusion"
}
variable "ami_id" {
default = "ami-0c101f26f147fa7fd"
}
variable "instance_type" {
default = "t2.micro"
}
Make sure you have updated
prefixdefault value according to your task.
This is optional. You can create a outputs.tf file to display the ec2 instance information.
output "ec2_info" {
value = {
public_ip = aws_instance.ec2.public_ip
private_ip = aws_instance.ec2.private_ip
}
}
Let’s run the terraform commands:
terraform init
terraform plan
terraform apply -auto-approve
tls_private_key resource generates the private key locally, while aws_key_pair uploads the public key to AWS.tfstate file tracks resource mappings - keep it secure and backed up