﻿---
title: Connect to Elastic Cloud with the Elastic CLI
description: Connect the Elastic CLI to Elastic Cloud and run example operations against the Cloud API.
url: https://www.elastic.co/elastic/docs-builder/docs/4083/reference/elastic-cli/cli/manage-elastic-cloud
applies_to:
  - Elastic Cloud Serverless: Preview
  - Elastic Cloud Hosted: Preview
  - Elastic Cloud Enterprise: Unavailable
---

# Connect to Elastic Cloud with the Elastic CLI
The Elastic CLI exposes Elastic Cloud APIs for managing organizations, Elastic Cloud Hosted deployments, and Elastic Cloud Serverless projects. These APIs manage Cloud resources and their lifecycle; they are separate from the Elasticsearch and Kibana APIs used to work with data and solution features.
This guide assumes that you have already installed the CLI. Follow these steps to configure an Elastic Cloud connection, verify authentication, and run example operations against the Elastic Cloud API.

## Before you begin

You need:
- An [installed Elastic CLI](https://www.elastic.co/elastic/docs-builder/docs/4083/reference/elastic-cli/cli/installation).
- Access to an Elastic Cloud organization.
- An [Elastic Cloud API key](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/4083/deploy-manage/api-keys/elastic-cloud-api-keys) with Cloud API access and roles for the resources you want to manage.


## Add an Elastic Cloud context

The following steps create a context named `elastic-cloud` that connects to the public Elastic Cloud API.
If you use Bash or zsh, keep the API key out of your shell history by capturing it in a temporary variable:
```bash
read -rs ELASTIC_CLOUD_API_KEY
```

Paste your API key at the hidden prompt, press **Enter**, and create the context:
```bash
elastic config context add elastic-cloud \
  --cloud-url "https://api.elastic-cloud.com" \
  --cloud-api-key "$ELASTIC_CLOUD_API_KEY"
unset ELASTIC_CLOUD_API_KEY
```

The CLI stores the API key in your operating system's credential store when one is available. Otherwise, it stores the key in the configuration file and warns you. For other ways to provide and store secrets, refer to [external credentials](/elastic/docs-builder/docs/4083/reference/elastic-cli/cli/configuration#external-credentials).

## Verify the connection

Check connectivity and authentication for the new context:
```bash
elastic --use-context elastic-cloud status
```

A successful check displays a check mark next to `https://api.elastic-cloud.com`.
To make `elastic-cloud` the active context, run:
```bash
elastic config current-context set elastic-cloud
```

The remaining examples use `--use-context` explicitly, so changing the active context is optional.

## Run Elastic Cloud API operations

The following commands are representative examples, not a complete list of supported operations. The resources and operations available to you depend on the permissions granted to the Elastic Cloud API key in the selected context.
<warning>
  The creation examples are optional and can incur charges. Add `--dry-run` to a creation command to validate its input without creating a resource.
</warning>


### List Hosted deployments

List the Elastic Cloud Hosted deployments that your API key can access:
```bash
elastic --use-context elastic-cloud \
  cloud hosted deployments list-deployments
```

The response contains deployment identifiers, names, aliases, and resource details.

### List Serverless projects

List the Elastic Cloud Serverless search projects that your API key can access:
```bash
elastic --use-context elastic-cloud \
  cloud serverless projects search list
```

Replace `search` with `observability` or `security` to list another project type:
```bash
elastic --use-context elastic-cloud \
  cloud serverless projects observability list
```


### Create a Serverless project

To create a Serverless project and verify access, complete the following steps:
1. List the available Serverless regions:
   ```bash
   elastic --use-context elastic-cloud \
     cloud serverless regions list-regions
   ```
2. Create a Serverless project using one of the regions returned by the previous command. The following example creates an Observability project named `cli-example` in the AWS `us-east-1` region, identified as `aws-us-east-1` by Serverless:
   ```bash
   elastic --use-context elastic-cloud \
     cloud serverless projects observability create \
     --name "cli-example" \
     --region-id "aws-us-east-1" \
     --wait \
     --save-as "cli-example"
   ```
   The CLI asks you to confirm the operation. The `--wait` option waits for the project to initialize, and `--save-as` stores the project's Elasticsearch and Kibana endpoints and default credentials in a context named `cli-example`. Refer to [credential-safe project creation](/elastic/docs-builder/docs/4083/reference/elastic-cli/cli/configuration#credential-safe-project-creation) for more details.
   <note>
   For ongoing programmatic access, we recommend using an API key with only the permissions required for your use case instead of the default credentials. The Elastic Cloud API key used in this guide can also authenticate to the new project's Elasticsearch and Kibana APIs when it has **Cloud, Elasticsearch, and Kibana API** access and a role that applies to all projects of the relevant type. To use it, configure the project's endpoints and the same API key in the corresponding `elasticsearch` and `kibana` blocks of a context.
   </note>
3. Verify that the new context can access the project's Elasticsearch API:
   ```bash
   elastic --use-context cli-example es info
   ```
   A successful response confirms that the project is available and the saved credentials work.


### Create an Elastic Cloud Hosted deployment

To create an Elastic Cloud Hosted deployment, complete the following steps:
1. List the deployment templates available in your chosen region:
   ```bash
   elastic --use-context elastic-cloud \
     cloud hosted deployment-templates get-deployment-templates-v2 \
     --region "<region-id>" 
   ```
2. Create an Elastic Cloud Hosted deployment using one of the templates returned by the previous command. The following example creates a deployment named `cli-example` from the **General purpose** template in the AWS US East (N. Virginia) region (`us-east-1`):
   ```bash
   elastic --use-context elastic-cloud \
     cloud hosted deployments create-deployment \
     --name "cli-example" \
     --region "us-east-1" \
     --template-id "aws-general-purpose"
   ```
   The response contains the new deployment identifier and resource details. Refer to [Manage deployments using the Elastic Cloud API](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/4083/deploy-manage/deploy/elastic-cloud/manage-deployments-using-elastic-cloud-api#ec-api-examples-deployment-simple) for more information about regions, templates, and custom deployment configurations.
   <note>
   The Elastic Cloud API key used in this guide can't authenticate to Elasticsearch or Kibana APIs on Elastic Cloud Hosted. For ongoing programmatic access to the new deployment, create an [Elasticsearch API key](https://docs-v3-preview.elastic.dev/elastic/docs-builder/docs/4083/deploy-manage/api-keys/elasticsearch-api-keys) with only the permissions required for your use case. Configure the deployment's endpoints and API key in the corresponding `elasticsearch` and `kibana` blocks of a context. You can add these blocks to `elastic-cloud` or create a separate context for the deployment.
   </note>


## Next steps

- Run `elastic cloud --help`, `elastic cloud hosted --help`, or `elastic cloud serverless --help` to explore Cloud management commands.
- Use the [Elastic CLI command reference](https://www.elastic.co/elastic/docs-builder/docs/4083/reference/elastic-cli/cli) to inspect command options and input schemas.
- Use the [configuration guide](https://www.elastic.co/elastic/docs-builder/docs/4083/reference/elastic-cli/cli/configuration) to add Elasticsearch or Kibana to this context, or create a separate context for each environment.