# Create an access key

Create a credential that acts as an IAM user outside the browser, for the API, the CLI, Terraform, the container registry, git over HTTPS and S3 clients.

An access key is a pair: a key ID of 20 characters that starts with `EWS1`, and a secret of 40
characters. The key acts as one IAM user and can do at most what that user's policies allow.
Replace ci-deploy with what the key is for.

## Prerequisites

- Permission for `iam:CreateAccessKey`. Without it the console says "Creating a key needs
  iam:CreateAccessKey; ask an administrator." and shows no **Create access key** button.
- In the console, also `iam:ListAccessKeys`: without it the **Access keys** page shows only
  "You do not have permission to list access keys."
- To limit the key to **One bucket**, `s3:ListBuckets` (the choice is offered only when you can
  list buckets or create one).
- The IAM user the key acts as is active, and has fewer active, unexpired keys than the
  `iam.access-keys` quota allows per user (see [Quotas](quotas.md)).

## Create the key

**Console**
1. Open **Identity**, then choose **Access keys**.
2. Choose **Create access key**.
3. (Optional) For **Acts as**, choose the IAM user the key acts as. It starts on you.
4. (Optional) For **Description**, enter ci-deploy.
5. (Optional) Under **Limited to**, instead of **No limit**:
   - choose **One bucket**, then the **Bucket**, **Read only** or **Read and write** under
     **Access**, and optionally a **Folder (prefix)**; or
   - choose **Custom policy** and paste a policy document into **Scope policy (JSON)**.
6. (Optional) Open **Expiry** and, for **Expires after (days)**, enter the key's lifetime. It
   starts on your organization's default and cannot exceed its maximum.
7. Choose **Create access key**.
8. Copy the **Key ID** and the **Secret**, then choose **I've saved the secret, dismiss**.

**CLI**
```sh
dcloud iam access-keys create --description ci-deploy --expires-in-days 30
```

**API**
```http
POST /iam/api/access-keys
{ "description": "ci-deploy", "expiresInDays": 30 }
```

**Terraform**
```hcl
resource "dcloud_iam_access_key" "ci" {
  iam_user_id     = dcloud_iam_user.ci.id
  description     = "ci-deploy"
  expires_in_days = 30
}
```

## Result

The secret is in the create response and on the console panel, and nowhere else: it cannot be read
again. With Terraform it is kept in the state, so protect the state as you would the key.

Use the pair as:

```http
Authorization: EWS-ACCESS-KEY EWS1EXAMPLEEXAMPLE00:<secret>
```

For a key without a bucket limit, the panel shows the same pair as a `docker login` line for the
container registry (the same pair works for git over HTTPS), as `dcloud auth login --with-key`
for the CLI, and as `aws configure set` lines for S3 clients. For a **One bucket** key it shows an
**AWS CLI profile** and an `aws s3 ls` line for that bucket instead.

- **Expiry.** Every new key expires. Without `expiresInDays` it gets the organization's default
  (90 days unless changed); a request may ask for 1 day up to the organization's maximum (365 days
  unless changed).
- **Scope.** With **One bucket**, **Custom policy** or `scopePolicy`, a request needs both the IAM
  user's policies and the scope to allow it, and a `Deny` in the scope always wins (see
  [Limit a key to one bucket](/docs/s3/guide/limit-a-key-to-one-bucket)).
- **Acting as another person.** A key for another person's own IAM user records everything done
  with it as that person. The console asks you to select **I create a key that acts as …**; the
  API needs `"actAsMember": true`. The audit log names you as the creator.

## If the key is refused

- 400 `expiresInDays is 400; this organization allows at most 365 days`.
- 400 `This key would act as …, a person in this organization: …` when `actAsMember` is missing.
- 400 `scopePolicy: …` when the scope is not a valid policy document.
- 404 `IAM user not found` for an `iamUserId` outside your organization.
- 409 `… is suspended (for example a former member) and cannot be given new access keys.`
- 409 `Quota exceeded: Access keys per user (iam.access-keys) — …` when the user has as many
  active keys as allowed. Deactivate or delete one first (see
  [Manage access keys](manage-access-keys.md)).

## Related

- [Manage access keys](manage-access-keys.md)
- [Sign in the CLI](sign-in-the-cli.md)
- [Web identity federation](web-identity-federation.md): credentials for CI jobs without a stored
  key.
- `iam.access-keys.create` · `dcloud iam access-keys create` · `dcloud_iam_access_key`
