<br />

Google Cloud Organization Policy gives you centralized, programmatic
control over your organization's resources. As the
[organization policy administrator](https://docs.cloud.google.com/resource-manager/docs/organization-policy/using-constraints#add-org-policy-admin), you can define an organization policy,
which is a set of restrictions called constraints that apply to
Google Cloud resources and descendants of those resources in the
[Google Cloud resource hierarchy](https://docs.cloud.google.com/resource-manager/docs/cloud-platform-resource-hierarchy.md.txt). You can enforce organization policies at
the organization, folder, or project level.

Organization Policy provides [predefined constraints](https://docs.cloud.google.com/resource-manager/docs/organization-policy/org-policy-constraints) for various
Google Cloud services. However, if you want more granular, customizable
control over the specific fields that are restricted in your organization
policies, you can also create custom organization policies.

By implementing a custom organization policy you enforce consistent
configurations and restrictions. This ensures that your Memorystore for Redis Cluster
instances adhere to security best practices and regulatory requirements.

## Benefits

You can use a custom organization policy to allow or deny specific
Memorystore for Redis Cluster resources. For example, if a request to create or update
a Redis cluster fails to satisfy the custom constraint validation as set by your
organization policy, the request fails and an error is returned to the caller.

## Policy inheritance

By default, organization policies are inherited by the descendants of the
resources on which you enforce the policy. For example, if you enforce a policy
on a folder, Google Cloud enforces the policy on all projects in the
folder. To learn more about this behavior and how to change it, refer to
[Hierarchy evaluation rules](https://docs.cloud.google.com/resource-manager/docs/organization-policy/understanding-hierarchy#disallow_inheritance).

## Pricing

The Organization Policy Service, including predefined and custom organization policies, is
offered at no charge.

## Limitations

Like all organization policy constraints, policy changes don't apply
retroactively to existing instances.

- A new policy doesn't impact existing instance configurations.
- An existing instance configuration remains valid, unless you change the instance configuration from a compliance to non-compliance state using the Google Cloud console, Google Cloud CLI, or RPC.
- A scheduled maintenance update doesn't cause a policy enforcement, because maintenance doesn't change the configuration of instances.

## Before you begin

1. **Set up your project.**
   1. Ensure that you know your [organization ID](https://docs.cloud.google.com/resource-manager/docs/creating-managing-organization.md.txt#retrieving_your_organization_id).

### Required roles


To get the permissions that
you need to manage organization policies,

ask your administrator to grant you the
[Organization policy administrator](https://docs.cloud.google.com/iam/docs/roles-permissions/orgpolicy.md.txt#orgpolicy.policyAdmin) (`roles/orgpolicy.policyAdmin`) IAM role on the organization resource.


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).
You also need to add required roles to create Memorystore for Redis Cluster to your user account. See [Configuring access to Memorystore for Redis Cluster resources](https://docs.cloud.google.com/memorystore/docs/cluster/access-control.md.txt).

<br />

## Create a custom constraint

You can create a custom constraint by using a YAML file to define the resources,
methods, conditions, and actions that are subject to the constraint. These are
specific to the service on which you're enforcing the organization policy.
Conditions for your custom constraints must be defined using Common Expression
Language. See the GitHub page about [Common Expression Language (CEL)](https://github.com/google/cel-spec/blob/master/doc/intro.md).
For more information about how to build conditions in custom constraints using
CEL, see the CEL section of [Creating and managing custom constraints](https://docs.cloud.google.com/resource-manager/docs/organization-policy/creating-managing-custom-constraints#common_expression_language).

Use the following template to create a YAML file for a custom constraint:

```yaml
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- redis.googleapis.com/RESOURCE_NAME
methodTypes:
- CREATE
- UPDATE
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION
```

Replace the following:

- `ORGANIZATION_ID`: your organization ID, such as
  `123456789`.

- `CONSTRAINT_NAME`: the name you want for your new
  custom constraint. A custom constraint must start with `custom.`, and can
  only include uppercase letters, lowercase letters, or numbers, for
  example, `custom.restrictFiveShardClusters`. The maximum length of this field is 70
  characters, not counting the prefix, for example,
  `organizations/123456789/customConstraints/custom.allowConstraint`.

- `RESOURCE_NAME`: the name (not the URI) of the
  Memorystore for Redis Cluster resource containing the object and field
  you want to restrict. For example, redis.googleapis.com/Cluster.

- `CONDITION`: a [CEL condition](https://docs.cloud.google.com/resource-manager/docs/organization-policy/creating-managing-custom-constraints#common_expression_language) that is written
  against a representation of a supported service resource. This
  field has a maximum length of 1,500 characters. See
  [Supported resources](https://docs.cloud.google.com/memorystore/docs/cluster/custom-constraints.md.txt#supported_resources) for more information about the
  resources available to write conditions against. For example,
  "resource.shardCount == 5".

- `ACTION`: the action to take if the `condition` is
  met. This can be either `ALLOW` or `DENY`.

- `DISPLAY_NAME`: a human-friendly name for the
  constraint. This field has a maximum length of 200 characters.

- `DESCRIPTION`: a human-friendly description of the
  constraint to display as an error message when the policy is violated. This
  field has a maximum length of 2000 characters.

For more information about how to create a custom constraint, see
[Creating and managing custom organization policies](https://docs.cloud.google.com/resource-manager/docs/organization-policy/creating-managing-custom-constraints#defining_custom_constraints).

## Set up a custom constraint

After you create the YAML file for a new custom constraint, you must set
it up to make it available for organization policies in your organization.

To set up a custom constraint, use the [`gcloud org-policies set-custom-constraint`](https://docs.cloud.google.com/sdk/gcloud/reference/org-policies/set-custom-constraint) command:

```
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
```

Replace <var translate="no">CONSTRAINT_PATH</var> with the full path to your custom constraint
file. For example, `/home/user/customconstraint.yaml`. After you set up your
custom constraints, they're available as organization policies in your list of
Google Cloud organization policies.

To verify that the custom constraint exists, use the [`gcloud org-policies list-custom-constraints`](https://docs.cloud.google.com/sdk/gcloud/reference/org-policies/list-custom-constraints) command:

```
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
```

Replace <var translate="no">ORGANIZATION_ID</var> with the ID of your organization resource.
For more information, see [Viewing organization policies](https://docs.cloud.google.com/resource-manager/docs/organization-policy/creating-managing-policies#viewing_organization_policies).

## Enforce a custom organization policy

You can enforce a constraint by creating an organization policy that references it, and then applying that organization policy to a Google Cloud resource.

### Console

1. In the Google Cloud console, go to the **Organization policies** page.

   [Go to Organization policies](https://console.cloud.google.com/iam-admin/orgpolicies)
2. From the project picker, select the project that you want to set the organization policy for.
3. From the list on the **Organization policies** page, select your constraint to view the **Policy details** page for that constraint.
4. To configure the organization policy for this resource, click **Manage policy**.
5. On the **Edit policy** page, select **Override parent's policy**.
6. Click **Add a rule**.
7. In the **Enforcement** section, select whether this organization policy is enforced or not.
8. Optional: To make the organization policy conditional on a tag, click **Add condition** . Note that if you add a conditional rule to an organization policy, you must add at least one unconditional rule or the policy cannot be saved. For more information, see [Scope organization policies with tags](https://docs.cloud.google.com/organization-policy/scope-policies.md.txt).
9. Click **Test changes** to simulate the effect of the organization policy. For more information, see [Test organization policy changes with Policy Simulator](https://docs.cloud.google.com/policy-intelligence/docs/test-organization-policies.md.txt).
10. To enforce the organization policy in dry-run mode, click **Set dry run policy** . For more information, see [Test organization policies](https://docs.cloud.google.com/organization-policy/test-policies.md.txt).
11. After you verify that the organization policy in dry-run mode works as intended, set the live policy by clicking **Set policy**.

### gcloud

1. To create an organization policy with boolean rules, create a policy YAML file that references the constraint:

```yaml
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
spec:
  rules:
  - enforce: true

dryRunSpec:
  rules:
  - enforce: true
```
2. Replace the following:
   - `PROJECT_ID`: the project that you want to enforce your constraint on.
   - `CONSTRAINT_NAME`: the name you defined for your custom constraint. For example, `custom.restrictFiveShardClusters`.
3. To enforce the organization policy in [dry-run mode](https://docs.cloud.google.com/organization-policy/test-policies.md.txt), run the following command with the `dryRunSpec` flag:

```bash
gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec
```
4. Replace `POLICY_PATH` with the full path to your organization policy YAML file. The policy requires up to 15 minutes to take effect.
5. After you verify that the organization policy in dry-run mode works as intended, set the live policy with the `org-policies set-policy` command and the `spec` flag:

```bash
gcloud org-policies set-policy POLICY_PATH --update-mask=spec
```
6. Replace `POLICY_PATH` with the full path to your organization policy YAML file. The policy requires up to 15 minutes to take effect.

## Test a custom constraint

To test a custom constraint, run the gcloud command that attempts
to create a cluster.

For example, assume that a constraint restricts creation of clusters with five
shards. You could test this constraint by running the `gcloud redis clusters create`
command with shard-count set to 5 as demonstrated in the following snippet:


    gcloud redis clusters create redis-cluster-test \
        --replica-count=0 \
        --region=us-east1 \
        --project=my-project \
        --network=projects/my-project/global/networks/default \
        --node-type=redis-shared-core-nano
        --shard-count=5 \

The output is similar to the following:

    Operation denied by custom org policies: ["customConstraints/custom.restrictFiveShardClusters": "Prevent users from creating Redis clusters with five shards."]

> [!CAUTION]
> **Caution** : We recommend that you use the `redis-shared-core-nano` node type only for development or testing purposes because this node type has no SLA. If you run Memorystore for Redis Cluster in a production environment, then we recommend using another node type. For more information about these node types, see [Choose a node type](https://docs.cloud.google.com/memorystore/docs/cluster/cluster-node-specification.md.txt#choose_a_node_type).

## Memorystore for Redis Cluster supported resources and operations

You can use the following custom constraint fields when you **create or update a
resource**:

- [Cluster](https://docs.cloud.google.com/memorystore/docs/cluster/reference/rest/v1/projects.locations.clusters.md.txt)
  - `resource.allowFewerZonesDeployment`
  - `resource.asyncClusterEndpointsDeletionEnabled`
  - `resource.authorizationMode`
  - `resource.automatedBackupConfig`
  - `resource.clusterEndpoints`
  - `resource.deletionProtectionEnabled`
  - `resource.gcsSource`
  - `resource.kmsKey`
  - `resource.maintenancePolicy`
  - `resource.managedBackupSource`
  - `resource.name`
  - `resource.nodeType`
  - `resource.persistenceConfig`
  - `resource.persistenceConfig.aofConfig.appendFsync`
  - `resource.persistenceConfig.mode`
  - `resource.persistenceConfig.rdbConfig.rdbSnapshotPeriod`
  - `resource.persistenceConfig.rdbConfig.rdbSnapshotStartTime`
  - `resource.pscConfigs`
  - `resource.pscConfigs.network`
  - `resource.redisConfigs`
  - `resource.replicaCount`
  - `resource.shardCount`
  - `resource.simulateMaintenanceEvent`
  - `resource.transitEncryptionMode`
  - `resource.zoneDistributionConfig`

## Example custom constraints

The following table provides an example custom constraint that restricts
clusters with five shards:

| Description | Constraint syntax |
|---|---|
| Restrict Redis clusters with five shards | ```yaml name: organizations/ORGANIZATION_ID/customConstraints/custom.restrictFiveShardClusters resourceTypes: - redis.googleapis.com/Cluster methodTypes: - CREATE - UPDATE condition: "resource.shardCount == 5" actionType: DENY displayName: Restrict five shard Redis clusters description: Prevent users from creating Redis clusters with five shards. ``` |

## What's next

- See [Introduction to the Organization Policy Service](https://docs.cloud.google.com/resource-manager/docs/organization-policy/overview) to learn more about organization policies.
- Learn more about how to [create and manage organization policies](https://docs.cloud.google.com/resource-manager/docs/organization-policy/using-constraints).
- See the full list of predefined [Organization policy constraints](https://docs.cloud.google.com/resource-manager/docs/organization-policy/org-policy-constraints).