Terraform level 1 - task 1, 'datacenter-kp' doesn't exist or or its type is not 'rsa'

resource “tls_private_key” “rsa_key” {
algorithm = “RSA”
rsa_bits = 4096
}

resource “aws_key_pair” “datacenter-kp” {
key_name = “datacenter-kp”
public_key = trimspace(tls_private_key.rsa_key.public_key_openssh)
tags = {
type = “rsa”
}
}

resource “local_file” “datacenter-kp_pem” {
content = “tls_private_key.rsa_key.private_key_pem”
filename = “/home/bob/datacenter-kp.pem”
file_permission = “0600”
}

output “key_type” {
value = “rsa”
}

I tried to create aws key pair & I mentioned the type RSA also but still I’m unable to clear this task

First, please use a code block for this kind of code – pasting it directly into the edit window as you did garbles your code. Here’s what I think you meant:

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

resource "aws_key_pair" "datacenter-kp" {
key_name ="datacenter-kp"
public_key = trimspace(tls_private_key.rsa_key.public_key_openssh)
tags = {
type ="rsa"
}
}

resource "local_file" "datacenter-kp_pem" {
content ="tls_private_key.rsa_key.private_key_pem"
filename ="/home/bob/datacenter-kp.pem"
file_permission ="0600"
}

Your code may lack spaces in some points of it that made it syntactically invalid. I can’t tell, again, because your code is garbled, and I had to add in spaces to make it parse correctly in terraform. The other possibility is that you’re not naming some of the items as the task expects.

Assuming I got your code correctly, then it does generate a PEM file that looks valid. But without knowing the exact wording of the task (this changes from each time you call up the task), I can’t be sure why the grader is unhappy.

With a correction, and having applied terraform fmt:

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

resource "aws_key_pair" "datacenter-kp" {
  key_name   = "datacenter-kp"
  public_key = trimspace(tls_private_key.rsa_key.public_key_openssh)
  tags = {
    type = "rsa"
  }
}

resource "local_file" "datacenter-kp_pem" {
  content         = tls_private_key.rsa_key.private_key_pem
  filename        = "/home/bob/datacenter-kp.pem"
  file_permission = "0600"
}

output "key_type" {
  value = "rsa"
}

This may well be your problem – this is wrong. You need to remove the quote marks of this one, since content needs to be set to a variable to work here.

1 Like