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

Created on 25 Mar 2019  ·  18Comments  ·  Source: hashicorp/terraform

Terraform Version

Terraform v0.12.0-beta1
+ provider.aws v1.60.0-dev20190216h00-dev
+ provider.local v1.2.0
+ provider.random v2.1.0

Terraform Configuration Files

resource "aws_instance" "default" {
  ami                   = var.dsm_ami
  instance_type         = var.dsm_instance_type
  security_groups       = var.dsm_security_groups
  iam_instance_profile  = var.dsm_iam_instance_profile
  key_name              = var.dsm_key_pair
  tags                  = local.tags

  /* copy up private keyfile for chef-solo to use */

  provisioner "file" {
    source = "~/.ssh/Oregon.pem"
    destination = "/home/ec2-user/.ssh/Oregon.pem"
    connection {
      type = "ssh"
      user = "ec2-user"
      private_key = "${file("~/.ssh/Oregon.pem")}"
    }
  }
}

Debug Output

2019/03/25 11:38:43 [TRACE] statemgr.Filesystem: unlocked by closing terraform.tfstate
Error: Missing required argument

  on main.tf line 27, in resource "aws_instance" "default":
2019-03-25T11:38:43.461-0400 [DEBUG] plugin: plugin process exited: path=C:\Terraform\terraform.exe pid=2384
2019-03-25T11:38:43.461-0400 [DEBUG] plugin: plugin exited
  27: resource "aws_instance" "default" {
2019-03-25T11:38:43.463-0400 [DEBUG] plugin: plugin process exited: path=C:\Users\wesleym\AppData\Roaming\terraform.d\plugins\windows_amd64\terraform-provider-aws_v1.60.0-dev20190216H00-dev_x4.exe pid=15504

2019-03-25T11:38:43.463-0400 [DEBUG] plugin: plugin exited
The argument "host" is required, but no definition was found.

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. terraform init
  2. terraform plan or terraform apply
cli enhancement

Most helpful comment

I encountered the same error on DigitalOcean. It was fixed by specifying host = "self.public_ip"

connection {
        user = "root"
        type = "ssh"
        private_key = "${var.pvt_key}"
        timeout = "2m"
        host = "self.public_ip"
}

All 18 comments

Hi @wesmartin17,

Sorry about the rather unhelpful error output here. This is the expected result because you're missing the required host field in the connection block. You most likely want host = self.public_ip for an aws_instance.

I'm going to keep this open, and see if we can't improve the validation output here to provide more context and possibly catch it earlier in validation.

There is more information about this in the upgrade guide section Default settings in connection blocks.

Great! Thank you so much for getting to this so quickly @jbardin @apparentlymart I must have missed that i

It seems like there is a bug somewhere in the vicinity of validateConnConfig because the source location reported for this error is, somehow, the header of the containing resource block rather than the the connection block where the problem is. With that said, at first glance it's not clear to me how that could be since this codepath doesn't even have access to the containing block to report it as a location; perhaps this is actually being reported elsewhere too and whatever other codepath is validating this is not setting the source location properly.

It's actually not caught in validation, and the error isn't found until provisioning (EvalApplyPost).
I don't think there's there's any context, and it's simply reporting which resource failed to provision.

There is more information about this in the upgrade guide section Default settings in connection blocks.

@apparentlymart and @jbardin Thanks for your appreciations. I am following the Azure guide, and when I added host = self.public_ip as a default setting in connection IP I get this:

⟩ terraform plan -out tfprov

Error: Unsupported attribute

  on main.tf line 198, in resource "azurerm_virtual_machine" "vm":
 198:       host = self.public_ip

This object has no argument, nested block, or exported attribute named
"public_ip".

Finally I had to put host = "${azurerm_public_ip.publicip.name}" which reference my azurerm_public_ip resource previously defined, and it's works to generate the plan, but when I apply the plan I get this problem:

⟩ terraform apply tfprov 
azurerm_resource_group.rg: Creating...
azurerm_resource_group.rg: Creation complete after 0s [id=/subscriptions/4e65758d-dbf5-456f-bb55-6b92273772dd/resourceGroups/myTFResourceGroup]
azurerm_virtual_network.vnet: Creating...
azurerm_public_ip.publicip: Creating...
azurerm_network_security_group.nsg: Creating...
azurerm_public_ip.publicip: Creation complete after 1s [id=/subscriptions/4e65758d-dbf5-456f-bb55-6b92273772dd/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/publicIPAddresses/myTFPublicIP]
azurerm_virtual_network.vnet: Still creating... [10s elapsed]
azurerm_network_security_group.nsg: Still creating... [10s elapsed]
azurerm_network_security_group.nsg: Creation complete after 11s [id=/subscriptions/4e65758d-dbf5-456f-bb55-6b92273772dd/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/networkSecurityGroups/myTFNSG]
azurerm_virtual_network.vnet: Still creating... [20s elapsed]
azurerm_virtual_network.vnet: Still creating... [30s elapsed]
azurerm_virtual_network.vnet: Still creating... [40s elapsed]
azurerm_virtual_network.vnet: Creation complete after 41s [id=/subscriptions/4e65758d-dbf5-456f-bb55-6b92273772dd/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
azurerm_subnet.subnet: Creating...
azurerm_subnet.subnet: Still creating... [10s elapsed]
azurerm_subnet.subnet: Creation complete after 11s [id=/subscriptions/<subscription-id>/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet/subnets/myTFSubnet]
azurerm_network_interface.nic: Creating...
azurerm_network_interface.nic: Creation complete after 1s [id=/subscriptions/<subscription-id>/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/networkInterfaces/myNIC]
azurerm_virtual_machine.vm: Creating...
azurerm_virtual_machine.vm: Still creating... [10s elapsed]
azurerm_virtual_machine.vm: Still creating... [20s elapsed]
azurerm_virtual_machine.vm: Still creating... [30s elapsed]
azurerm_virtual_machine.vm: Still creating... [40s elapsed]
azurerm_virtual_machine.vm: Still creating... [50s elapsed]
azurerm_virtual_machine.vm: Still creating... [1m0s elapsed]
azurerm_virtual_machine.vm: Still creating... [1m10s elapsed]
azurerm_virtual_machine.vm: Still creating... [1m20s elapsed]
azurerm_virtual_machine.vm: Still creating... [1m30s elapsed]

azurerm_virtual_machine.vm: Still creating... [6m30s elapsed]


Error: timeout - last error: dial tcp: lookup myTFPublicIP on 127.0.0.53:53: server misbehaving

I am here in this step, and I am not sure how should I proceed here ...

Hi @bgarcial!

It seems that something has changed in the Azure provider itself that has affected what is shown in the getting started guide. For that, it may be best to ask in the AzureRM provider's own repository to see if the documentation needs to be updated and whether the update you made here is the best one.

For me it worked like this:

  provisioner "remote-exec" {
    connection {
      user     = "${var.vm_admin_username}"
      password = "${var.vm_admin_password}"
      port     = 5986
      https    = true
      timeout  = "10m"

      # NOTE: if you're using a real certificate, rather than a self-signed one, you'll want this set to `false`/to remove this.
      insecure = true
      host = azurerm_public_ip.main.ip_address
    }
    inline = [
      "powershell.exe -command \"Write-Host '$(Get-Date) Provisioning is done'\""
    ]
  }
resource "azurerm_public_ip" "main" {
  name                = "contoso-sharepoint-${var.environmentId}-${var.vm_name}-pip"
  location            = "westeurope"
  resource_group_name = "${var.vm_resource_group_name}"
  allocation_method   = "Dynamic"
  domain_name_label   = "${var.vm_domain_name_label}"
}

Hi @apparentlymart, thanks for your support

I've changed the host = "${azurerm_public_ip.publicip.name}" by host = "${azurerm_public_ip.publicip.ip_address}" like @shurick81 suggest and it's works.
It has sense because the host attribute is looking for some ip equivalence and not for a name string I suppose.

Of anyway, there are several details which would be nice were updated on the guide.
One of them is related to the tags attribute, (which you already pointed to someone in the right path)

Also in the definition of the azurerm_public_ip resource, in this 0.12 version, the public_ip_address_allocation attribute was deprecated in favor of allocation_method

How can I report this small details in order to be corrected in the guide?
Maybe here in the Azure RM provider repo like you told me?

The "Getting Started" guides are unusual in that while they are provider-specific we think of them as guides for Terraform Core -- they are primarily about how to use Terraform itself rather than how to use the provider.

With that said, if you make a new issue in this repository then we'll label it so the team who maintains the learning guides can find it. Thanks!

@apparentlymart Ok, nice. I still doing the Azure Guide. I will share the issues here when it has finished

I encountered the same error on DigitalOcean. It was fixed by specifying host = "self.public_ip"

connection {
        user = "root"
        type = "ssh"
        private_key = "${var.pvt_key}"
        timeout = "2m"
        host = "self.public_ip"
}

I am using host = "${google_compute_instance.default.network_interface.0.access_config.0.nat_ip}" for Google Compute Engine ephemeral, external IP addresses.

For Google Compute Engine, the terraform 0.12upgrade command, initially suggested this:

     access_config {
-      nat_ip = "${data.google_compute_address.<name-of-static-ip>.address}"
+      nat_ip = data.google_compute_address.<name-of-static-ip>.address
     }

...
   provisioner "remote-exec" {
     connection {
+      host = coalesce( # TF-UPGRADE-TODO: Simplify this to reference a specific desired IP address, if possible.
+        concat(
+          # Prefer any available NAT IP address
+          flatten([
+            for ni in self.network_interface : [
+              for ac in ni.access_config : ac.nat_ip
+            ]
+          ]),
+
+          # Otherwise, use the first available LAN IP address
+          [
+            for ni in self.network_interface : ni.network_ip
+          ],
+        )...
+      )
       type = "ssh"

With this config of a static IP, this code worked ok:

host = data.google_compute_address.<name-of-static-ip>.address

Hi @petervandenabeele!

Indeed, there are lots of different ways to configure IP addresses for Google Compute and the upgrade tool isn't smart enough to understand those different ways, so it produces this complex expression that is essentially a direct port of the logic the provider itself would've used in Terraform 0.11 and prior. We added that # TF-UPGRADE-TODO comment because in practice there's usually a much simpler expression to replace it with, as in your case, but it requires some knowledge about how the rest of the configuration is built and is thus better done via human intuition than by heuristic. (More historical context is in terraform-providers/terraform-provider-google#3126 .)

Note that the docs for Provisioner Connections still claims:

host - The address of the resource to connect to. This is usually specified by the provider.

https://www.terraform.io/docs/provisioners/connection.html#host

I encountered the same error on DigitalOcean.
Change: SERVERNAME

    connection {
      type     = "ssh"
      private_key = "${file("~/.ssh/id_rsa")}"
      user     = "root"
      timeout  = "2m"
      host = "${digitalocean_droplet.SERVERNAME.ipv4_address}"
    }

I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kforsthoevel picture kforsthoevel  ·  86Comments

glenjamin picture glenjamin  ·  112Comments

atkinchris picture atkinchris  ·  68Comments

ncraike picture ncraike  ·  77Comments

gwagner picture gwagner  ·  81Comments