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

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/)
- [Deploy Redis as a cache for MySQL on an AWS Arm based Instance](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_mysql_aws/)
- [Deploy Redis as a cache for MySQL on an Azure Arm based Instance](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_mysql_azure/)
- [Deploy Redis as a cache for MySQL on a GCP Arm based Instance](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_mysql_gcp/)
- [Deploy Redis as a cache for Postgres on an AWS Arm based Instance](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_psql_aws/)
- [Deploy Redis as a cache for Postgres on an Azure Arm based Instance](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_psql_azure/)
- [Deploy Redis as a cache for Postgres on a Google Cloud Arm based Instance](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_psql_gcp/)
- [Next Steps](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/_next-steps/)

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](https://learn.arm.com/learning-paths/servers-and-cloud-computing/gcp/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](https://console.cloud.google.com/?hl=en-au) 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](https://learn.arm.com/install-guides/gcloud/#acquire-user-credentials).

### 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](https://learn.arm.com/install-guides/ssh/#ssh-keys).

> 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.

```hcl
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.

```bash
terraform init
```

The output should be similar to:

```
__output__Initializing the backend...
__output__Initializing provider plugins...
__output__- Finding latest version of hashicorp/google...
__output__- Finding latest version of hashicorp/local...
__output__- Installing hashicorp/google v4.57.0...
__output__- Installed hashicorp/google v4.57.0 (signed by HashiCorp)
__output__- Installing hashicorp/local v2.4.0...
__output__- Installed hashicorp/local v2.4.0 (signed by HashiCorp)
__output__Terraform has created a lock file .terraform.lock.hcl to record the provider
__output__selections it made above. Include this file in your version control repository
__output__so that Terraform can guarantee to make the same selections by default when
__output__you run "terraform init" in the future.
__output__Terraform has been successfully initialized!
__output__You may now begin working with Terraform. Try running "terraform plan" to see
__output__any changes that are required for your infrastructure. All Terraform commands
__output__should now work.
__output__If you ever set or change modules or backend configuration for Terraform,
__output__rerun this command to reinitialize your working directory. If you forget, other
__output__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.

```bash
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.

```bash
terraform apply
```

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

The output should be similar to:

```
__output__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](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_psql_aws/#configure-postgres-through-ansible) to configure Postgres through Ansible.

## Connect to Database from local machine

Follow the instructions in this [section](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_psql_aws/#connect-to-database-from-local-machine) to connect to the database from a local machine.

## Deploy Redis as a cache for Postgres using Python

Follow the instructions in this [section](https://learn.arm.com/learning-paths/servers-and-cloud-computing/redis_cache/redis_psql_aws/#deploy-redis-as-a-cache-for-postgres-using-python) 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.

```bash
terraform destroy
```
