Working with Resource Tags

Tagging resources enables you to identify characteristics of resources and apply the same policies to a group of resources. For more information about policies, see Using Defined Tags in Conditions.

Note the following requirements:

  • To apply, modify, or delete a tag on a resource, you must have permission to update the resource.

  • To apply, modify, or delete a defined tag on a resource, you must also have use access on the tag namespace.

  • A resource can have at most 64 defined tags and ten free-form tags.

Adding Tags at Resource Creation

Any tag defaults that are defined on a compartment are automatically added to all resources that are created in that compartment, or any child compartment of that compartment, after the tag default was defined. A tag default might require you to enter a value for the tag in order to create the resource. See Configuring Tag Defaults.

Using the Compute Web UI

  1. In the Create dialog for the resource, scroll to the Tagging section.

  2. Select the Tag Namespace or select None (apply a free-form tag).

    • If you selected a Tag Namespace, then select the Tag Key, and enter a value or select a value from the list.

    • If you selected None (apply a free-form tag), then enter a Tag Key and enter a value.

  3. To apply another tag, click the Additional Tag button.

    You cannot specify more than one tag with the same tag namespace and the same tag key for a defined tag, or more than one tag with the same tag key for a free-form tag.

  4. To review the tags on the resource, to go the details page for the new resource.

    On the resource details page, click the Tags tab to display the tags that are applied to this resource.

Using the OCI CLI

To add a tag to a resource when you create the resource, use the resource create or launch command.

  1. Get the information for each tag that you want to add to the resource.

    • Get the namespace, key, and value for each defined tag that you want to add to the resource.

      Construct an argument for the --defined-tags option. Specify each tag namespace and tag key pair only one time.

    • Get the key and value for each free-form tag that you want to add to the resource.

      Construct an argument for the --freeform-tags option. Specify each tag key only one time.

    The value of the --defined-tags option argument and the --freeform-tags option argument is a JSON definition of the tags. This JSON definition can be provided as a string on the command line or in a file.

    You can generate a template of the correct JSON to provide by using the --generate-param-json-input option with the base command that you will use to tag the resource. The argument for the --generate-param-json-input option is the name of the option that you will use to specify the tags (--defined-tags in this example) without the option indicator (--), as shown in the following example:

    $ oci service resource create \
    --generate-param-json-input defined-tags > defined_tags.json

    The content of the output defined_tags.json file is:

    {
      "tagNamespace1": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      },
      "tagNamespace2": {
        "tagKey1": "tagValue1",
        "tagKey2": "tagValue2"
      }
    }

    If you specify freeform-tags instead of defined-tags in the preceding command, you get the following output:

    {
      "tagKey1": "tagValue1",
      "tagKey2": "tagValue2"
    }

    Edit these templates to provide the desired tags. Specify the result in the final command as shown in the following step.

  2. Run the resource create or launch command.

    If you want to add one or more defined tags, use the --defined-tags option. If you want to add one or more free-form tags, use the --freeform-tags option.

    Syntax:

    oci service resource create --compartment-id compartment_OCID \
    --defined-tags defined_tags_json --freeform-tags freeform_tags_json \
    other_resource_create_options

    Example:

    In the following example, one or more defined tags is added using a file, and a free-form tag is added using a string argument. Use the file:// syntax to specify a file as the option argument.

    $ oci service resource create --compartment-id ocid1.compartment.unique_ID \
    --defined-tags file://defined_tags.json --freeform-tags '{"MyTag":"val-u"}' \
    other_resource_create_options

    The output of the resource create or launch command is the same as the output of the resource get command. The output shows the defined and free-form tags.

Applying Tags to an Existing Resource

Using the Compute Web UI

  1. In the resource Edit dialog, scroll to the Tagging section.

    You can add tags, and you can modify or delete any tags that already exist.

  2. To add tags, click the Additional Tag button if necessary, and select the Tag Namespace or select None (apply a free-form tag).

    • If you selected a Tag Namespace, then select a Tag Key, and enter a value or select a value from the list.

    • If you selected None (apply a free-form tag), then enter a Tag Key and enter a value.

    You cannot specify more than one tag with the same tag namespace and the same tag key for a defined tag, or more than one tag with the same tag key for a free-form tag.

  3. To modify existing tags, change the selections or enter different values.

  4. To delete a tag, click the trash can.

  5. When you are finished adding and modifying tags, click Save Changes.

  6. To review the tags on the resource, go to the details page for the resource.

    On the resource details page, click the Tags tab to display the list of tags that are applied to this resource.

Using the OCI CLI

To add tags to an existing resource, and modify or delete any tags that already exist, use the resource update command.

  1. Get the namespace, key, and value information for each tag that you want to add to the resource.

  2. Create a JSON definition of the tags that you want to apply. See Adding Tags at Resource Creation for information about how to create correct JSON.

    Note:

    Any defined tags that already exist on this resource will be replaced by the --defined-tags argument. Any free-form tags that already exist on this resource will be replaced by the --freeform-tags argument. Be sure to include any existing tags that you want to keep in the new arguments for these options.

    Use the resource get command to show the current defined and free-form tags.

  3. Run the resource update command.

    If you want to apply one or more defined tags, use the --defined-tags option. If you want to apply one or more free-form tags, use the --freeform-tags option.

    The output of the resource update command is the same as the output of the resource get command. The output shows the defined and free-form tags.

Filtering a List of Resources by Tag

Using the Compute Web UI

  1. Display a list of resources.

  2. Under Filter by Tag(s), click Select Tag(s).

  3. Do the following in the Filter by Tag dialog:

    1. Select either Defined Tag or Free-Form Tag.

      • If you select Defined Tag, then select a Tag Namespace and a Tag Key.

      • If you selected Free-Form Tag, then enter a Tag Key.

    2. Optionally enter values in the Select Values (optional) field.

      • Leave the Select Values (optional) field blank. This option returns all resources that are tagged with the selected namespace and key (for defined tags) or returns all resources that are tagged with the specified key (for free-form tags), regardless of the tag value.

      • Enter values in the Select Values (optional) field. This option returns all resources that are tagged with any of the tag value(s) that you enter. Select a value or enter a single value in the text box. To specify multiple values for the same namespace and key, enter Enter or Return, and then enter a new value. Each value is displayed below the text box.

    3. Click Filter by Tag.

      The filter that is currently applied is displayed on the Select Tag(s) button.

  4. To filter by multiple tags, click the + on the Select Tag(s) button, and repeat the previous step.

  5. To remove a filter, click the filter definition on the Select Tag(s) button.

Using the OCI CLI

  1. Use the list command for the resource to show each resource in the specified compartment.

    The information for each resource shows the existing defined tags and free-form tags.

  2. Use tools for your operating system to filter the list.