|
| 1 | +Running this sample with the managed YDB instance in Yandex Cloud |
| 2 | +--- |
| 3 | + |
| 4 | +(0) Install the required Python dependencies |
| 5 | + |
| 6 | +python3 -m pip install iso8601 |
| 7 | +python3 -m pip install 'ydb[yc]' |
| 8 | + |
| 9 | +(1) Install the yc command line tool |
| 10 | + |
| 11 | +https://cloud.yandex.ru/docs/cli/operations/install-cli |
| 12 | + |
| 13 | +(2) Create the service account using the YC Web Console, and assign it the ydb.editor role. |
| 14 | + |
| 15 | +Alternatively use the following shell snippet: |
| 16 | + |
| 17 | +```bash |
| 18 | +# Specify the Yandex Cloud folder and service account name |
| 19 | +export YC_FOLDER=mzinal |
| 20 | +export SA_NAME=ydb-sa-0 |
| 21 | +# Create the service account |
| 22 | +yc iam service-account create $SA_NAME |
| 23 | +# Obtain the service account id |
| 24 | +export SA_ID=`yc iam service-account get --name $SA_NAME | sed -n 's/^id: \(.*\)$/\1/p'` |
| 25 | +# Assign the ydb.editor role for the specified YC folder to the service account just created |
| 26 | +yc resource-manager folder add-access-binding $YC_FOLDER --role ydb.editor --subject serviceAccount:$SA_ID |
| 27 | +``` |
| 28 | + |
| 29 | +(3) Generate the service account key to be used for authentication. |
| 30 | + |
| 31 | +Note: unfortunately, right now YC Web Console does not offer a way to generate the SA key |
| 32 | +with its Web interface. |
| 33 | + |
| 34 | +```bash |
| 35 | +yc iam key create --service-account-name $SA_NAME --output $HOME/key-ydb-sa-0.json |
| 36 | +``` |
| 37 | + |
| 38 | +(4) Obtain the endpoint and database path from the Web Console. |
| 39 | + |
| 40 | +Alternatively, use the following command to grab the required data in the shell: |
| 41 | + |
| 42 | +```bash |
| 43 | +yc ydb db get --name ydb1 |
| 44 | +``` |
| 45 | + |
| 46 | +`ydb1` value in the command above is the logical name of the YDB managed database. |
| 47 | + |
| 48 | +(5) Run the sample: |
| 49 | + |
| 50 | +```bash |
| 51 | +# Set the path to the service account key file |
| 52 | +export YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS=$HOME/key-ydb-sa-0.json |
| 53 | +# Set the database path and its endpoint |
| 54 | +export YDB_ENDPOINT=grpcs://ydb.serverless.yandexcloud.net:2135 |
| 55 | +export YDB_DATABASE=/ru-central1/b1gfvslmokutuvt2g019/etnvbffeqegu1ub2rg2o |
| 56 | +# Run the script |
| 57 | +python3 __main__.py -e $YDB_ENDPOINT -d $YDB_DATABASE |
| 58 | +``` |
0 commit comments