Terraform first day task

Hello everyone,

i have a question regarding Terraform first day task.
I have to create a rsa private key.
Below you can see my main.tf input.

resource “tls_private_key” “devops_kp” {
algorithm = “RSA”
rsa_bits = 2048
}

resource “local_file” “private_key” {
content = tls_private_key.devops_kp.private_key_pem
filename = “/home/bob/devops-kp.pem”
}

output “key_name” {
value = “devops-kp”
}

Actually, you’re only doing half of the task here – you need to create an AWS keypair. So you need to pass the public key to AWS to do this. The full main.tf file should look something like this (note that I use a code block, as should you for these kinds of questions, so the code is not corrupted by the Discourse editor):

# Generate RSA private key locally
resource "tls_private_key" "devops_kp" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

# Create AWS key pair using the public key
resource "aws_key_pair" "devops_kp" {
  key_name   = "devops-kp"
  public_key = tls_private_key.devops_kp.public_key_openssh
}

# Save private key locally
resource "local_file" "private_key_pem" {
  content         = tls_private_key.devops_kp.private_key_pem
  filename        = "/home/bob/devops_kp.pem"
  file_permission = "0600"
}