We'll create a simple backend service that stores user data in an SQL database. We'll use CloudSQL-managed service solution from the Google Cloud Platform
- Golang Programming Language v1.19
- Echo Framework
- sqlc
- migrate
- terraform
- docker (compose)
- Create
.env
file and export it as environment variablescd ilt-4 cat << EOT > .env DB_USER="<your_db_username>" DB_PASSWORD="<your_db_password>" DB_HOST="localhost" #For development DB_HOST_COMPOSE="db" #For development on docker compose DB_PORT="5432" DB_NAME="<your_db_name>" POSTGRESQL_URL="postgres://<your_db_username>:<your_db_password>@<your_db_host>:5432/<your_db_name>?sslmode=disable" EOT export $(xargs < .env)
- Build a Docker image with this command
cd ilt-4 docker build -t <name_ilt e.g ilt4>:<ilt_version e.g 0.1.0> -f ./Dockerfile.prod .
- Run docker compose in
./build/docker-compose/
foldercd ilt-4/build/docker-compose docker compose up -d
- Migrate database schema
cd ilt-4 migrate -database $POSTGRESQL_URL -path migrations/migrate up
- Test the API with Postman
- Teardown docker compose and delete image
cd ilt-4/build/docker-compose docker compose down docker image rm <name_ilt e.g ilt4>:<ilt_version e.g 0.1.0>
- Push docker image from Development Step # 2 to Google Container Registry
docker tag <name_ilt e.g ilt4>:<ilt_version e.g 0.1.0> gcr.io/<your_project_id>/<name_ilt e.g ilt4>:<ilt_version e.g 0.1.0> docker push gcr.io/<your_project_id>/<name_ilt e.g ilt4>:<ilt_version e.g 0.1.0>
- Upload the migration schema that is located on
ilt-4/migrations/migrate/*.up.sql
to any Google Cloud Storage bucket - Update your
.env
file from Development Step # 1 with new envarscd ilt-4 cat << EOT >> .env PROJECT_ID="<your_gcp_project_id>" REGION="<your_prefered_region>" ILT_NAME="ilt-4" CLOUDSQL_NAME_PREFIX="<you_prefered_prefix>" SUBNET_IP_RANGE="11.1.0.0/16" PRIVATE_IP_FIRST_ADDRESS="11.255.0.0" SVPC_IP_RANGE="11.254.0.0/28" EOT export $(xargs < .env)
- Prepare the terraform envars
export GCR_VERSION="$(git describe --tags --abbrev=0 | cut -d '/' -f2)" export TF_VAR_project="$PROJECT_ID" export TF_VAR_region="$REGION" export TF_VAR_ilt_name="$ILT_NAME" export TF_VAR_image="gcr.io/$PROJECT_ID/$ILT_NAME:$GCR_VERSION" export TF_VAR_db_user="$DB_USER" export TF_VAR_db_password="$DB_PASSWORD" export TF_VAR_db_port="$DB_PORT" export TF_VAR_db_name="$DB_NAME" export TF_VAR_name_prefix="$CLOUDSQL_NAME_PREFIX" export TF_VAR_master_user_name="$DB_USER" export TF_VAR_master_user_password="$DB_PASSWORD" export TF_VAR_subnet_ip_range="$SUBNET_IP_RANGE" export TF_VAR_subnet_region="$REGION" export TF_VAR_private_ip_first_address="$PRIVATE_IP_FIRST_ADDRESS" export TF_VAR_svpc_connector_ip_range="$SVPC_IP_RANGE"
- Got to
ilt-4/infra/gcp-vpc
to create a new VPC network with a subnet, private service connection, and serverless VPC accessterraform apply # Then type yes
- Got to
ilt-4/infra/gcp-cloud-sql
to create a new CloudSQL instanceterraform apply # Then type yes
- After CloudSQL was created. Go to GCP Console > Cloud SQL > your_instance
- Click Import in the top ribbon. Select the migration schema that you uploaded to Google Cloud Storage. Select the
users
database. Then start the migration by clickingImport
- Got to
ilt-4/infra/gcp-cloud-run
to create a new Cloud Run containerterraform apply # Then type yes
- In the terraform output there will be a variable service_url that contains your API URL. You can test your API with it