Created
August 8, 2024 18:15
-
-
Save ivikash/8cd9bac45007779274d2ede3802d16bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import sys | |
import os | |
import logging | |
import boto3 | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
def lambda_handler(event, context): | |
try: | |
# Log the start of the function | |
logger.info("Starting lambda_handler") | |
# Install faker if not already installed | |
install_dir = '/tmp/python' | |
if not os.path.exists(install_dir): | |
logger.info("Installing faker library...") | |
os.makedirs(install_dir) | |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'faker', '-t', install_dir]) | |
# Add /tmp/python to the system path to import installed packages | |
sys.path.append(install_dir) | |
# Try to import the faker library | |
try: | |
logger.info("Importing faker library...") | |
import faker | |
except ImportError as e: | |
logger.error(f"Failed to import faker: {str(e)}") | |
return { | |
'statusCode': 500, | |
'body': "Internal Server Error: Could not import faker" | |
} | |
# Initialize Faker and DynamoDB | |
logger.info("Initializing Faker and DynamoDB...") | |
fake = faker.Faker() | |
dynamodb = boto3.resource('dynamodb') | |
table_name = 'dummy_table_name' # Replace with your table name | |
table = dynamodb.Table(table_name) | |
# Log table name | |
logger.info(f"DynamoDB table name: {table_name}") | |
# Loop to generate and insert 100 entries | |
for i in range(1000): | |
entry = { | |
"id": str(fake.uuid4()), # UUID for unique id | |
"name": fake.name(), | |
"address": fake.address().replace("\n", ", "), | |
"phone_number": fake.phone_number(), | |
"age": fake.random_int(min=18, max=90), | |
"gender": fake.random_element(elements=("Male", "Female", "Non-binary")), | |
"email": fake.email(), | |
"job": fake.job(), | |
"company": fake.company(), | |
"date_of_birth": fake.date_of_birth(tzinfo=None, minimum_age=18, maximum_age=90).isoformat(), | |
"ssn": fake.ssn(), | |
"text": fake.text(), | |
} | |
# Log the generated entry | |
logger.info(f"Inserting entry {i+1}: {entry}") | |
# Insert the record into DynamoDB | |
table.put_item(Item=entry) | |
# Return a success response | |
logger.info("All data inserted successfully") | |
return { | |
'statusCode': 200, | |
'body': "Successfully inserted 100 entries into DynamoDB" | |
} | |
except subprocess.CalledProcessError as e: | |
logger.error(f"Failed to install faker: {str(e)}") | |
return { | |
'statusCode': 500, | |
'body': "Internal Server Error: Could not install faker" | |
} | |
except Exception as e: | |
logger.error(f"An unexpected error occurred: {str(e)}") | |
return { | |
'statusCode': 500, | |
'body': "Internal Server Error" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment