Skip to content

Instantly share code, notes, and snippets.

@nikhilkumarsingh
Last active May 7, 2021 23:40
Show Gist options
  • Save nikhilkumarsingh/7f4df786dbb8e2a90fd7d59e2dbcc755 to your computer and use it in GitHub Desktop.
Save nikhilkumarsingh/7f4df786dbb8e2a90fd7d59e2dbcc755 to your computer and use it in GitHub Desktop.
AWS DynamoDB Local
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# AWS DynamoDB Local\n",
"\n",
"![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/DynamoDB.png/220px-DynamoDB.png)\n",
"\n",
"## What?\n",
"\n",
"A **downloadable version** of Amazon DynamoDB using which you can develop and test applications without accessing the DynamoDB web service. Instead, the database is **self-contained** on your computer. \n",
"\n",
"When you're ready to deploy your application in production, you remove the local endpoint in the code, and then it points to the DynamoDB web service.\n",
"\n",
"\n",
"## Why?\n",
"\n",
"While developing an application:\n",
"\n",
"- save on throughput, data storage, and data transfer fees\n",
"- don't need an internet connection\n",
"\n",
"\n",
"## How?\n",
"\n",
"DynamoDB Local is available as \n",
"\n",
"- a download (requires JRE), \n",
"- an Apache Maven dependency, or \n",
"- a Docker image.\n",
"\n",
"\n",
"[Official Guide](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Setting up Docker\n",
"\n",
"![](https://docs.docker.com/engine/images/architecture.svg)\n",
"\n",
"## Install docker\n",
"\n",
"https://docs.docker.com/get-docker/\n",
"\n",
"- The **Docker daemon (dockerd)** listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.\n",
"\n",
"- The **Docker client (docker)** is the primary way that many Docker users interact with Docker. \n",
"\n",
"- A **Docker registry** stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default. \n",
"\n",
"- A **Docker image** is a read-only template with instructions for creating a Docker container. \n",
"\n",
"- A **Docker container** is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI.\n",
"\n",
">*A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.*\n",
"\n",
"\n",
"## Install docker-compose\n",
"\n",
"```bash\n",
"$ pip install docker-compose\n",
"```\n",
"\n",
"Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. \n",
"\n",
"- https://docs.docker.com/compose/\n",
"\n",
"- https://docs.docker.com/compose/compose-file/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Run dynamodb-local container\n",
"\n",
"Copy the following code to a file and save it as **docker-compose.yml**:\n",
"\n",
"```yml\n",
"version: '3.7'\n",
"services:\n",
" dynamodb-local:\n",
" image: amazon/dynamodb-local:latest\n",
" container_name: dynamodb-local\n",
" ports:\n",
" - \"8000:8000\"\n",
"```\n",
"\n",
"and run:\n",
"\n",
"```bash\n",
"docker-compose up\n",
"```\n",
"\n",
"Now, test via aws cli:\n",
"\n",
"```bash\n",
"$ aws dynamodb list-tables --endpoint-url http://localhost:8000\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\r\n",
" \"TableNames\": []\r\n",
"}\r\n"
]
}
],
"source": [
"!aws dynamodb list-tables --endpoint-url http://localhost:8000"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import boto3"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"ddb = boto3.resource(\"dynamodb\", endpoint_url=\"http://localhost:8000\")"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dynamodb.Table(name='students')"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddb.create_table(TableName='students',\n",
" AttributeDefinitions=[{\n",
" 'AttributeName': 'id',\n",
" 'AttributeType': 'S'\n",
" }],\n",
" KeySchema=[{\n",
" 'AttributeName': 'id',\n",
" 'KeyType': 'HASH'\n",
" }],\n",
" ProvisionedThroughput={\n",
" 'ReadCapacityUnits': 5,\n",
" 'WriteCapacityUnits': 5\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[dynamodb.Table(name='students')]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(ddb.tables.all())"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"table = ddb.Table('students')"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ResponseMetadata': {'RequestId': '279d305f-4723-43d5-8034-05b41f27f774',\n",
" 'HTTPStatusCode': 200,\n",
" 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.0',\n",
" 'x-amz-crc32': '2745614147',\n",
" 'x-amzn-requestid': '279d305f-4723-43d5-8034-05b41f27f774',\n",
" 'content-length': '2',\n",
" 'server': 'Jetty(8.1.12.v20130726)'},\n",
" 'RetryAttempts': 0}}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.put_item(Item={\n",
" 'id': '1',\n",
" 'name': 'nikhil'\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'Items': [{'name': 'nikhil', 'id': '1'}],\n",
" 'Count': 1,\n",
" 'ScannedCount': 1,\n",
" 'ResponseMetadata': {'RequestId': 'e8ddcd0c-9b29-4be6-bf09-35afcf529918',\n",
" 'HTTPStatusCode': 200,\n",
" 'HTTPHeaders': {'content-type': 'application/x-amz-json-1.0',\n",
" 'x-amz-crc32': '1519037507',\n",
" 'x-amzn-requestid': 'e8ddcd0c-9b29-4be6-bf09-35afcf529918',\n",
" 'content-length': '77',\n",
" 'server': 'Jetty(8.1.12.v20130726)'},\n",
" 'RetryAttempts': 0}}"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.scan()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pyenv37",
"language": "python",
"name": "pyenv37"
},
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment