Terraform Level 1 - Lab 7. Resources have not been created using 'terraform'

Good evening,

I’m trying to complete this lab but it fails with “Resources have not been created using ‘terraform’.”

main.tf

# EC2 Instance
resource "aws_instance" "devops-ec2" {
  ami             = "ami-0c101f26f147fa7fd"
  instance_type   = "t2.micro"
  key_name        = "devops-kp"
  security_groups = [aws_default_security_group.default.id]

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

# Default SC
resource "aws_default_security_group" "default" {
  vpc_id = aws_aws_default_vpc.default.id
}

# Default VPC
resource "aws_default_vpc" "default" {
  tags = {
    Name = "Default VPC"
  }
}

# EC2 key-pair
resource "aws_key_pair" "devops-kp" {
  key_name   = "devops-kp"
  public_key = tls_private_key.key.public_key_openssh
}

# TLS provider. TLS key
resource "tls_private_key" "key" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

The following Terraform commands were used:

  • terraform init
  • terraform fmt
  • terraform validate
  • terraform plan
  • terraform apply --auto-approve

No error shows in the terminal and I can verify that the instance exists with aws ec2 describe-instances.

What am I doing wrong?

Not sure. I had thought that you were handling the SG incorrectly, but even using the data operator, I get exactly your error. I’ll see if I can get the KKE team to tell me just why “Resources have not been created using ‘terraform’ is emitted.

1 Like

@cralonso

It’s working from my end with the below terraform, please check it and try again.

# Create a new RSA key pair
resource "tls_private_key" "devops_kp" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

resource "aws_key_pair" "devops_kp" {
  key_name   = "devops-kp"
  public_key = tls_private_key.devops_kp.public_key_openssh
}

# Save the private key locally
resource "local_file" "devops_private_key" {
  content  = tls_private_key.devops_kp.private_key_pem
  filename = "${path.module}/devops-kp.pem"
  file_permission = "0400"
}

# Create an EC2 instance
resource "aws_instance" "devops_ec2" {
  ami           = "ami-0c101f26f147fa7fd"
  instance_type = "t2.micro"
  key_name      = aws_key_pair.devops_kp.key_name

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