3

getting error "import KeyPair: MissingParameter: The request must contain the parameter PublicKeyMaterial " when I run "terraform apply". what does this error mean.

resource "aws_instance" "ec2_test_instance" {
  ami           = var.instance_test_ami
  instance_type = var.instance_type
  subnet_id     = var.aws_subnet_id
  key_name      = aws_key_pair.deployer.key_name

  tags = {
    Name = var.environment_tag
    }
   provisioner "local-exec" {
    command = "echo ${self.public_ip} > public-ip.txt"
    }
   provisioner "remote-exec" {

     connection {
        type    = "ssh"
        host    = self.public_ip
        user    = "centos"
        private_key   = file("${path.module}/my-key")
        }

    inline = [

        "sudo yum -y install wget, unzip",
        "sudo yum -y install java-1.8.0-openjdk"
    ]
   }
}
Anu
  • 101
  • 2
  • 8

1 Answers1

0

Assuming that everything else is correct, connection block should be inside provisioner, not outside of it:

resource "aws_instance" "ec2_test_instance" {

  ami           = var.instance_test_ami
  instance_type = var.instance_type
  subnet_id     = var.aws_subnet_id
  key_name      = aws_key_pair.deployer.key_name  


  provisioner "remote-exec" {

    connection {
      type    = "ssh"
      host    = self.public_ip
      user    = "centos"
      private_key   = file("${path.module}/my-key")
    }

    inline = [
        "sudo yum -y install wget, unzip",
        "sudo yum -y install java-1.8.0-openjdk",
     ]
   }
}

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • 1
    I moved the connection inside the provisioner per your suggestion. I found that there was another "tf script" that is importing the "key" as well. it was user error. works as expected. Thanks – Anu Sep 21 '20 at 15:02