Home > Back-end >  Mutate table in DynamoDB without using boto3
Mutate table in DynamoDB without using boto3

Time:07-27

I am trying to create a Python project that is able to both query and update tables in my AWS Dynamo database.

My query is as follows:

query = """query {
    listTodos {
    nextToken
    startedAt
    items {
      createdAt
      name
      description
    }
  }
}"""


Headers = {'X-API-KEY': 'myKey'}
url = 'https://example.amazonaws.com/graphql'
r = requests.post(url, json={'query': query}, headers=Headers)
print(r.status_code)
print(r.text)

I get an expected 200 response that gives me the name, createdAt (time), and description.

What I am trying to do is pass in a mutation, but there is no documentation for this when not using boto3. For simplicity, I am curious to know how I would mutate this table while just using requests.post

I have tried the following with no success:

query = """
    mutation {
        updateTodo (id: 80e1a712-0053-436c-ba73-9182a7cca67f, name: "Paul") {
            name
        }
    }
"""

From this I recieve MalformedHttpRequestException

What is the best way to accomplish this? Thank you in advance, as I am new to the AWS platform.

CodePudding user response:

In a blog post I once compared the DynamoDB request protocol to another protocol, CQL. One of my observations was that one of the most "obvious" advantages of the DynamoDB protocol is that it uses the standard HTTP and JSON for requests, so at first glance it appears as if it is trivial to write a DynamoDB client in any language that has an HTTP request library - without needing a complex AWS-specific library like boto3.

So, why do people end up using boto3 (for Python) anyway? Well, the main reason is the need to sign requests. You didn't say why your attempts were not successful (what error did you get?) but if I ignore for a moment the fact that your request doesn't look like a valid DynamoDB request, the next problem you'd encounter is Amazon complaining that your request is not signed. You need to digitally sign each and every request using your AWS id and secret key, to prove who you are and allow Amazon to charge you for the request. Doing this without a library is very possible (I have done it), but not trivial. It's much easier to just use a library, like boto3. Why are you trying to avoid it?

If you are really serious about avoiding boto3 (or other alternative libraries) and implementing the protocol yourself, please take a look at Amazon's documentation on the low-level API for more details on DynamoDB's request format and the signature implementation.

  • Related