> For the complete documentation index, see [llms.txt](https://docs.lenses.io/latest/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lenses.io/latest/devx/6.0/resources/cli/environment-creation.md).

# Environment Creation

Prerequisites:

* Running HQ;
* Enough permissions to create a new Service Account;
* Running Docker daemon.

{% stepper %}
{% step %}

###

1. Go to IAM section.<br>

   <figure><img src="/files/EFvodT9yIHoZC56BPLpN" alt=""><figcaption><p>Main screen</p></figcaption></figure>
2. Click the "Service Account" Tab;<br>

   <figure><img src="/files/nVb06t4QBJk8E0JJz4rC" alt=""><figcaption><p>IAM page</p></figcaption></figure>
3. Click on "New Service Account";<br>

   <figure><img src="/files/vNUNidcCaBhDs5L92T4N" alt=""><figcaption><p>Create new Service Account</p></figcaption></figure>
4. Fill in the mandatory fields. Make sure you assign a group with enough permissions to create new environments to the service account.<br>

   <div data-full-width="true"><figure><img src="/files/Y3U8JiSv8kbMqMTV7Z0Y" alt="" width="238"><figcaption></figcaption></figure></div>
5. Copy the service account key, which will be used later;<br>

   <figure><img src="/files/pNRcO37HdV5Az7zOo2gC" alt="" width="238"><figcaption></figcaption></figure>

   [More](/latest/devx/6.0/user-guide/iam/service-accounts.md) about service accounts.
   {% endstep %}

{% step %}

### Create a new environment

{% tabs %}
{% tab title="With CLI" %}
{% code title="terminal" %}

```bash
docker run -it lensesio/lenses-cli:6.0 --help \
 environments create \
 --name dev-env --tier development \ 
 --address [HQ_URL] \
 --token sa_key_*
```

{% endcode %}

Output:

{% code title="terminal" %}

```bash
{
  "name": "dev-env",
  "display_name": "dev-env",
  "lrn": "environments:environment:dev-env",
  "id": "env_s9UdVL2uHr7btloc",
  "created_at": "2024-11-27T09:55:23.263588Z",
  "tier": "development",
  "status": {
    "agent_connected": false
  },
  "agent_key": "agent_key_*"
}
```

{% endcode %}
{% endtab %}

{% tab title="With bash script" %}
An alternative method is to make an API call to HQ to create a new environment.&#x20;

{% code title="create\_new\_env.sh" %}

```bash
#!/bin/bash

# Variables for URLs and endpoints
URL_LOGIN="http://hq:9991/api/v1/login"
URL_DATA="http://hq:9991/api/v1/environments"

# Check if the file exists and is not empty
if grep -q '^agent' "$FILE"; then
    echo "[INFO] Environment has already been created -> Proceeding with the Agent creation"
    exit 0
else
  # Step 1: Perform the first request to get the session cookie
  # Replace the login payload and headers as per your requirements
  response=$(curl -s -D - \
    -X POST \
    -H 'Accept: application/json' \
    -H "Content-Type: application/json" \
    -d '{"username":"admin", "password":"admin"}' \
    $URL_LOGIN)

  # Step 2: Extract the session cookie from the response headers
  # Assuming the session cookie is called "session_id" in the response
  cookie=$(echo "$response" | grep -i 'Set-Cookie' | grep -o 'session_id=[^;]*')

  if [ -z "$cookie" ]; then
    echo "[ERROR] Failed to retrieve session cookie"
    exit 1
  fi

  echo "[INFO] Session Cookie: $cookie"

  # Step 3: Use the session cookie to make another request
  response_body=$(curl -s \
    -X POST \
    -H "Cookie: $cookie" \
    -H "Content-Type: application/json" \
    -d '{ "name": "demo-environment", "tier": "development" }' \
    $URL_DATA)

```

{% endcode %}
{% endtab %}
{% endtabs %}

{% endstep %}
{% endstepper %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.lenses.io/latest/devx/6.0/resources/cli/environment-creation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
