Terraform automation on IBM Cloud (#2/2): Provisioning a PostgreSQL Database on IBM Cloud using Terraform

ibmcloud regions
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
resource_group = var.resource_group
region = var.ibmcloud_region
}

Variables

variable "ibmcloud_api_key" {}

variable "resource_group" {
default = "xxxxxx"
}

variable "ibmcloud_region" {
default = "xxxxx"
}

variable "zone" {
default = "xxxx"
}

variable "db_name" {
description = "Name of the PostgreSQL database"
type = string
default = "xxxxx"
}

Versions file

terraform {
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.23.0"
}
}
}

Main Script

data "ibm_resource_group" "icd_postgres_group" {
name = var.resource_group
}

resource "ibm_database" "icd_postgres" {
name = var.db_name
location = var.ibmcloud_region
service = "databases-for-postgresql"
plan = "standard"
adminpassword = "xxxxxxx"
# If not provided it takes the default resource group.
resource_group_id = data.ibm_resource_group.icd_postgres_group.id

version = "12"
# Total amount of memory to be shared between the DB members
# Postgres has 2 members by default.

members_memory_allocation_mb = 3072
members_disk_allocation_mb = 20480

timeouts {
# Default timeout is 10mins
create = "15m"
delete = "3m"
}

provisioner "local-exec" {
environment = {
db_name = var.db_name
IBMCLOUD_API_KEY = var.ibmcloud_api_key
ibmcloud_region = var.ibmcloud_region
ibmcloud_resource_group = var.resource_group
}

working_dir = "postgres"
command = "prepcommands-v2.sh"
}

}
adminpassword     = "xxxxxxx"
members_memory_allocation_mb = 3072
members_disk_allocation_mb = 20480
timeouts {
# Default timeout is 10mins
create = "15m"
delete = "3m"
}
terraform init
terraform plan
terraform apply
terraform plan -target="ibm_database.icd_postgres"
terraform apply -target="ibm_database.icd_postgres"

Populating the database

#!/bin/bash

### The name of the PostgreSQL db is set by the Terraform script which provisionned the databse
postrgresql_db_name=$db_name
echo "Retrieving the $db_name database"

### logging into IBM Cloud
echo "Logging into IBM Cloud using IBMCLOUD_API_KEY environment variable"
ibmcloud login -a cloud.ibm.com -r $ibmcloud_region -g $ibmcloud_resource_group

### export the database information in a JSON format
echo "Exporting database $db_name properties to JSON"
ibmcloud cdb cxn --all --json $db_name > db_desc.json

echo "Setting database connectivity parameters"

export PGHOST=$(cat db_desc.json | jq -r '.connection.postgres.hosts[0].hostname')
export PGPORT=$(cat db_desc.json | jq -r '.connection.postgres.hosts[0].port')
export PGDATABASEPATH=$(cat db_desc.json | jq -r '.connection.postgres.database')

#export PGDATABASE=${PGDATABASE:-ibmclouddb}
export PGDATABASE1=${PGDATABASE:-$PGDATABASEPATH}
export PGUSER=${PGUSER:-admin}
export PGPASSWORD=${PGPASSWORD:-adminpassword}

echo "PGHOST: " $PGHOST
echo "PGPORT: " $PGPORT
echo "PGUSER: " $PGUSER
echo "PGDATABASE: " $PGDATABASE

echo "Populating the database $db_name"

psql -X -c "DROP database if exists POSTGRES_DB" -f db_step1.sql -f db_step2.sql -f db_step3.sql -f db_step4.sql -f db_step5.sql -f db_step6.sql -f db_step7.sql -f db_step8.sql -c "COMMIT"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alain Airom (Ayrom)

Alain Airom (Ayrom)

IT guy for a long time, looking for technical challenges everyday!