Skip to content

Instantly share code, notes, and snippets.

@lvthillo
Created March 18, 2020 16:37
Show Gist options
  • Save lvthillo/fe8e093ab7681c4c3ebb409d5b5aff29 to your computer and use it in GitHub Desktop.
Save lvthillo/fe8e093ab7681c4c3ebb409d5b5aff29 to your computer and use it in GitHub Desktop.
Part of load_data function
import os
import json
import boto3
import logging
from botocore.exceptions import ClientError
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def get_ddb_connection():
ENV = os.environ['Environment']
ddbclient=''
if ENV == 'local':
ddbclient = boto3.client('dynamodb', endpoint_url='http://dynamodb:8000/')
else:
ddbclient = boto3.client('dynamodb')
return ddbclient
def lambda_handler(event, context):
ddbclient = get_ddb_connection()
try:
response = ddbclient.batch_write_item(
RequestItems={
os.environ['DDBTableName']: [
{
'PutRequest': {
'Item': {
'documentId': {'N': '1043'},
'versionId': {'S': 'v_1'},
'location': {'S': 's3://bucket-a/6591636740.doc'}
}
}
},
...
]}
)
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': json.dumps({
'message': 'Filled DynamoDB',
}),
}
except ddbclient.exceptions.ResourceNotFoundException as e:
logging.error('Cannot do operations on a non-existent table')
raise e
except ClientError as e:
logging.error('Unexpected error')
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment