Skip to content

Commit

Permalink
Adding new advanced examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kernelpanek committed Aug 5, 2022
1 parent 7348dc7 commit a09342d
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 14 deletions.
114 changes: 114 additions & 0 deletions jupyterlab_advanced/aws_ec2_volumes.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "6759997c-519b-49e6-97c4-d286c178ac92",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"from pathlib import Path\n",
"\n",
"%run -i /notebooks/helper/aws.py\n",
"%run -i /notebooks/helper/aws_functions.py\n",
"aws_ctx = await aws_connect(\n",
" profiles=[\n",
" \"prod\",\n",
" ],\n",
" regions=[\n",
" \"us-east-1\",\n",
" \"us-west-1\",\n",
" \"ca-central-1\",\n",
" \"eu-west-1\",\n",
" \"eu-west-2\",\n",
" \"eu-central-1\",\n",
" \"ap-southeast-2\",\n",
" ],\n",
" svc_include=[\n",
" \"ec2\",\n",
" ],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e3275ad-4fcb-449e-904a-0e3c678e9fe9",
"metadata": {},
"outputs": [],
"source": [
"def get_volume_data(aws_ctx_obj, acct, region):\n",
" print(f\"Waiting on data from {acct}/{region}\")\n",
" volumes = retrieve_all_pages(aws_ctx_obj[acct][region][\"ec2\"], \"describe_volumes\").get(\"Volumes\", [])\n",
" print(f\" - Picked up data from {acct}/{region}\")\n",
" return dict(volumes=volumes)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8383e9aa-5ea7-4070-9b6f-b824508728e8",
"metadata": {},
"outputs": [],
"source": [
"all_ec2_volumes = perform_aws_operations(aws_ctx, get_volume_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56491e10-9233-40f3-9e69-380931fc84b1",
"metadata": {},
"outputs": [],
"source": [
"all_volumes = [{\"Account\": acct, \"Region\": reg_name, **vol} \n",
" for acct, reg_dict in all_ec2_volumes.items() \n",
" for reg_name, volumes in reg_dict.items() \n",
" for vol, volume_list in volumes.items()\n",
" for vol in volume_list]\n",
"total_volume_count = len(all_volumes)\n",
"total_volume_count"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96f7c621-92b6-4e13-9a2f-fb1b5d224dcd",
"metadata": {},
"outputs": [],
"source": [
"sum([v.get(\"Size\") for v in all_volumes])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67822ff1-0389-4d5b-8655-e8934ef428f9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
97 changes: 97 additions & 0 deletions jupyterlab_advanced/aws_ssm_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "013a0918-01b9-4ded-810b-c06d7c733e5c",
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"%run -i /notebooks/helper/aws.py\n",
"%run -i /notebooks/helper/aws_functions.py\n",
"aws_ctx = await aws_connect(\n",
" profiles=[\n",
" \"sandbox\",\n",
" ],\n",
" regions=[\n",
" \"us-east-1\",\n",
" ],\n",
" svc_include=[\n",
" \"ssm\",\n",
" ],\n",
")\n",
"\n",
"\n",
"def get_all_env_parameters(ssm_client, env_id):\n",
" pager = ssm_client.get_paginator(\"get_parameters_by_path\")\n",
" params_response = pager.paginate(\n",
" Path=f\"/DGC/{env_id}\", Recursive=True\n",
" ).build_full_result()\n",
" env_parameters = params_response.get(\"Parameters\")\n",
" decrypted_env_params = {\n",
" param.get(\"Name\"): ssm_client.get_parameter(\n",
" Name=param.get(\"Name\"), WithDecryption=True\n",
" )\n",
" .get(\"Parameter\", {})\n",
" .get(\"Value\")\n",
" for param in env_parameters\n",
" }\n",
" return decrypted_env_params"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47d5f3df-bf4e-4cfc-8c27-c860eef0f8f1",
"metadata": {},
"outputs": [],
"source": [
"env_params = get_all_env_parameters(aws_ctx.sandbox.us_east_1.ssm, \"1b3f969e-ed7c-5240-8a89-6ed164defe8e\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03f9af92-62de-4f54-a74f-3f2f3b1beb78",
"metadata": {},
"outputs": [],
"source": [
"html_src = \"\"\n",
"for name, value in env_params.items():\n",
" html_src += f\"<h4>{name}</h4><div>{value}</div>\"\n",
"display(HTML(html_src))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "49476bf2-d1dd-4b21-8fc5-c9ddcb3a4fcd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
4 changes: 2 additions & 2 deletions jupyterlab_advanced/aws_vpc_viz.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
},
"outputs": [],
"source": [
"%run -i helper/aws.py\n",
"%run -i helper/aws_functions.py\n",
"%run -i /notebooks/helper/aws.py\n",
"%run -i /notebooks/helper/aws_functions.py\n",
"aws_ctx = await aws_connect(\n",
" profiles=[\"sandbox\", \"prod\"], regions=AWS_ALL_REGIONS, svc_include=[\"ec2\"]\n",
")\n",
Expand Down
2 changes: 1 addition & 1 deletion jupyterlab_basics/cell_magics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
"metadata": {},
"outputs": [],
"source": [
"%run -i helper/aws.py"
"%run -i /notebooks/helper/aws.py"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion python_basics/001_data_type_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion python_basics/002_conditionals.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion python_basics/003_loops.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion python_basics/003_math.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion python_basics/004_functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion python_basics/005_exceptions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion python_basics/006_builtin_functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
6 changes: 4 additions & 2 deletions python_basics/007_string_formatting.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion python_basics/008_dictionaries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion python_basics/020_comprehensions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.10.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit a09342d

Please sign in to comment.