実現したいこと
cloud run 環境のRailsのAPI から Cloud SQLに接続したいです
発生している問題・分からないこと
課題
Cloud Run で Rails APIから Cloud SQLにアクセスする際に下記のエラーがでしまい原因がわかりません
`Exception: ActiveRecord::ConnectionNotEstablished: connection to server at \"xxxx.xx.xx.xxx\", port 5432 failed: Connection timed out\n\tIs the server running on that host and accepting TCP/IP connections?\n"`
環境
- Cloud Run でRails の実装
- Cloud SQLを postgresで作成
- パブリック IPで設定
Cloud Run で Rails APIから Cloud SQLにアクセスに必要だと思いやっていること
- デプロイ
gcloud builds submit --config cloudbuild.yaml --substitutions=_RAILS_ENV="production",_RAILS_MASTER_KEY="xxxxxx",_CLOUD_SQL_CONNECTION="xxxxxxx-419612:asia-northeast1:xxxxx-db",_DATABASE_HOST="xxxxx.xx.xxx.xx",_DATABASE_DB="xxxxx-db"",_DATABASE_USER="postgres",_DATABASE_PASSWORD="password",_CORS_ORIGINS="https://xxxxx"
-
cloud sqlの作成
- 接続はパブリックIPで作成
-
cloud build
--add-cloudsql-instances で cloud runと cloud sql の接続設定ができていると思っています
steps: - id: build name: 'gcr.io/kaniko-project/executor:latest' args: - --destination=asia-northeast1-docker.pkg.dev/$PROJECT_ID/xxxx/xxxxx-api - --destination=asia-northeast1-docker.pkg.dev/$PROJECT_ID/xxxx/xxxxx-api:latest - --build-arg=_RAILS_ENV=$_RAILS_ENV - --build-arg=_RAILS_MASTER_KEY=$_RAILS_MASTER_KEY - --build-arg=_CLOUD_SQL_CONNECTION=$_CLOUD_SQL_CONNECTION - --build-arg=_DATABASE_HOST=$_DATABASE_HOST - --build-arg=_DATABASE_DB=$_DATABASE_DB - --build-arg=_DATABASE_USER=$_DATABASE_USER - --build-arg=_DATABASE_PASSWORD=$_DATABASE_PASSWORD - --build-arg=_CORS_ORIGINS=$_CORS_ORIGINS - --cache=true - id: proxy waitFor: ['build'] name: 'gcr.io/cloudsql-docker/gce-proxy:1.16' args: - '/cloud_sql_proxy' - '-dir=/cloudsql' - '-instances=$_CLOUD_SQL_CONNECTION' - id: dockerize waitFor: ['build'] name: 'jwilder/dockerize' args: - '-wait' - 'unix:///cloudsql/$_CLOUD_SQL_CONNECTION/.s.PGSQL.5432' - '-timeout' - '30s' - id: migration waitFor: ['dockerize'] name: asia-northeast1-docker.pkg.dev/$PROJECT_ID/xxxx/xxxx-api entrypoint: bundle args: ['exec', 'rails', 'db:migrate'] env: - 'RAILS_ENV=$_RAILS_ENV' - 'DATABASE_DB=$_DATABASE_DB' - 'DATABASE_USER=$_DATABASE_USER' - 'RAILS_MASTER_KEY=$_RAILS_MASTER_KEY' - 'DATABASE_HOST=/cloudsql/$_CLOUD_SQL_CONNECTION' - 'DATABASE_PASSWORD=$_DATABASE_PASSWORD' - name: gcr.io/cloud-builders/docker waitFor: ['migration'] entrypoint: bash args: - -c - docker kill -s TERM $$(docker ps -q --filter ancestor=gcr.io/cloudsql-docker/gce-proxy:1.16) - id: 'deploy-api' name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' waitFor: ['migration'] entrypoint: gcloud args: - 'run' - 'deploy' - 'xxxx-api' - '--image' - 'asia-northeast1-docker.pkg.dev/$PROJECT_ID/xxxx/xxxx-api' - '--region' - 'asia-northeast1' - '--platform' - 'managed' - '--add-cloudsql-instances' - '$_CLOUD_SQL_CONNECTION' options: volumes: - name: cloudsql path: /cloudsql timeout: 900s
- database.yml
production: <<: *default database: <%= ENV.fetch('DATABASE_DB') %> host: <%= ENV.fetch('DATABASE_HOST') %> username: <%= ENV['DATABASE_USER'] %> password: <%= ENV['DATABASE_PASSWORD'] %>
エラーメッセージ
error
1`Exception: ActiveRecord::ConnectionNotEstablished: connection to server at \"xxxx.xx.xx.xxx\", port 5432 failed: Connection timed out\n\tIs the server running on that host and accepting TCP/IP connections?\n"` 2
該当のソースコード
def create user = User.new(user_params) if user.save render json: user, serializer: UserSerializer, status: :ok else # エラーメッセージをログに出力 Rails.logger.error user.errors.full_messages.to_sentence render json: {errors: {body: user.errors.full_messages}}, status: :unprocessable_entity end rescue StandardError => e # 例外の詳細をログに出力 Rails.logger.error "Exception: #{e.class.name}: #{e.message}" Rails.logger.error e.backtrace.join("\n") render json: {errors: {body: ["Exception: #{e.class.name}: #{e.message}"]}}, status: :internal_server_error end
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
Cloud Run で Rails APIから Cloud SQLにアクセスに必要だと思いやっていることに必要情報は記載しております
補足
特になし
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。