-
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.
* Allow multiple RDS instances * Update the test example * PR reviews updates * Note that the instance needs to be a writer * Set target group port to RDS instance port * Fix a typo * Change lambda status if update fails * Add source_code_hash * Terraform format
- Loading branch information
1 parent
7fad4e0
commit 8878b26
Showing
10 changed files
with
146 additions
and
138 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
output "rds_instance_endpoint" { | ||
value = module.rds_postgres.rds_instance.endpoint | ||
value = module.rds_postgres.rds_instance.endpoint | ||
sensitive = true | ||
} | ||
|
||
output "mz_rds_details" { | ||
value = module.rds_postgres.mz_rds_details | ||
value = module.rds_postgres.mz_rds_details | ||
sensitive = true | ||
} |
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 |
---|---|---|
@@ -1,57 +1,60 @@ | ||
import boto3 | ||
import socket | ||
import os | ||
import json | ||
|
||
# Define the clients at the top of your function | ||
# Initialize clients | ||
elbv2_client = boto3.client('elbv2') | ||
rds_client = boto3.client('rds') | ||
|
||
RDS_IDENTIFIER = os.environ['RDS_IDENTIFIER'] # RDS instance identifier | ||
TARGET_GROUP_ARN = os.environ['TARGET_GROUP_ARN'] # Target Group ARN | ||
# Load RDS details from environment variables | ||
RDS_DETAILS = json.loads(os.environ['RDS_DETAILS']) | ||
|
||
def update_target_registration(rds_identifier, details): | ||
try: | ||
# Retrieve the current IP address of the RDS instance | ||
rds_instances = rds_client.describe_db_instances(DBInstanceIdentifier=rds_identifier) | ||
rds_port = rds_instances['DBInstances'][0]['Endpoint']['Port'] | ||
if not rds_instances['DBInstances']: | ||
raise Exception(f"No instances found for {rds_identifier}") | ||
|
||
def lambda_handler(event, context): | ||
# Retrieve the current IP address of the RDS instance | ||
rds_instances = rds_client.describe_db_instances( | ||
DBInstanceIdentifier=RDS_IDENTIFIER) | ||
rds_endpoint = rds_instances['DBInstances'][0]['Endpoint']['Address'] | ||
ip_address = socket.gethostbyname(rds_endpoint) | ||
rds_port = rds_instances['DBInstances'][0]['Endpoint']['Port'] | ||
|
||
# Retrieve the existing target of the target group | ||
targets = elbv2_client.describe_target_health( | ||
TargetGroupArn=TARGET_GROUP_ARN) | ||
|
||
# Get the current IP address in the target group | ||
if targets['TargetHealthDescriptions']: | ||
current_ip = targets['TargetHealthDescriptions'][0]['Target']['Id'] | ||
else: | ||
current_ip = None | ||
|
||
# If the IP addresses don't match, update the target group | ||
if current_ip and current_ip != ip_address: | ||
# Deregister the current target | ||
elbv2_client.deregister_targets( | ||
TargetGroupArn=TARGET_GROUP_ARN, | ||
Targets=[ | ||
{ | ||
'Id': current_ip | ||
}, | ||
] | ||
) | ||
rds_endpoint = rds_instances['DBInstances'][0]['Endpoint']['Address'] | ||
ip_address = socket.gethostbyname(rds_endpoint) | ||
|
||
# Retrieve the existing target of the target group | ||
target_group_arn = details['target_group_arn'] | ||
targets = elbv2_client.describe_target_health(TargetGroupArn=target_group_arn) | ||
|
||
# Check and update the target group | ||
current_ip = targets['TargetHealthDescriptions'][0]['Target']['Id'] if targets['TargetHealthDescriptions'] else None | ||
if current_ip != ip_address: | ||
if current_ip: | ||
# Deregister the current target | ||
elbv2_client.deregister_targets(TargetGroupArn=target_group_arn, Targets=[{'Id': current_ip}]) | ||
|
||
# Register the new target | ||
elbv2_client.register_targets( | ||
TargetGroupArn=TARGET_GROUP_ARN, | ||
Targets=[ | ||
{ | ||
'Id': ip_address, | ||
'Port': rds_port | ||
}, | ||
] | ||
) | ||
elbv2_client.register_targets(TargetGroupArn=target_group_arn, Targets=[{'Id': ip_address, 'Port': rds_port}]) | ||
message = f"Target group {target_group_arn} updated. New target IP: {ip_address}" | ||
else: | ||
message = f"Target group {target_group_arn} already up to date. Current target IP: {ip_address} and Port: {rds_port}" | ||
|
||
return {'success': True, 'message': message} | ||
except Exception as e: | ||
return {'success': False, 'message': f"Failed to update targets for {rds_identifier} with error: {e}"} | ||
|
||
def lambda_handler(event, context): | ||
update_messages = [] | ||
all_success = True | ||
|
||
for rds_identifier, details in RDS_DETAILS.items(): | ||
result = update_target_registration(rds_identifier, details) | ||
update_messages.append(result['message']) | ||
if not result['success']: | ||
all_success = False | ||
|
||
status_code = 200 if all_success else 500 | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': f'Target group updated. Current target IP: {ip_address}' | ||
'statusCode': status_code, | ||
'body': json.dumps(update_messages) | ||
} |
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.