-
Notifications
You must be signed in to change notification settings - Fork 320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Use IAM with RDS, increase number of backups and upgraded engine. #578
Open
charles-marion
wants to merge
2
commits into
aws-samples:main
Choose a base branch
from
charles-marion:rds_iam
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
91 changes: 91 additions & 0 deletions
91
lib/rag-engines/aurora-pgvector/functions/pg-setup/index.py
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,91 @@ | ||
import json | ||
import boto3 | ||
import psycopg2 | ||
import cfnresponse | ||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
from pgvector.psycopg2 import register_vector | ||
|
||
logger = Logger() | ||
secretsmanager_client = boto3.client("secretsmanager") | ||
|
||
|
||
@logger.inject_lambda_context(log_event=True) | ||
def lambda_handler(event, context: LambdaContext): | ||
request_type = event["RequestType"] | ||
resource_properties = event["ResourceProperties"] | ||
AURORA_DB_SECRET_ID = resource_properties["AURORA_DB_SECRET_ID"] | ||
|
||
secret_response = secretsmanager_client.get_secret_value( | ||
SecretId=AURORA_DB_SECRET_ID | ||
) | ||
database_secrets = json.loads(secret_response["SecretString"]) | ||
dbhost = database_secrets["host"] | ||
dbport = database_secrets["port"] | ||
dbuser = database_secrets["username"] | ||
dbpass = database_secrets["password"] | ||
|
||
if request_type == "Create" or request_type == "Update": | ||
dbconn = psycopg2.connect( | ||
host=dbhost, user=dbuser, password=dbpass, port=dbport, connect_timeout=10 | ||
) | ||
|
||
dbconn.set_session(autocommit=True) | ||
|
||
cur = dbconn.cursor() | ||
|
||
cur.execute("CREATE EXTENSION IF NOT EXISTS vector;") | ||
register_vector(dbconn) | ||
|
||
cur.execute("SELECT typname FROM pg_type WHERE typname = 'vector';") | ||
rows = cur.fetchall() | ||
|
||
for row in rows: | ||
logger.info(f"pg_type.typname: {row}") | ||
|
||
cur.execute( | ||
"SELECT extname, extversion FROM pg_extension WHERE extname = 'vector';" | ||
) | ||
rows = cur.fetchall() | ||
|
||
if len(rows) == 1: | ||
logger.info("Attempt upgrading vector extension") | ||
cur.execute("ALTER EXTENSION vector UPDATE;") | ||
|
||
# Set up IAM user | ||
# https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.DBAccounts.html#UsingWithRDS.IAMDBAuth.DBAccounts.PostgreSQL | ||
cur.execute( | ||
( | ||
"select pg_user.usename from pg_catalog.pg_user where " | ||
"pg_user.usename='aurora_db_iam_admin';" | ||
) | ||
) | ||
rows = cur.fetchall() | ||
if len(rows) == 0: | ||
# Should only run once | ||
cur.execute("CREATE USER aurora_db_iam_admin; ") | ||
cur.execute("CREATE USER aurora_db_iam_read; ") | ||
cur.execute("CREATE USER aurora_db_iam_write; ") | ||
cur.execute("GRANT rds_iam TO aurora_db_iam_admin; ") | ||
cur.execute("GRANT rds_iam TO aurora_db_iam_read; ") | ||
cur.execute("GRANT rds_iam TO aurora_db_iam_write; ") | ||
# Step functions need to create/delete tables on workspace change | ||
# Pre-defined roles | ||
# https://www.postgresql.org/docs/current/predefined-roles.html | ||
cur.execute("GRANT pg_read_all_data TO aurora_db_iam_admin; ") | ||
cur.execute("GRANT pg_write_all_data TO aurora_db_iam_admin; ") | ||
cur.execute("GRANT CREATE ON SCHEMA public TO aurora_db_iam_admin; ") | ||
# Adding documents requires write permissions | ||
cur.execute("GRANT pg_read_all_data TO aurora_db_iam_write; ") | ||
cur.execute("GRANT pg_write_all_data TO aurora_db_iam_write; ") | ||
# Quering the RAG only requires read operations | ||
cur.execute("GRANT pg_read_all_data TO aurora_db_iam_read; ") | ||
|
||
cur.close() | ||
dbconn.close() | ||
|
||
logger.info("Created vector extension and users") | ||
|
||
cfnresponse.send(event, context, cfnresponse.SUCCESS, {"ok": True}) | ||
|
||
return {"ok": True} |
53 changes: 0 additions & 53 deletions
53
lib/rag-engines/aurora-pgvector/functions/pgvector-setup/index.py
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to force running the script on existing environments.