-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#17 add handler with logic to create db
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import psycopg2 | ||
import boto3 | ||
|
||
def lambda_handler(event, context): | ||
# Retrieve database connection details from environment variables | ||
host = os.environ['DB_HOST'] | ||
port = os.environ['DB_PORT'] | ||
user = os.environ['DB_USER'] | ||
password = os.environ['DB_PASSWORD'] | ||
|
||
# Name of the new database to create | ||
new_db_name = 'ItemDB' | ||
|
||
try: | ||
# Connect to the default 'postgres' database | ||
conn = psycopg2.connect( | ||
host=host, | ||
port=port, | ||
user=user, | ||
password=password, | ||
database='postgres' | ||
) | ||
conn.autocommit = True | ||
|
||
# Create a cursor | ||
cur = conn.cursor() | ||
|
||
# Check if the database already exists | ||
cur.execute(f"SELECT 1 FROM pg_database WHERE datname = '{new_db_name}'") | ||
exists = cur.fetchone() | ||
|
||
if not exists: | ||
# Create the new database | ||
cur.execute(f'CREATE DATABASE "{new_db_name}"') | ||
print(f"Database '{new_db_name}' created successfully") | ||
else: | ||
print(f"Database '{new_db_name}' already exists") | ||
|
||
# Close the cursor and connection | ||
cur.close() | ||
conn.close() | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': f"Database operation for '{new_db_name}' completed successfully" | ||
} | ||
|
||
except Exception as e: | ||
print(f"Error: {str(e)}") | ||
return { | ||
'statusCode': 500, | ||
'body': f"An error occurred: {str(e)}" | ||
} |