Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Python - DynamoDB table Insert

Hi Experts,


I would like the Python code to update the DynamoDB table called - inventTest 

 

=============================================================

import json
 import boto3
 import logging
 import os
 import botocore
 from datetime import datetime
 import traceback
 import time
 import sys, subprocess, boto3
 import re
 import io
 import csv
 from botocore.exceptions import ClientError
 from boto3.dynamodb.conditions import Key, Attr
 
 def getInstanceDetails(event, context):
 
     dynamodb = boto3.client('dynamodb', region_name='eu-west-2')
     table = dynamodb.Table('invenTest')
     
     dynamodb.put_item(TableName='invesTest', Item={'instanceId':{'S':'m},'key2':{'N':'value2'}})

 

========================================================

 

dynamoDB invenTest Table has the following columns

 

* InstanceId = key id

* account Id

* subnetId

*VpcId

 Any idea or Direction much appreciated

Avatar of Adelaido Jimenez
Adelaido Jimenez
Flag of United States of America image

How are you getting the items that are going to get inserted into the DynamoDB table? Is it a json file your reading?

Here is an example code from amazon using python to interact with dynamoDB
from decimal import Decimal
import json
import boto3


def load_movies(movies, dynamodb=None):
    if not dynamodb:
        dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")

    table = dynamodb.Table('Movies')
    for movie in movies:
        year = int(movie['year'])
        title = movie['title']
        print("Adding movie:", year, title)
        table.put_item(Item=movie)


if __name__ == '__main__':
    with open("moviedata.json") as json_file:
        movie_list = json.load(json_file, parse_float=Decimal)
    load_movies(movie_list)


Open in new window

If you want to see the full demo you can check out their docs here https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.02.html#GettingStarted.Python.02.01

I would also recommend checking out their SDK for python (Boto3) they have code snippets of the functions.
 https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html

Avatar of ukerandi

ASKER

@Adelaido Jimenez 
Thank you very much for reply
How to connect with endpoint_url  , where i can use username and password
Any tutorial or code much appriciated
ASKER CERTIFIED SOLUTION
Avatar of Adelaido Jimenez
Adelaido Jimenez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial