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

# Manage applications

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

## Reference an externally managed application

To reference an application that you’ve deployed outside of the current Terraform plan, in your Terraform plan add a data source of the `juju_application` type, specifying the name of the application and of the model it is deployed to. For example:

```terraform
data "juju_application" "my-application" {
  model_uuid  = juju_model.development.uuid
  application = "mattermost"
}
```

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

<a id="deploy-an-application"></a>

## Deploy an application

To deploy an application, find and deploy a charm that delivers it.

> See more: [Deploy a charm](https://documentation.ubuntu.com/terraform-provider-juju/latest/howto/manage-charms.md#deploy-a-charm)

## Set the machine base for an application

> Only for machine clouds.

TBA

## Trust an application with a credential

Some applications may require access to the backing cloud in order to fulfil their purpose (e.g., storage-related tasks). In such cases, the remote credential associated with the current model would need to be shared with the application. When the Juju administrator allows this to occur the application is said to be *trusted*.

To trust an application with a credential, in the `juju_application` resource definition, add a `trust` attribute and set it to `true`:

```terraform
resource "juju_application" "this" {
  model_uuid = juju_model.development.uuid

  charm {
    name     = "hello-kubecon"
  }

    trust = true
}
```

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

<a id="configure-an-application"></a>

## Configure an application

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

To configure an application, in its resource definition add a configuration map with the `key=value` pairs you want (from the list of configurations available for the charm).

```terraform
resource "juju_application" "this" {
  model_uuid = juju_model.development.uuid

  charm {
    name = "hello-kubecon"
  }

  config = {
    redirect-map = "https://demo"
   }
}
```

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

<a id="scale-an-application"></a>

## Scale an application

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

### Scale an application vertically

To scale an application vertically, set constraints for the resources that the application’s units will be deployed on.

> See more: [Manage constraints for an application](#manage-constraints-for-an-application)

<a id="scale-an-application-horizontally"></a>

### Scale an application horizontally

To scale an application horizontally, control the number of units.

> See more: [Control the number of units](https://documentation.ubuntu.com/terraform-provider-juju/latest/howto/manage-units.md#control-the-number-of-units)

## Make an application highly available

> See also: [Juju | High availability (HA)](https://canonical.com/juju/docs/juju-cli/latest/reference/high-availability/#high-availability)
1. Find out if the charm delivering the application supports high availability natively or not. If the latter, find out what you need to do. This could mean integrating with a load balancing reverse proxy, configuring storage etc.

> See more: [Charmhub > `<your charm of interest>`](https://charmhub.io/)
1. Scale up horizontally as usual.

> See more: [Scale an application horizontally](#scale-an-application-horizontally)

<a id="integrate-an-application-with-another-application"></a>

## Integrate an application with another application

> See more: [Manage relations](https://documentation.ubuntu.com/terraform-provider-juju/latest/howto/manage-relations.md#manage-relations)

<a id="manage-an-applications-public-availability-over-the-network"></a>

## Manage an application’s public availability over the network

**Expose.** To expose an application over a network, in its resource definition use an expose attribute:

```terraform
resource "juju_application" "this" {
  model_uuid = juju_model.development.uuid

  charm {
    name = "hello-kubecon"
  }

  expose = {}
}
```

This will expose all of the application’s endpoints. To restrict exposure to just specific endpoints, spaces, or CIDRs, specify nested attributes.

> See more: [`juju_application` > `expose` > nested schema]()

**Unexpose.** To unexpose an application, remove the `expose` attribute from its resource definition.

<a id="manage-constraints-for-an-application"></a>

## Manage constraints for an application

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

To set constraints for an application, in its resource definition specify a `constraints` attribute followed by a quotes-enclosed, space-separated list of key=value pairs. For example:

```terraform
resource "juju_application" "this" {
  model_uuid = juju_model.development.uuid

  charm {
    name = "hello-kubecon"
  }

  constraints = "mem=6G cores=2"

}
```

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

<a id="change-space-bindings-for-an-application"></a>

## Change space bindings for an application

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

To set space bindings for an application, in its resource definition specify an `endpoint_bindings` with a `space` key, to set a default for the entire application, and/or a `space` and an `endpoint` key, to set the space binding for a particular application endpoint. For example, below all the application’s endpoints are bound to the `public` space except for the `juju-info` endpoint, which will be bound to the `private` space:

```terraform
resource "juju_application" "application_three" {
  model_uuid = resource.juju_model.testmodel.uuid
  charm {
    name = "juju-qa-test"
  }
  units = 0
  endpoint_bindings = [
    {
      "space" = "public"
    }
    {
      "space" = "private"
      "endpoint" = "juju-info"
    }
  ]
}
```

> See more: [`juju_application` > `endpoint_bindings`]()

<a id="upgrade-an-application"></a>

## Upgrade an application

To upgrade an application, update its charm.
When the charm is updated, its resources will also be updated unless pinned.

> See more: [Update a charm](https://documentation.ubuntu.com/terraform-provider-juju/latest/howto/manage-charms.md#update-a-charm)

<a id="remove-an-application"></a>

## Remove an application

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

To remove an application, remove its resource definition from your Terraform plan.

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