# Create a policy

Write a customer-managed policy, in the console's builder or as JSON, before you attach it to anyone.

A policy grants nothing until it is attached to a user, a group or a role. Replace
S3ReadMyBucket with the policy's name and org-EXAMPLE1234 with your organization id.
For the language itself, see [Policy documents](policy-documents.md).

## Prerequisites

- Permission for `iam:CreatePolicy`.
- In the console, also `iam:ListPolicies`: the **Policies** list, where **Create policy** is, shows
  nothing else without it.

## Write the policy

**Console**
1. Open **Identity**, then choose **Policies**.
2. Choose **Create policy**.
3. For **Policy name**, enter S3ReadMyBucket. (Optional) For **Description**, say what it is
   for.
4. Under **Permissions**, write the document in one of four tabs:
   - **Quick**: choose **None**, **Read-only** or **Full** per service. The result applies to every
     resource in the organization.
   - **Statements**: one or more statements, each with an effect, its actions and a **Resource**.
   - **JSON**: the document itself.
   - **Templates**: start from a managed policy's document and change it.
5. Choose **Create policy**. The console returns to the list of policies.

Each tab submits what it shows. Leaving **Quick** or **Statements** copies what you built into the
**JSON** tab.

**CLI**
```sh
dcloud iam policies create --name S3ReadMyBucket \
  --document '{"Version":"2024-01-01","Statement":[{"Effect":"Allow","Action":["s3:GetObject","s3:ListObjects"],"Resource":["rn:ews::org-EXAMPLE1234:s3:*"]}]}'
```

**API**
The document is sent as JSON text inside the request:

```http
POST /iam/api/policies
{ "name": "S3ReadMyBucket", "document": "{\"Version\":\"2024-01-01\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"s3:GetObject\"],\"Resource\":[\"*\"]}]}" }
```

**Terraform**
```hcl
resource "dcloud_iam_policy" "s3_read" {
  name = "S3ReadMyBucket"
  document = jsonencode({
    Version = "2024-01-01"
    Statement = [{
      Effect   = "Allow"
      Action   = ["s3:GetObject", "s3:ListObjects"]
      Resource = ["rn:ews::${var.org_id}:s3:*"]
    }]
  })
}
```

## Result

The policy is listed under **Policies** as a custom policy. Attach it to put it into effect; see
[Attach a policy](attach-a-policy.md).

## If the policy is refused

The document is checked before it is stored. Refusals are 400 with the statement and the reason:

- `Invalid policy document: Invalid JSON`
- `Invalid policy document: Statement[0] references unknown action "s3:GetObjekt"`: every action
  pattern must match at least one action.
- `Invalid policy document: Statement[0] has invalid resource "…"`: a resource is `*` or a resource
  name.
- `Invalid policy document: Statement[0].Condition.…`: an operator or key outside the supported
  ones, or a value that cannot be read; see [Policy documents](policy-documents.md#conditions).
- 409 `Quota exceeded: …` when the organization has as many custom policies as its quota allows;
  see [Quotas](quotas.md).

## Related

- [Edit or delete a policy](edit-or-delete-a-policy.md)
- [Test access with the policy simulator](test-access-with-the-simulator.md): try a draft before
  you save it.
- `iam.policies.create` · `dcloud iam policies create` · `dcloud_iam_policy`
