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

"terraform validate" gives me the error:

Error: Missing required argument

on main.tf line 52, in resource "aws_instance" "appserver1": 52: connection {

The argument "host" is required, but no definition was found.

2
Why remove your question? I think it was good and could help others with similar issue. - Marcin

2 Answers

0
votes

You have to specify connection details in the provisioner block. For example:

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

But in your case, using user_data would be more suited.

0
votes

Instead of using a connection you can make use of userdataEC2 userdata options to install tomcat while launching the deploy instance.

I don't think giving a connection block inside the instance configuration will work