Skip to content

本番環境

Kazane Shimizu edited this page Jan 21, 2025 · 36 revisions
いづれ CI/CDつくるまでの暫定運用

構成

構成の参考

5aaaf8b7-9705-1e3f-0eca-43c470f0be13

ただし

  • nginx は置いてない
  • プライベートサブネットに置くと OAuth 認証でインターネットにでられないためエラーになるが、NATゲートウェイが高いのでパブリックに置く運用にした

本番環境への手動デプロイ

  • ログイン
aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin 176340339620.dkr.ecr.ap-northeast-1.amazonaws.com
  • ビルド
docker-compose build
  • タグ付け
docker tag collections-app:latest 176340339620.dkr.ecr.ap-northeast-1.amazonaws.com/collections/prd/app:latest
  • プッシュ
docker push 176340339620.dkr.ecr.ap-northeast-1.amazonaws.com/collections/prd/app:latest
  • Docker イメージからビルドをコピー
docker create --name temp-container collections-app:latest
rm -rf ./sentry/dist && docker cp temp-container:/dist ./sentry/dist
docker rm temp-container
  • source map として Sentry にプッシュ
RELEASE_NAME="collections@x.x.x"
npx @sentry/cli releases new "$RELEASE_NAME"
npx @sentry/cli releases files "$RELEASE_NAME" upload-sourcemaps ./sentry --dist x.x.x --rewrite
npx @sentry/cli releases finalize "$RELEASE_NAME"

この辺参考

https://qiita.com/Uta_840/items/60eee4b09cecfc19592f https://qiita.com/kawatani32/items/b79da8af72fc4f0e7b31

ローカル Docker の証明書

test.com から localhost につなぐときに Cors エラーがでるため、ローカルは自己証明書を使う。 以下を参考 https://qiita.com/ohakutsu/items/814825a76b5299a96661

じゃ、はじめから localhost でいいじゃん。って思ったけど、auth.js の認証が通らない(セッショントークンが生成されない) https://github.com/nextauthjs/next-auth/issues/405

ローカルから ECS を経由して DB に接続する

参考 https://qiita.com/k_bobchin/items/a2b19ada7148607d70c8

execute-command を有効にする

aws ecs update-service \
    --cluster collections-prd-app-cluster \
    --service collections-prd-app-public-task \
    --enable-execute-command
    
※ 確認コマンド
aws ecs describe-services \
    --cluster collections-prd-app-cluster \
    --service collections-prd-app-public-task \
    --query "services[0].enableExecuteCommand"

ECS exec でコンテナにログイン

コンテナ
aws ecs execute-command \
  --cluster <クラスター名> \
  --task <タスクID> \
  --container <コンテナ名> \
  --interactive \
  --command "/bin/sh"
aws ecs execute-command \
  --cluster collections-prd-app-cluster \
  --task 060326c49ad2453e8d1ee0d40d9c10d1 \
  --container node \
  --interactive \
  --command "/bin/sh"

ECS exec で DBに接続する

テンプレ
aws ssm start-session \
        --target ecs:${CLUSTER_NAME}_${TASK_ID}_${RUNTIME_ID} \
        --parameters "{\"host\":[\"${RDS_HOST}\"],\"portNumber\":[\"${RDS_PORT}\"], \"localPortNumber\":[\"8888\"]}" \
        --document-name AWS-StartPortForwardingSessionToRemoteHost
aws ssm start-session \
        --target ecs:collections-prd-app-cluster_f0e53ca4572745bcb0a293064070ec49_f0e53ca4572745bcb0a293064070ec49-2982235661 \
        --parameters "{\"host\":[\"collections.cthpwuqq7fu6.ap-northeast-1.rds.amazonaws.com\"],\"portNumber\":[\"5432\"], \"localPortNumber\":[\"8888\"]}" \
        --document-name AWS-StartPortForwardingSessionToRemoteHost

直接 DB に入るとき

psql -h collections.cthpwuqq7fu6.ap-northeast-1.rds.amazonaws.com -U app -d collections

あとは、GUI ツールから閲覧する

参考

RLSのユーザー作成

ユーザー作成
CREATE USER app with password '***';
ALTER USER app CREATEDB;
GRANT ALL PRIVILEGES ON DATABASE collections TO app;
GRANT ALL PRIVILEGES ON SCHEMA public TO app;

データベース作成(appで入り直す)
psql -h collections.cthpwuqq7fu6.ap-northeast-1.rds.amazonaws.com -U app -d postgres
create database collections
Clone this wiki locally