# Manage access keys

See when each access key was last used and when it expires, deactivate or reactivate a key, delete it, and set how long new keys live.

Deactivating a key stops it working and keeps it, so it can be turned back on; deleting it is
final. Replace key-id with the key's id (a UUID, not the `EWS1…` key ID).

## Prerequisites

- To list keys: `iam:ListAccessKeys`. The console's **Access keys** page needs it for every task
  below; without it the page shows only "You do not have permission to list access keys."
- To deactivate or activate a key, or give old keys an expiry: `iam:UpdateAccessKey`.
- To delete a key: `iam:DeleteAccessKey`.

## List keys

**Console**
1. Open **Identity**, then choose **Access keys**.
2. (Optional) Choose **Active**, **Inactive** or **Stale** to narrow the list.

The table shows each key's **Status**, **Principal**, **Last used**, **Expires** and
**Created**. **Last used** is updated at most once a minute.

**CLI**
```sh
dcloud iam access-keys list --iam-user-id 3f8b6c2e-1d4a-4b7e-9c2f-5a6b7c8d9e0f
```

**API**
```http
GET /iam/api/access-keys?iamUserId=3f8b6c2e-1d4a-4b7e-9c2f-5a6b7c8d9e0f
```

**Terraform:** not available. Terraform manages the keys it created; it does not list others.

## Deactivate or activate a key

**Console**
1. On **Access keys**, open the key's actions menu.
2. Choose **Deactivate**, or **Activate** for an inactive key.

To deactivate several keys at once, select them and choose **Deactivate** above the table.

**CLI**
```sh
dcloud iam access-keys set-status key-id --status inactive
```

**API**
```http
PATCH /iam/api/access-keys/key-id
{ "status": "inactive" }
```

**Terraform**
```hcl
resource "dcloud_iam_access_key" "ci" {
  iam_user_id = dcloud_iam_user.ci.id
  status      = "inactive"
}
```

A request signed with an inactive key is refused with 401 `Invalid or inactive access key`.

## Delete a key

**Warning:**
Anything using the key loses access at once, through the API and S3 alike. A deleted key cannot be
restored.

**Console**
1. On **Access keys**, open the key's actions menu and choose **Delete key**.
2. Choose **Delete**.

**CLI**
```sh
dcloud iam access-keys delete key-id
```

**API**
```http
DELETE /iam/api/access-keys/key-id
```

**Terraform**
Remove the `dcloud_iam_access_key` resource from your configuration and apply.

## Expiry

Every key made now expires. A request signed with an expired key is refused with 401
`Access key has expired`; the key stays listed, marked **Expired**.

- **Lifetime of new keys.** Your organization's settings, under **Access keys**, hold the
  **Default lifetime (days)** (90 unless changed) and the **Maximum lifetime (days)** (365 unless
  changed, at most 3650). Changing them needs `iam:UpdateOrganizationAuthPolicy` and affects only
  keys created afterwards.
- **Keys without an expiry.** Keys made before every key expired have none. Give them all one at
  once, counted from today:

**Console**
1. Open your organization's settings and find **Access keys**.
2. Under **Keys that never expire**, choose **Give them an expiry**.

The keys get the default lifetime from today.

**CLI**
```sh
dcloud iam access-keys set-expiry --expires-in-days 90
```

**API**
```http
POST /iam/api/access-keys/set-expiry
{ "expiresInDays": 90 }
```

**Terraform:** not available. This is a one-off change to existing keys, not a resource.

Keys the platform made for app machines and keys from a role's token exchange are left out. The
answer says how many keys changed and their new expiry date. More days than the organization's
maximum is refused with 400 `expiresInDays is …; this organization allows at most … days`.

## Related

- [Create an access key](create-an-access-key.md)
- [Revoke role sessions](revoke-role-sessions.md): end temporary credentials of a role.
- `iam.access-keys.list` · `iam.access-keys.set-status` · `iam.access-keys.delete` ·
  `iam.access-keys.set-expiry`
