#!/usr/bin/env bash # Run interactively on C1. psql prompts once for the RDS owner's password. # The EC2 role can read the application password, never the owner's secret. set -euo pipefail umask 077 bucket=${1:?Usage: bash init-database.sh BUCKET REGION} region=${2:?Supply the deployment region as the second argument.} dnf -qy install postgresql16 jq scratch=$(mktemp -d) trap 'rm -rf "$scratch"' EXIT cd "$scratch" aws s3 cp "s3://$bucket/bootstrap/settings.json" settings.json --region "$region" --only-show-errors export PGDATABASE=xyops PGSSLMODE=verify-full PGSSLROOTCERT="$scratch/rds-ca.pem" PGHOST=$(jq -er .DBHost settings.json) export PGHOST curl -fsS --retry 3 "https://truststore.pki.rds.amazonaws.com/$region/$region-bundle.pem" -o "$PGSSLROOTCERT" secret_id=$(jq -er .AppSecret settings.json) password=$(aws secretsmanager get-secret-value --region "$region" --secret-id "$secret_id" --query SecretString --output text) # The fleet template generates 48 alphanumeric characters. Validate this before # using the value as a SQL literal. Avoid putting any password on a command line. [[ $password =~ ^[a-zA-Z0-9]{48}$ ]] PGUSER=dbowner psql -X -W --quiet --set=ON_ERROR_STOP=1 <