'ResourceNotFoundException' when batch writing json items to a dynamoDB table

Viewed 13

When attempting to batch write 10 items to a dynamoDB table from boto3 python, I get this error when I attempt it: botocore.errorfactory.ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the PutItem operation: Requested resource not found

Here is my code for creating the table:

def create_table():
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.create_table(
        TableName='Courses',
        KeySchema=[
            {
                'AttributeName': 'CourseID',
                'KeyType': 'HASH'
            }
        ],
        AttributeDefinitions=[
            {
                'AttributeName': 'CourseID',
                'AttributeType': 'N'
            },
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 15,
            'WriteCapacityUnits': 15
        }
    )

and here is my code for batch loading the files:

def load_data():
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('Courses')

    with open('files/courses_data.json') as json_file:
        courses = json.load(json_file)
        print('Populating table...')
        for course in courses:
            CourseID= 1
            Subject= course['Subject']
            print(Subject)
            CatalogNbr= int(course['CatalogNbr'])
            print(CatalogNbr)
            Title= course['Title']
            print(Title)
            NumCredits= int(course['NumCredits'])
            print(NumCredits)
            
            print("Loading course: ",CourseID)
            
            table.put_item(
                Item={
                    'CourseID': CourseID,
                    'Subject': Subject,
                    'CatalogNbr': CatalogNbr,
                    'Title': Title,
                    'NumCredits': NumCredits
                }
            
            )
            CourseID = CourseID + 1

I am printing in between assignments to see if they are assigned correctly and it seems to go through one iteration of the for loop to where I correctly get my first item printed out. It seems like my issue is within the table_put.item block that comes right after the assingments, but I do not know what it is. Here is what prints to the terminal:

Populating table... CMIS 141 Introductory Programming 3 Loading course: 1

but after that loading course bit, I then get the crash.

Here is my JSON file as well:

[
{
  "Subject": "CMIS",
  "CatalogNbr": "141",
  "Title": "Introductory Programming",
  "NumCredits": 3
},
{"Subject": "CMIS", "CatalogNbr": "242", "Title": "Intermediate Programming", "NumCredits": 3},
{"Subject": "CMIS", "CatalogNbr": "320", "Title": "Relational Database Concepts and Applications", "NumCredits": 3},
{"Subject": "SDEV", "CatalogNbr": "300", "Title": "Building Secure Web Applications", "NumCredits": 3},
{"Subject": "SDEV", "CatalogNbr": "325", "Title": "Detecting Software Vulnerabilities", "NumCredits": 3},
{"Subject": "SDEV", "CatalogNbr": "350", "Title": "Database Security", "NumCredits": 3},
{"Subject": "SDEV", "CatalogNbr": "360", "Title": "Secure Software Engineering", "NumCredits": 3},
{"Subject": "SDEV", "CatalogNbr": "400", "Title": "Secure Programming in the Cloud", "NumCredits": 3},
{"Subject": "SDEV", "CatalogNbr": "425", "Title": "Mitigating Software Vulnerabilities", "NumCredits": 3},
{"Subject": "SDEV", "CatalogNbr": "460", "Title": "Software Security Testing", "NumCredits": 3},
{"Subject": "CMSC", "CatalogNbr": "495", "Title": "Current Trends and Projects in Computer Science", "NumCredits": 3}
]
0 Answers
Related