Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
"output_type": "stream",
"name": "stdout",
"text": [
"Enter your Elasticsearch Endpoint URL: https://your-first-elastic-agent-a4dc2b.es.us-central1.gcp.elastic.cloud:443\n",
"Enter your Elasticsearch API Key: bmU3SGRKa0JJdGhRdS1VdGJ5STg6a0ViYkZKcXA5ekx6LWNEZkR2TGxvZw==\n",
"Using connection details:\n",
"Elasticsearch URL: https://your-first-elastic-agent-a4dc2b.es.us-central1.gcp.elastic.cloud:443\n",
"Kibana URL: https://your-first-elastic-agent-a4dc2b.kb.us-central1.gcp.elastic.cloud:443\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# <font color='orange'>Create an Elastic Serverless Search Project Programmatically</font>\n",
"\n",
"\n",
"## This notebook will show you how to:\n",
" - Create an Elastics Serverless Project\n",
"\n",
"[Serverless API Docs](https://www.elastic.co/docs/api/doc/elastic-cloud-serverless/operation/operation-createelasticsearchproject#operation-createelasticsearchproject-body-application-json-optimized_for)\n",
"\n",
"This tutorial assumes you have an Elastic Cloud account.<br>\n",
"If not head over to [cloud.elastic.co/trial](https://cloud.elastic.co/trial)"
],
"metadata": {
"id": "_ebYbHHh_0hI"
}
},
{
"cell_type": "markdown",
"source": [
"# Notebook Setup\n",
"- Install elasticsearch python library\n",
"- import required libraries"
],
"metadata": {
"id": "JBHESuD7PJ54"
}
},
{
"cell_type": "code",
"source": [
"!pip install -qq elasticsearch"
],
"metadata": {
"id": "_DmXlQWsGNeM",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "6c04a6f7-a4e7-4e96-faaf-04d9bf2e9af7"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/960.5 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m952.3/960.5 kB\u001b[0m \u001b[31m28.8 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m960.5/960.5 kB\u001b[0m \u001b[31m17.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/65.3 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m65.3/65.3 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
]
},
{
"cell_type": "code",
"source": [
"import requests\n",
"import getpass\n",
"from pprint import pprint\n",
"from elasticsearch import Elasticsearch\n",
"from elasticsearch.exceptions import ConnectionTimeout\n",
"from IPython.display import clear_output\n",
"from time import sleep"
],
"metadata": {
"id": "cuomUVE-zYjB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Setup Your Cloud API Key\n",
"\n",
"1. Generate your secret API key at https://cloud.elastic.co/account/keys\n",
"2. Run the next cell to store your API Key"
],
"metadata": {
"id": "yWSg_D91x9mF"
}
},
{
"cell_type": "code",
"source": [
"api_key = getpass.getpass(\"Enter your API key: \")\n",
"\n",
"print(\"API key successfully entered!\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bidHlfsy2OPf",
"outputId": "33eceaab-1963-4c36-992a-ed96cd98904a"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter your API key: ··········\n",
"API key successfully entered!\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"## Create Elasticsearch project\n"
],
"metadata": {
"id": "mt4_kL0b0E75"
}
},
{
"cell_type": "markdown",
"source": [
"Configure:\n",
"- name of project - <font color='orange'>`name`</font>\n",
"- cloud region - <font color='orange'>`region_id`</font>\n",
"- headers\n",
"\n",
"Then create the project!"
],
"metadata": {
"id": "ZcQ2AgeNQHOA"
}
},
{
"cell_type": "code",
"source": [
"url = \"https://api.elastic-cloud.com/api/v1/serverless/projects/elasticsearch\"\n",
"\n",
"project_data = {\n",
" \"name\": \"Serverless Search Project\",\n",
" \"region_id\": \"aws-us-east-1\"\n",
"}\n",
"\n",
"auth_header = f\"ApiKey {api_key}\"\n",
"headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": auth_header\n",
" }\n",
"\n",
"es_project = requests.post(url, json=project_data, headers=headers)"
],
"metadata": {
"id": "lVkyA7KUyDEO"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Serverless projects generally start pretty quickly<br>We'll loop and wait for the project to be configured and ready to use"
],
"metadata": {
"id": "3oFstW-wQVWl"
}
},
{
"cell_type": "code",
"source": [
"if 200 <= es_project.status_code < 300:\n",
" es_project_keys = es_project.json()\n",
" prg_name = es_project_keys[\"name\"]\n",
" print(f\"Project {prg_name} creation started\")\n",
"\n",
" # wait for the project to be initialized and ready\n",
" project_id = es_project.json()[\"id\"]\n",
" print(\"Checking if project is created and ready\")\n",
" loop = 1\n",
" while True:\n",
" es_project_check = requests.get(url + f\"/{project_id}/status\", headers=headers)\n",
" if es_project_check.json()[\"phase\"] == \"initialized\":\n",
" break\n",
" else:\n",
" clear_output(wait=True)\n",
" print(\n",
" f\"Waiting for project to be ready. Current status:{es_project_check.json()['phase']} - Loop {loop} Sleeping 10 seconds\"\n",
" )\n",
" sleep(10)\n",
" loop += 1\n",
"\n",
" print(\"Project is ready\")\n",
"\n",
"else:\n",
" print(es_project.text)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jlOjE8l5QU91",
"outputId": "b7ecd36e-62df-4d29-e75f-121aa37e2d76"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Waiting for project to be ready. Current status:initializing - Loop 16 Sleeping 10 seconds\n",
"Project is ready\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Retrieve Serverless Project Connection Information"
],
"metadata": {
"id": "RogmJvonQnRu"
}
},
{
"cell_type": "markdown",
"source": [
"## Create elasticsearch client\n",
"\n",
"We set the connection information from the `es_project` response above."
],
"metadata": {
"id": "Uh0JpsnONMhv"
}
},
{
"cell_type": "code",
"source": [
"es = Elasticsearch(\n",
" es_project_keys[\"endpoints\"][\"elasticsearch\"],\n",
" basic_auth=(\n",
" es_project_keys[\"credentials\"][\"username\"],\n",
" es_project_keys[\"credentials\"][\"password\"],\n",
" ),\n",
")"
],
"metadata": {
"id": "KG01YrIwMdHz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Project API Key\n",
"Create a [Project level API key](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html)"
],
"metadata": {
"id": "Xhu2U-YszbDe"
}
},
{
"cell_type": "code",
"source": [
"project_key_response = es.security.create_api_key(\n",
" name=\"full_access_key\",\n",
" metadata={\"description\": \"API key for full access\"},\n",
" expiration=\"14d\",\n",
")\n",
"\n",
"project_api_key = project_key_response[\"encoded\"]\n",
"print(f\"{project_key_response['name']} has been created\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Puj1UWIKVtSv",
"outputId": "b195543a-2206-4a20-8d10-b80030d276de"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"full_access_key has been created\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"# Summary"
],
"metadata": {
"id": "EnKDbhz6RIYx"
}
},
{
"cell_type": "code",
"source": [
"print(\"\\n--- Project Summary ---\")\n",
"print(f\"Project Name: {es_project_keys['name']}\")\n",
"print(f\"Elasticsearch Endpoint: {es_project_keys['endpoints']['elasticsearch']}\")\n",
"if 'kibana' in es_project_keys['endpoints']:\n",
" print(f\"Kibana Endpoint: {es_project_keys['endpoints']['kibana']}\")\n",
"else:\n",
" print(\"Kibana Endpoint: Not available (Kibana may not be part of the serverless project)\")\n",
"\n",
"\n",
"print(\"\\n--- API Key ---\")\n",
"print(f\"API key stored in variable: project_api_key\")\n",
"\n",
"show_api_key = input(\"Do you want to print the API key in clear text? (yes/no): \").lower()\n",
"\n",
"if show_api_key == 'yes':\n",
" print(f\"Project API Key: {project_api_key}\")\n",
"else:\n",
" print(\"API key not printed in clear text as requested. Stored in `project_api_key`\")\n"
],
"metadata": {
"id": "ACDRmumLRGd_"
},
"execution_count": null,
"outputs": []
}
]
}

Loading