<a id="manage-machines"></a>

# Manage machines

<!--FIGURE OUT A GOOD PLACE FOR THIS:
An interactive pseudo-terminal (pty) is enabled by default. For the OpenSSH client, this corresponds to the \`-t\` option ("force pseudo-terminal allocation").

Remote commands can be run as expected. For example: \`juju ssh 1 lsb_release -c\`. For complex commands the recommended method is by way of the \`run\` command.
-->
> See also: [Juju | Machine](https://canonical.com/juju/docs/juju-cli/latest/reference/machine/#machine)

## Reference an externally managed machine

To reference a machine that you’ve already provisioned outside of the current Terraform plan, in your Terraform plan add a data source of the `juju_machine` type, specifying the machine ID and the name of its hosting model. For example:

```terraform
data "juju_machine" "this" {
  model_uuid = juju_model.development.uuid
  machine_id = "2"
}
```

> See more: [`juju_machine` (data source)](https://documentation.ubuntu.com/terraform-provider-juju/latest/reference/terraform-provider/data-sources/machine.md)

## Add a machine

To add a machine to a model, in your Terraform plan add a resource of the `juju_machine` type, specifying the model.

```terraform
resource "juju_machine" "machine_0" {
  model_uuid = juju_model.development.uuid
}
```

You can optionally specify a base, a name, regular constraints, storage constraints, etc. You can also specify a `private_key_file`, `public_key_file`, and `ssh_address` – that will allow you to add to the model an existing, manual machine (rather than a virtual one provisioned for you by the cloud).

> See more: [`juju_machine` (resource)](https://documentation.ubuntu.com/terraform-provider-juju/latest/reference/terraform-provider/resources/machine.md)

## Manage constraints for a machine

> See also: [Juju | Constraint](https://canonical.com/juju/docs/juju-cli/latest/reference/constraint/#constraint)

To set constraints for a machine, in your Terraform plan, in the machine resource definition, set the constraints attribute to the desired quotes-enclosed, space separated list of key=value pairs. For example:

```terraform
resource "juju_machine" "machine_0" {
  model_uuid = juju_model.development.uuid
  name        = "machine_0"
  constraints = "tags=my-machine-tag"
}
```

> See more: [`juju_machine` (resource)](https://documentation.ubuntu.com/terraform-provider-juju/latest/reference/terraform-provider/resources/machine.md)

## Remove a machine

> See also: [Juju | Removing things](https://canonical.com/juju/docs/juju-cli/latest/reference/removing-things/#removing-things)

To remove a machine, remove its resource definition from your Terraform plan.

> See more: [`juju_machine` (resource)](https://documentation.ubuntu.com/terraform-provider-juju/latest/reference/terraform-provider/resources/machine.md)
