Skip to content

Instantly share code, notes, and snippets.

@koliadych
Created May 10, 2026 12:42
Show Gist options
  • Select an option

  • Save koliadych/897cf1724843eda69d87f156af07d8c4 to your computer and use it in GitHub Desktop.

Select an option

Save koliadych/897cf1724843eda69d87f156af07d8c4 to your computer and use it in GitHub Desktop.
fintechner tokyo-solo apply
#!/bin/bash
# fintechner — apply tokyo-solo terraform plan and seed the DB secret.
set -euo pipefail
TFDIR=$HOME/work/fintechner/infra/envs/tokyo-solo
REGION=ap-northeast-1
if [ ! -f "$TFDIR/plan.bin" ]; then
echo "ERROR: $TFDIR/plan.bin not found. Run 02-tf-plan.sh first."
exit 1
fi
cd "$TFDIR"
# Skip apply if already applied (idempotent re-run).
if terraform state list 2>/dev/null | grep -q aws_instance.this; then
echo "EC2 already exists in state — skipping apply, just printing outputs."
else
echo "=== terraform apply (creates AWS resources, ~2-3 min) ==="
terraform apply -no-color plan.bin
fi
# Seed DB password if Secrets Manager doesn't yet have a value.
if ! aws --region "$REGION" secretsmanager get-secret-value \
--secret-id fintechner/db-password >/dev/null 2>&1; then
echo
echo "=== Seeding DB password (random 32-char, never logged) ==="
DB_PWD=$(openssl rand -base64 32 | tr -d '/+=\n' | head -c 32)
aws --region "$REGION" secretsmanager put-secret-value \
--secret-id fintechner/db-password \
--secret-string "$DB_PWD" >/dev/null
echo " done."
fi
EIP=$(terraform output -raw public_ip)
URL=$(terraform output -raw dashboard_url)
INSTANCE=$(terraform output -raw instance_id)
echo
echo "============================================================"
echo " APPLY COMPLETE — your AWS resources are running"
echo "============================================================"
echo " Public IP: $EIP"
echo " Dashboard URL: $URL"
echo " Instance ID: $INSTANCE"
echo
echo "Cost is now ~\$0.50/day until you destroy with 'terraform destroy'."
echo
echo " Open shell on the box (no SSH key, no port 22):"
echo " aws --region $REGION ssm start-session --target $INSTANCE"
echo
echo "Paste the 3 lines above (Public IP, Dashboard URL, Instance ID) back to me."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment