graph LR;
A[Compute Instance] <--> B[VPC Peering] <--> C[Cloud SQL Instance]
HandsOn project using GCP to provide a server that connects to a Postgres Database. The Postgres Database acn only be accessed via the server.
Core concepts are put into practice, such as subnetworks, VPCs and even the way of connecting the DB with the VPC network created.
- First of all, create a GCP project, then define the
project_id
variable inside a file namedterraform.tfvars
:
project_id = "project_name"
- Create a
bucket
using GCP. - Create the file
backend.conf
, and set with the information of the created bucket:
cp backend.conf.example backend.conf
-
Create an
ssh-key
, name the.pub
as follows./ssh_keys/gcp.pub
-
Init Terraform's state
terraform init -backend-config=backend.conf
- Apply the changes needed to set the resources:
terraform apply
Now, after all resources have been initialized, ssh into the VM. After the terraform apply
the output should be:
compute_instance_public_ip = "public_ip"
internal_ipv4 = "postgres_ip"
Use the compute_instance_public_ip
to access the VM:
ssh -i priv_key dev@public_ip
Inside the VM, install psql
:
sudo apt install postgresql-client
Finally, inspect the created database, it will ask for the password, the default is password
:
psql -h "postgres_ip" -U dev -d db_policy1 -p 5432
Inside the postgres cli, we can type \l
to inspect the db_policy1
db.