Search code examples
pythonamazon-web-servicesamazon-dynamodbboto3amazon-cloudtrail

Load JSON data from CloudTrail into DynamoDB using Boto


I am working on a Boto3 script that can load the attributes from Cloudtrail into Dynamodb. The format of my cloudtrail logs is JSON. I am fairly new to DynamoDB and I am not sure where I am making a mistake. I'm trying to store "S3BucketName" as well as the name of the bucket which is "goodbucket3". Name for the cloudtrail is "GoodTrail".This is what I have come up with so far. I am getting this error "ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the PutItem operation: Requested resource not found"

import boto3

dynamodb = boto3.resource('dynamodb')

table =dynamodb.create_table(
    TableName='GoodTable',
    AttributeDefinitions=[
    {
      "AttributeName": "S3BucketName",
      "AttributeType": "S"
    }
    ],
      KeySchema=[
    {
      "AttributeName": "S3BucketName",
      "KeyType": "HASH"
    }
  ],
  ProvisionedThroughput={
    "ReadCapacityUnits": 1,
    "WriteCapacityUnits": 1
  }
)

table = dynamodb.Table('GoodTable')
response = table.put_item(
    Item= {
        'S3BucketName': 'some-bucket-name', 
        'content': f'{path}',
    }
)

Solution

  • Your table is just a dictionary. It is not dynamodb's table object.

    To rectify the issue:

    import boto3
    
    dynamodb_res = boto3.resource('dynamodb')
    
    table = dynamodb_res.Table('GoodTable')
    
    response = table.put_item(
        Item= {
            'S3BucketName': 'some-bucket-name', 
            'content': f'{path}',
        }
    )
    
    print(response)
    

    Based on your definition, you don't have any id. Your primary key in your table is S3BucketName, not id.