Skip to content

Commit

Permalink
BFD-2935: Update BFD Server to use default RDS reader endpoint (#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
malessi authored Sep 25, 2023
1 parent dcf10f0 commit b08a750
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ locals {
env = terraform.workspace

# When the CustomEndpoint is empty, fall back to the ReaderEndpoint
rds_reader_endpoint = data.external.rds.result["CustomEndpoint"] == "" ? data.external.rds.result["ReaderEndpoint"] : data.external.rds.result["CustomEndpoint"]
rds_reader_endpoint = data.external.rds.result["ReaderEndpoint"]

additional_tags = { Layer = var.layer, role = var.role }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#######################################
# Results in two calls to the AWS RDS APIs for describe-db-clusters and describe-db-instances.
# Returns a well-formatted json object including the following keys:
# "DBClusterIdentifier", "Endpoint", "ReaderEndpoint", "WriterAZ", "WriterNode" and "CustomEndpoint".
# "DBClusterIdentifier", "Endpoint", "ReaderEndpoint", "WriterAZ", and "WriterNode".
#
# This exists to accommodate the desire for placing write-intensive workloads in the same AZ as the
# writer node. As of May 2022, the data source for DB instances does not expose the `IsClusterWriter`
Expand All @@ -12,34 +12,26 @@
#
# Globals:
# CLUSTER_IDENTIFIER mapped to the "$1" positional argument
# BFD_ENV mapped to the "$2" positional argument
# CLUSTER a modified json object from the aws rds describe-db-clusters command
# WRITER_NODE a string representing the writer node's db-instance-identifier
# WRITER_AZ a string representing the writer node's availability zone
# WRITER_CONFIG a json object including writier-specific keys
# WRITER_CONFIG a json object (as a string) including writer-specific keys
#
# Arguments:
# $1 maps to the CLUSTER_IDENTIFIER
#######################################
set -euo pipefail

CLUSTER_IDENTIFIER="$1"
BFD_ENV="$2"

ENDPOINT_IDENTIFIER="bfd-${BFD_ENV}-ro"

CLUSTER="$(aws rds describe-db-clusters \
--query 'DBClusters[].{DBClusterIdentifier:DBClusterIdentifier,Endpoint:Endpoint,ReaderEndpoint:ReaderEndpoint,Members:DBClusterMembers}[0]' \
--db-cluster-identifier "$CLUSTER_IDENTIFIER")"

CUSTOM_ENDPOINT="$(aws rds describe-db-cluster-endpoints --db-cluster-identifier "$CLUSTER_IDENTIFIER" | jq -r --arg endpoint "$ENDPOINT_IDENTIFIER" '.[][] | select(.DBClusterEndpointIdentifier == $endpoint).Endpoint')"
--query 'DBClusters[].{DBClusterIdentifier:DBClusterIdentifier,Endpoint:Endpoint,ReaderEndpoint:ReaderEndpoint,Members:DBClusterMembers}[0]' \
--db-cluster-identifier "$CLUSTER_IDENTIFIER")"

WRITER_NODE="$(jq -r '.Members[] | select(.IsClusterWriter == true) | .DBInstanceIdentifier' <<<"$CLUSTER")"

WRITER_AZ="$(aws rds describe-db-instances --db-instance-identifier "$WRITER_NODE" --query 'DBInstances[0].AvailabilityZone' --output text)"

WRITER_CONFIG="$(jq --null-input --arg writer_node "$WRITER_NODE" --arg writer_az "$WRITER_AZ" '{ WriterNode: $writer_node, WriterAZ: $writer_az }')"

# Construct the final object keeping just "DBClusterIdentifier", "Endpoint", "ReaderEndpoint", "WriterAZ" from the $CLUSTER
# Add the objects representing "WriterNode" and "CustomEndpoint"
jq --argjson obj1 "$WRITER_CONFIG" --arg str1 "$CUSTOM_ENDPOINT" '. += $obj1 | . += { "CustomEndpoint": $str1 } | del(.Members)' <<<$CLUSTER
jq --argjson obj "$WRITER_CONFIG" '. += $obj | del(.Members)' <<<$CLUSTER

0 comments on commit b08a750

Please sign in to comment.