diff --git a/create_user_lambda.tf b/create_user_lambda.tf new file mode 100644 index 0000000..e69de29 diff --git a/create_user_ssm_parameter.tf b/create_user_ssm_parameter.tf new file mode 100644 index 0000000..206d64e --- /dev/null +++ b/create_user_ssm_parameter.tf @@ -0,0 +1,15 @@ + +# Read the JSON file +#https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file +data "local_file" "user_list" { + filename = "${path.module}/user_list.json" +} + +# Create SSM Parameter +#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_parameter +resource "aws_ssm_parameter" "user_list" { + name = "/${var.name}/db_user_list" # Replace with your desired parameter name + description = "User and database mappings for Amazon RDS for PostgreSQL DB users." + type = "String" + value = data.local_file.user_list.content +} \ No newline at end of file diff --git a/manage_db_user/handler.py b/manage_db_user/handler.py new file mode 100644 index 0000000..c697480 --- /dev/null +++ b/manage_db_user/handler.py @@ -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)}" + } diff --git a/provider.tf b/provider.tf index 08f537b..98e9485 100644 --- a/provider.tf +++ b/provider.tf @@ -4,6 +4,10 @@ terraform { source = "hashicorp/aws" version = "5.63.1" } + local = { + source = "hashicorp/local" + version = "2.5.2" + } random = { source = "hashicorp/random" version = "3.6.2" @@ -21,6 +25,9 @@ provider "aws" { } } } +provider "local" { + # Configuration options +} provider "random" { # Configuration options } \ No newline at end of file diff --git a/user_list.json b/user_list.json new file mode 100644 index 0000000..30d92ea --- /dev/null +++ b/user_list.json @@ -0,0 +1,19 @@ +{ + "user_database_mappings": [ + { + "username": "user1" + }, + { + "username": "user2", + "database": "postgres" + }, + { + "username": "user3", + "database": "db1" + }, + { + "username": "user4", + "database": "db3" + } + ] +}