DocumentDB API Example
Project description
Amazon DocumentDB Demo Construct
Installing
Pypi (Python)
pip install docdbdemo
npm (JavaScript and TypeScript)
npm install @richardhboyd/doc_db_lib
Usage
Python
from aws_cdk import (
core
)
from docdbdemo import DocDbLib
class MyStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
DocDbLib(self, "MyDocDBClient")
TypeScript
import cdk = require('@aws-cdk/core');
import docdbdemo = require('@richardhboyd/doc_db_lib');
export class DocDbClientTsStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const queue = new docdbdemo.DocDbLib(this, "MyDocDBClient");
}
}
Diagram
Interacting
Deploying the stack will create an Amazon DocumentDB Cluster with one instance in each availability zone created by the CDK's VPC Construct, a Lambda function that can put items into the database and fetch them out again, and an API to send requests to the Lambda Funciton.
The stack will output an APIGateway endpoint that will front the created DocumentDB.
Outputs:
DocDbClientTsStack.MyDocDBClientMyDocDbApiMyDocDBApiEndpoint74609B36 = https://gv8ooq7zti.execute-api.us-east-1.amazonaws.com/prod/
Let's look at how we use this new API.
Putting a document in the DocumentDB
API=https://gv8ooq7zti.execute-api.us-east-1.amazonaws.com/prod
curl -H "Content-Type: application/json" -d '{"bestCoder":"@allenmichael", "worstCoder":"singledigit"}' -X POST $API/document
Getting a document out of the database
API=https://gv8ooq7zti.execute-api.us-east-1.amazonaws.com/prod
curl -H "Content-Type: application/json" $API/document?bestCoder=@allenmichael
Testing this out we see
$ curl -H "Content-Type: application/json" -d '{"bestCoder":"@allenmichael", "worstCoder":"singledigit"}' -X POST $API/document
{"id": "5d7bd032c7866a33171a1261"}
$ curl -H "Content-Type: application/json" $API/document?bestCoder=@allenmichael
{'_id': ObjectId('5d7bcfe6c7866a33171a125f'), 'bestCoder': '@allenmichael', 'worstCoder': 'singledigit'}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
docdbdemo-0.1.0a4.tar.gz
(751.0 kB
view hashes)
Built Distribution
docdbdemo-0.1.0a4-py3-none-any.whl
(749.2 kB
view hashes)
Close
Hashes for docdbdemo-0.1.0a4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06e4e2ae1bc0d4c119148032a45faa7b0f11868f39c6bcfd59e6ac184bd64f24 |
|
MD5 | 25746ee3fcf96f7456341ed4629b4a1c |
|
BLAKE2b-256 | 9bbf22afd19ffe1f53a30fb5184173714312c78444e664463ad40611ccb6a379 |