使用 terraform 连接运行 aws 实例

use*_*972 2 amazon-web-services terraform devops

resource "aws_instance" "appserver1" {
  ami             = var.imageid
  instance_type   = var.instancetype
  key_name        = var.key
  security_groups = [aws_security_group.allow_all.name]

  connection {
    user        = "ubuntu"
    private_key = file(var.privatekeypath)
  }

  provisioner "remote-exec" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install tomcat7 -y"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

“terraform validate”给了我错误:

错误:缺少必需的参数

在 main.tf 第 52 行,资源“aws_instance”“appserver1”中:52:连接{

参数“host”是必需的,但未找到定义。

Mar*_*cin 5

您必须在块中指定连接provisioner详细信息。例如:

resource "aws_instance" "appserver1" {

  ami             = var.imageid
  instance_type   = var.instancetype
  key_name        = var.key
  security_groups = [aws_security_group.allow_all.name]


  provisioner "remote-exec" {

    connection {
       type        = "ssh"
       user        = "ubuntu"
       private_key = file(var.privatekeypath)
       host        = self.public_ip
    }

    inline = [
      "sudo apt-get update",
      "sudo apt-get install tomcat7 -y"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

但就您而言,使用user_data会更适合。