How to manage charm resources

See also: Resource (charm)

When you deploy / update an application from a charm, that automatically deploys / updates any charm resources, using the defaults specified by the charm author. However, you can also specify resources manually (e.g., to try a resource released only to edge or to specify a non-Charmhub resource). This document shows you how.

Find out the resources available for a charm

To find out what resources are available for a charm on Charmhub, run the charm-resources command followed by the name of the charm:

juju charm-resources <charm>

Expand to view a sample output for the 'postgresql-k8s' charm
$ juju charm-resources postgresql-k8s
Resource          Revision
postgresql-image  68

The command has flags that allow you to specify a charm channel, an output format, an output file, etc.

See more: juju charm-resources

Alternatively, you can also consider a resource available somewhere else online (e.g., a link to an OCI image) or in your local filesystem.

The terraform juju client does not support this. Please use the juju client.

To find out what resources are available for a charm on Charmhub, on a connected Model object, select the charmhub object associated with the model, and use the list_resources() method, passing the name of the charm as an argument. For example:

await model.charmhub.list_resources('postgresql-k8s')

See more: charmhub (property), Model (module)

Specify the resources to be deployed with a charm

How you specify a resource to deploy with a charm depends on whether you want to do this during deployment or, as an update, post-deployment.

  • To specify a resource during deployment, run the deploy command with the --resources flag followed by a key-value pair consisting of the resource name and the resource:
juju deploy <charm name> --resources <resource name>=<resource>

See more: juju deploy

  • To specify a resource after deployment, run the attach-resource command followed by the name of the deployed charm (= application) and a key-value pair consisting of the resource name and the resource revision number of the local path to the resource file:
juju attach-resource <charm name> <resource name>=<resource>

Regardless of the case, the resource name is always as defined by the charm author (see the Resources tab of the charm homepage on Charmhub or the resources map in the metadata.yaml file of the charm) and the resource is the resource revision number, a path to a local file, or a link to a public OCI image (only for OCI-image type resources).


Expand to view an example where the resource is specified post-deployment by revision number
juju attach-resource  juju-qa-test foo-file=3

  • To update a resource’s revision, run the refresh command with the --resource flag followed by a key=value pair denoting the name of the resource and its revision number or the local path to the resource file.

See more: juju deploy ... --resources, juju attach-resource, juju refresh ... --resources

To specify the resource(s) to be deployed with your charm, in your Terraform plan, in the definition of the resource for the application specify a resources block with key-value pairs listing resource names and their revision number. For example:

resource "juju_application" "application_one" {
  name = "my-application"
  model = juju_model.testmodel.name
  
  charm {
    name = "juju-qa-test"
    channel = "2.0/edge"
  }
  resources = {
    "foo-file" = 4
  }
}

About charm > revision and resources and their counterparts in the juju client:

  • If you specify only charm > revision: This is equivalent to juju deploy <charm> --revision or juju refresh <charm> --revision – that is, the resource revision is automatically the latest.
  • If you specify only resources: This is equivalent to juju attach-resource – that is, the resource revision is whatever you’ve specified.

Note: While juju refresh <charm> --resource allows you to update a resource even if no update is available for the charm, this is not possible with terraform juju.

See more: juju_application > resources

To specify a resource during deployment, on a connected Model object, use the deploy method, passing the resources as a parameter. For example:

resources = {"file-res": "test.file"}
app = await model.deploy(charm_path, resources=resources)

To update a resource after deployment by uploading file from local disk, on an Application object, use the attach_resource() method, passing resource name, file name and the file object as parameters.

with open(str(charm_path / 'test.file')) as f:
    app.attach_resource('file-res', 'test.file', f)

See more: deploy(), attach_resource(), Model (module)

View the resources deployed with a charm

To view the resources that have been deployed with a charm, run the resources command followed by the name of the corresponding application / ID of one of the application’s units.

juju resources <application name> / <unit ID>

See more: juju resources

The terraform juju client does not support this. Please use the juju client.

To view the resources that have been deployed with a charm, on an Application object, use the get_resources() method. For example:

await my_app.get_resources()

See more: get_resources(), Model (module)


Contributors: @cderici, @hmlanigan, @tmihoc

charm name placeholder is missing, it should be

juju attach-resource <charm name> <resource name>=<resource>

1 Like

Fixed, thanks!