Skip to content

Commit

Permalink
Merge pull request #18 from localstack-samples/migrate-to-aws-endpoin…
Browse files Browse the repository at this point in the history
…t-url
  • Loading branch information
joe4dev authored Nov 28, 2023
2 parents af50aaf + 3d55a3b commit 7a8ec75
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions create-user.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
client = boto3.client('rds', endpoint_url=localstackHost)
sm = boto3.client('secretsmanager', endpoint_url=localstackHost)


def db_ops():
secret_arn = sm.list_secrets()["SecretList"][0]["ARN"]
secret = sm.get_secret_value(SecretId=secret_arn)
Expand Down
16 changes: 10 additions & 6 deletions rds/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import json
import boto3
import json
from os import environ
from urllib.parse import urlparse

import psycopg2


localstackHost = f"http://{environ.get('LOCALSTACK_HOSTNAME')}:{environ.get('EDGE_PORT')}"
client = boto3.client('rds', endpoint_url=localstackHost)
sm = boto3.client('secretsmanager', endpoint_url=localstackHost)
endpoint_url = environ.get("AWS_ENDPOINT_URL")
url = urlparse(endpoint_url)
host = url.hostname

client = boto3.client('rds', endpoint_url=endpoint_url)
sm = boto3.client('secretsmanager', endpoint_url=endpoint_url)


def db_ops():
secret_arn = sm.list_secrets()["SecretList"][0]["ARN"]
Expand All @@ -18,7 +23,7 @@ def db_ops():
try:
# create a connection object
connection = psycopg2.connect(
host=environ.get('LOCALSTACK_HOSTNAME'),
host=host,
database="mylab",
user=username,
password=password,
Expand All @@ -41,4 +46,3 @@ def lambda_handler(event, context):
'statusCode': 200,
'body': json.dumps(results, default=str)
}

16 changes: 10 additions & 6 deletions rdsproxy/app.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import json
import boto3
from os import environ
from urllib.parse import urlparse

import psycopg2

localstackHost = f"http://{environ.get('LOCALSTACK_HOSTNAME')}:{environ.get('EDGE_PORT')}"
client = boto3.client('rds', endpoint_url=localstackHost) # get the rds object

endpoint_url = environ.get("AWS_ENDPOINT_URL")
url = urlparse(endpoint_url)
hostname = url.hostname

client = boto3.client('rds', endpoint_url=endpoint_url) # get the rds object


def create_proxy_connection_token(username):
# get the required parameters to create a token
region = environ.get('region') # get the region
hostname = environ.get('LOCALSTACK_HOSTNAME') # get the rds proxy endpoint
port = 4510 # get the database port
region = environ.get('region')
port = 4510 # database port

# generate the authentication token -- temporary password
token = client.generate_db_auth_token(
Expand All @@ -33,7 +37,7 @@ def db_ops():
try:
# create a connection object
connection = psycopg2.connect(
host=environ.get('LOCALSTACK_HOSTNAME'),
host=hostname,
database="mylab",
user=username,
password=token,
Expand Down

0 comments on commit 7a8ec75

Please sign in to comment.