terraform V12:错误导入 KeyPair:MissingParameter:请求必须包含参数 PublicKeyMaterial

terraform V12: Error import KeyPair: MissingParameter: The request must contain the parameter PublicKeyMaterial

当我运行“terraform apply”时出现错误“import KeyPair: MissingParameter: The request must contain the parameter PublicKeyMaterial”。这个错误是什么意思。

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"
    ]
   }
}

假设其他一切都正确,connection 块应该在 provisioner 之内,而不是在它之外:

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",
     ]
   }
}