I essentially have two parts to my code, both work on their own but not together. So I think I have a syntax issue.
The first part is creating the table, the second part is populating it. The issue is, both parts share the variable of the table name.
import os
import boto3
import botocore.session
region = os.environ.get('AWS_DEFAULT_REGION', 'us-east-2')
session = botocore.session.get_session()
dynamo = session.create_client('dynamodb', region_name=region)
s3 = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
obj = s3.get_object(Bucket=bucket, Key=key)
rows = obj['Body'].read().decode("utf-8"). split ('\n')
table = dynamodb.Table(key)
dynamodb.create_table(
TableName=key,
KeySchema=[
{
'AttributeName': 'first',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'last',
'KeyType': 'RANGE' #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'first',
'AttributeType': 'S'
},
{
'AttributeName': 'last',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# Wait for the table to exist before exiting
print('Waiting for', key, '...')
waiter = dynamo.get_waiter('table_exists')
waiter.wait(TableName=key)
with table.batch_writer() as batch:
for row in rows:
batch.put_item(Item={
'first':row.split(',')[0],
'last':row.split(',')[1],
'age':row.split(',')[2],
'date':row.split(',')[3]
})
This is running as a lambda function whenever a CSV is dropped into my s3 bucket.
After running, it succeeds in creating the table but does not populate it. Ending with : "Task timed out after 3.00 seconds" It starts again after a few seconds and returns "Table already exists", but remains empty.
If I run just the batch_writer part, it will populate the table as long as it already exists.