mohammedzeglam
I am trying deploy phoenix app to cloud run every thing works locally fine using public IP but when i deployed to cloud run the app could not connect to database
- Public IP
- Private IP
- Socket
with socket the error was
00:26:07.027 [error] Postgrex.Protocol (#PID<0.3424.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (<project_id>:<region>:<instance-name>/.s.PGSQL.5432): connection refused - :econnrefused
Cloud SQL connection failed. Please see https://cloud.google.com/sql/docs/mysql/connect-run for additional details: ensure that the account has access to "<project_id>:<region>:<instance-name>" (and make sure there's no typo in that name). Error during generateEphemeral for <project_id>:<region>:<instance-name>: googleapi: Error 403: boss::NOT_AUTHORIZED: Not authorized to access resource. Possibly missing permission cloudsql.instances.connect on resource instances/<instance-name>., forbidden
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
mstibbard
I had issues getting started with Cloud Run and Cloud SQL also. I wrote up the answers here: https://www.stibbard.io/elixir-phoenix-deploy-google-cloud-run-and-cloud-sql/
Aadmaa
Nice notes! Thanks for sharing.
rathoud96
This page is no longer working
mstibbard
I’ve rehosted it now: New link here.
Sharing it here for posterity too.
1. Prepare your application
Generate a Dockerfile and other useful release helpers.
Prepare database environment variables in
config/runtime.exs.Note:
Postgrex.start_link/1(docs) defaults to PostgreSQL environment variables (e.g.,PGHOST,PGDATABASE,PGUSER,PGPASSWORD) when no database connection details are specified.Cloud Run automatically generates a semi-randomised URL for your app (E.g.,
https://[SERVICE NAME]-[RANDOM NUMBERS].a.run.app). To prevent infinite reloading behaviour in LiveView we need to allow-list the Cloud Run origin inconfig/prod.exs.Note: Once you are up and running with your correct domain make sure you unwind this as accepting a wildcard origin is insecure
2. Create a GCP project
Note: Use environment variables so that most subsequent commands can be copy-pasted.
Create environment variables for your application name and preferred GCP region.
Create a project. Accept the auto-generated project ID at the prompt.
Create environment variables for your project ID and project number. It may take ~30 seconds before the project successfully returns with the
gcloud projects listcommand.Find the billing account you set up (refer to prerequisites).
Add your billing account from the prior command to the project.
3. Enable the services we need
Google Cloud disables all cloud products/services on a new project by default so we will need to enable all the services we will use for this deployment: Artifact Registry, Cloud Build, Cloud SQL, Secret Manager, Cloud Run, and the IAM API.
The following command will enable all the services we need.
4. Create an Artifact Registry repository to store our compiled app
Create a new repository with an identifier (I generally align this with my elixir app name) and specifying the format and region.
Create an environment variable to capture the repository’s
Registry URLwith the following command:5. Update service account permissions
Update the service account to have all necessary permissions.
roles/logging.logWriterpermissions are required by Cloud Buildroles/cloudsql.clientpermissions are required to interact with Cloud SQLroles/artifactregistry.writerpermissions are required to read/write to Artifact Registryroles/run.developerpermissions are required to deploy on Cloud Runroles/iam.serviceAccountUserpermissions are required to allow the Service Account to “act as” another service account and assign ownership of services (such as Cloud Run). In this case the account is acting as itself, but it is still required despite being self-referentialAbove can be added with the following commands:
6. Create a Cloud SQL database instance
Set up the environment variables for the next handful of steps.
The instance name must be composed of lowercase letters, numbers, and hyphens; must start with a letter.
Create a new PostgreSQL instance specifying your desired region, type of DB, and compute tier. This can take a few minutes to provision.
Note: We’ve used the lowest tier for this example, refer to the pricing.
Create a user for the application to use when interacting with the database.
Create the database.
Retrieve the instance
connectionNamefor later:7. Create environment variables in Secrets Manager
Define secret names for easy management. E.g., based on environment.
Create your application’s secrets on GCP. These are mapped to environment variables in the deployment step later.
Add permissions to the Service Account so it can access the secrets.
Retrieve the paths for the database user and password for use later:
8. Connect a GitHub repository to Cloud Build
This step and the next step are easier via Google Cloud Console > Cloud Build > Repositories.
Click “CREATE HOST CONNECTION” and populate the fields.
It will then take you through authentication with GitHub. You will have an option to provide access to all of your GitHub repositories or just a selection. Pick whatever makes sense for your needs.
After you have successfully created a connection, click “LINK A REPOSITORY”. Select the connection you just created and your Phoenix app repository. Choose generated repository names.
9. Create a Cloud Build trigger
Create a trigger via Google Cloud Console > Cloud Build > Triggers.
Click “CREATE TRIGGER” and populate with your desired details:
^main$/cloudbuild.yaml10. Create a build configuration file
In your Phoenix project’s root directory run the following command to create a
cloudbuild.yamlfile.This script creates a
cloudbuild.yaml(docs) that:MIX_ENV=prodwe are still interacting with the database we created previously via thePGDATABASEenvironment variablemix phx.gen.release --dockerPGHOST,PGDATABASE,PGUSER,PGPASSWORD)script:andarg:approaches we need to set theautomapSubstitutions: trueoption otherwise our builds will fail11. Trigger a deploy to Cloud Run
Commit the
cloudbuild.yamlfile (or any other change) and push it to your GitHub repository and watch it build. You can manually trigger builds via Google Cloud Console > Cloud Build > Triggers.You can view previous builds and stream in-progress builds on the Cloud Build History tab.
You should now have a fully deployed application on GCP!
If at any time you need to retrieve details of this service you can do so with the following command
12. (OPTIONAL) psql into Cloud SQL
To remotely connect to the Cloud SQL database you can use Cloud SQL Proxy. This securely connects via API to the database using your Google Cloud CLI credentials.
Download and install the Cloud SQL Proxy. Follow the instructions at the link.
Cloud SQL Proxy uses Google Cloud CLI credentials for auth, set them with:
Start the proxy using
connectionName. The port must not already be in use.If successful you will see see output similar to:
Now you can psql in!