Deploy Redis as a cache for Postgres on a Google Cloud Arm based Instance

You can deploy Redis as a cache for Postgres on Google Cloud using Terraform and Ansible.

In this section, you will deploy Redis as a cache for Postgres on a Google Cloud instance.

If you are new to Terraform, you should look at Automate GCP instance creation using Terraform before starting this Learning Path.

Before you begin

You should have the prerequisite tools installed before starting the Learning Path.

Any computer which has the required tools installed can be used for this section. The computer can be your desktop or laptop computer or a virtual machine with the required tools.

You will need a Google Cloud account to complete this Learning Path. Create an account if you don’t have one.

Before you begin you will also need:

  • Login to the Google Cloud CLI
  • An SSH key pair

The instructions to login to the Google Cloud CLI and create the keys are below.

Acquire GCP Access Credentials

The installation of Terraform on your Desktop/Laptop needs to communicate with GCP. Thus, Terraform needs to be authenticated.

To obtain GCP user credentials, follow this guide .

Generate an SSH key-pair

Generate an SSH key-pair (public key, private key) using ssh-keygen to use for GCP instance access. To generate the key-pair, follow this guide .

Note

If you already have an SSH key-pair present in the ~/.ssh directory, you can skip this step.

Create GCP instances using Terraform

Using a text editor, save the code below in a file called main.tf. Here we are creating 2 instances.

    

        
        
            provider "google" {
  project = "{project_id}"
  region  = "us-central1"
  zone    = "us-central1-a"
}

resource "google_compute_instance" "PSQL_TEST" {
  name         = "psqltest-${count.index + 1}"
  count        = "2"
  machine_type = "t2a-standard-1"

  boot_disk {
    initialize_params {
      image = "ubuntu-os-cloud/ubuntu-2204-lts-arm64"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // Ephemeral public IP
    }
  }
  metadata = {
    ssh-keys = "ubuntu:${file("~/.ssh/id_rsa.pub")}"
  }
}

resource "google_compute_firewall" "rules" {
  project       = "{project_id}"
  name          = "my-firewall-rule"
  network       = "default"
  description   = "Open ssh connection and psql port"
  source_ranges = ["0.0.0.0/0"]

  allow {
    protocol = "icmp"
  }

  allow {
    protocol = "tcp"
    ports    = ["22", "5432"]
  }
}

resource "google_compute_network" "default" {
  name = "test-network1"
}
resource "local_file" "inventory" {
  depends_on = [google_compute_instance.PSQL_TEST]
  filename   = "/tmp/inventory"
  content    = <<EOF
[db_master]
${google_compute_instance.PSQL_TEST[0].network_interface.0.access_config.0.nat_ip}
${google_compute_instance.PSQL_TEST[1].network_interface.0.access_config.0.nat_ip}
[all:vars]
ansible_connection=ssh
ansible_user=ubuntu
                EOF
}
        
    

In the provider and google_compute_firewall sections, update the project_id with your value.

The inventory file is automatically generated and does not need to be changed.

Terraform Commands

Use Terraform to deploy the main.tf file.

Initialize Terraform

Run terraform init to initialize the Terraform deployment. This command downloads the dependencies required for GCP.

    

        
        
            terraform init
        
    

The output should be similar to:

    

        
        Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Finding latest version of hashicorp/local...
- Installing hashicorp/google v4.57.0...
- Installed hashicorp/google v4.57.0 (signed by HashiCorp)
- Installing hashicorp/local v2.4.0...
- Installed hashicorp/local v2.4.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

        
    

Create a Terraform execution plan

Run terraform plan to create an execution plan.

    

        
        
            terraform plan
        
    

A long output of resources to be created will be printed.

Apply a Terraform execution plan

Run terraform apply to apply the execution plan and create all GCP resources.

    

        
        
            terraform apply
        
    

Answer yes to the prompt to confirm you want to create GCP resources.

The output should be similar to:

    

        
        Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

        
    

Configure Postgres through Ansible

Install Postgres and the required dependencies on both the instances.

Follow the instructions in this section to configure Postgres through Ansible.

Connect to Database from local machine

Follow the instructions in this section to connect to the database from a local machine.

Deploy Redis as a cache for Postgres using Python

Follow the instructions in this section to deploy Redis as a cache for PostgreSQL using Python.

You have successfully deployed Redis as a cache for PostgreSQL on a Google Cloud Arm based Instance.

Clean up resources

Run terraform destroy to delete all resources created.

    

        
        
            terraform destroy
        
    
Back
Next