Launch OCI free tier ATP and ADW using Terraform on Window platform

Terraform template available at GitHub will not work directly with OCI free tier ADB database service. You have to adjust some parameter inside the Terraform template to make it compatible with free tier ADB services.

We will Launch two ADB databases one for the ATP database and one for the ADW database using a single command then we will play with ADB using OCI CLI and then finally we will destroy the created infrastructure using a single command.

Download the terraform for window10.
https://releases.hashicorp.com/terraform/0.12.19/terraform_0.12.19_windows_amd64.zip

Installation Steps
copy the downloads for windows version.
copy inside program file and configure windows path variable

 

 

C:\Users\oracle-learn>terraform
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
destroy Destroy Terraform-managed infrastructure
env Workspace management
fmt Rewrites config files to canonical format
get Download and install modules for the configuration
graph Create a visual graph of Terraform resources
import Import existing infrastructure into Terraform
init Initialize a Terraform working directory
output Read an output from a state file
plan Generate and show an execution plan
providers Prints a tree of the providers used in the configuration
refresh Update local state file against real resources
show Inspect Terraform state or plan
taint Manually mark a resource for recreation
untaint Manually unmark a resource as tainted
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management

All other commands:
0.12upgrade Rewrites pre-0.12 module source code for v0.12
debug Debug output management (experimental)
force-unlock Manually unlock the terraform state
push Obsolete command for Terraform Enterprise legacy (v1)
state Advanced state management

C:\Users\oracle-learn>

C:\Users\oracle-learn>terraform --version
Terraform v0.12.18

Prepare the Environment.

oracle-learn MINGW64 ~/projects/adb (master)
$ cat .profile
#Terraform
export TF_VAR_tenancy_ocid=ocid1.tenancy.oc1..xyz
export TF_VAR_user_ocid=ocid1.user.oc1..pqr
export TF_VAR_fingerprint=
export TF_VAR_region=ap-mumbai-1
export TF_VAR_private_key_path=<path/oci_api_key.pem>
export TF_VAR_private_key_password={put-here-the-private-key-password}
export TF_VAR_compartment_ocid=ocid1.compartment.oc1..cid

You may refer OCI Documentation to find these variable values.

 

$ cat provider.tf
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.

provider "oci" {
  tenancy_ocid     = "${var.tenancy_ocid}"
  user_ocid        = "${var.user_ocid}"
  fingerprint      = "${var.fingerprint}"
  private_key_path = "${var.private_key_path}"
  region           = "${var.region}"
}

provider "local" {
  version = ">=1.3.0" # Need this version of the local provider to support base64 encoded inputs
}
$ cat variables.tf
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.

variable "tenancy_ocid" {}
variable "user_ocid" {}
variable "fingerprint" {}
variable "private_key_path" {}
variable "region" {}
variable "compartment_ocid" {}

variable "autonomous_database_db_workload" {
  default = "OLTP"
}

variable "autonomous_data_warehouse_db_workload" {
  default = "DW"
}

variable "autonomous_database_defined_tags_value" {
  default = "value"
}

variable "autonomous_database_freeform_tags" {
  default = {
    "Department" = "Finance"
  }
}

variable "autonomous_database_license_model" {
  default = "LICENSE_INCLUDED"
}

variable "autonomous_database_is_dedicated" {
  default = false
}

I have modified the template accordingly for the free tier template available at GitHub

$ cat autonomous_database_wallet.tf
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.

resource "random_string" "autonomous_database_wallet_password" {
  length  = 16
  special = true
}

data "oci_database_autonomous_database_wallet" "autonomous_database_wallet" {
  autonomous_database_id = "${oci_database_autonomous_database.autonomous_database.id}"
  password               = "${random_string.autonomous_database_wallet_password.result}"
  base64_encode_content  = "true"
}

resource "local_file" "autonomous_database_wallet_file" {
  content_base64 = "${data.oci_database_autonomous_database_wallet.autonomous_database_wallet.content}"
  filename       = "${path.module}/autonomous_database_wallet.zip"
}

output "autonomous_database_wallet_password" {
  value = "${random_string.autonomous_database_wallet_password.result}"
}

$ cat autonomous_database.tf
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.

resource "random_string" "autonomous_database_admin_password" {
  length      = 16
  min_numeric = 1
  min_lower   = 1
  min_upper   = 1
  min_special = 1
}

resource "oci_database_autonomous_database" "autonomous_database" {
  #Required
  admin_password           = "${random_string.autonomous_database_admin_password.result}"
  compartment_id           = "${var.compartment_ocid}"
  cpu_core_count           = "1"
  data_storage_size_in_tbs = "1"
  db_name                  = "atpdb1"
  is_free_tier             = true
  #Optional
  db_workload                                    = "${var.autonomous_database_db_workload}"
  display_name                                   = "example_autonomous_database"
  is_auto_scaling_enabled                        = "false"
  is_preview_version_with_service_terms_accepted = "false"
}

data "oci_database_autonomous_databases" "autonomous_databases" {
  #Required
  compartment_id = "${var.compartment_ocid}"

  #Optional
  display_name = "${oci_database_autonomous_database.autonomous_database.display_name}"
  db_workload  = "${var.autonomous_database_db_workload}"
}

output "autonomous_database_admin_password" {
  value = "${random_string.autonomous_database_admin_password.result}"
}

output "autonomous_database_high_connection_string" {
  value = "${lookup(oci_database_autonomous_database.autonomous_database.connection_strings.0.all_connection_strings, "high", "unavailable")}"
}

output "autonomous_databases" {
  value = "${data.oci_database_autonomous_databases.autonomous_databases.autonomous_databases}"
}

Follow the same steps for autonomous_data_warehouse_wallet.tf and autonomous_data_warehouse.tf

[1] Initialize a Terraform working directory
$ terraform init

[2] Generate and show an execution plan
$ terraform plan

[3] Builds or changes infrastructure
$ terraform apply

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

random_string.autonomous_database_admin_password: Creating...
random_string.autonomous_data_warehouse_admin_password: Creating...
random_string.autonomous_data_warehouse_wallet_password: Creating...
random_string.autonomous_database_wallet_password: Creating...
random_string.autonomous_database_admin_password: Creation complete after 0s [id=0++6KrIV8T?=F1*k]
random_string.autonomous_data_warehouse_admin_password: Creation complete after 0s [id=Dtbt2t!0KZ]y0Y<&] random_string.autonomous_data_warehouse_wallet_password: Creation complete after 0s [id=a?*>RADQ+]Uv[Xgt]
random_string.autonomous_database_wallet_password: Creation complete after 0s [id=_HLrXaT*Zf@eXqu#]
oci_database_autonomous_database.autonomous_data_warehouse: Creating...
oci_database_autonomous_database.autonomous_database: Creating...
oci_database_autonomous_database.autonomous_database: Still creating... [10s elapsed]
oci_database_autonomous_database.autonomous_data_warehouse: Still creating... [10s elapsed]
oci_database_autonomous_database.autonomous_database: Still creating... [20s elapsed]
oci_database_autonomous_database.autonomous_data_warehouse: Still creating... [20s elapsed]
.........
oci_database_autonomous_database.autonomous_data_warehouse: Still creating... [1m50s elapsed]
oci_database_autonomous_database.autonomous_database: Still creating... [2m0s elapsed]
oci_database_autonomous_database.autonomous_data_warehouse: Still creating... [2m0s elapsed]
oci_database_autonomous_database.autonomous_database: Still creating... [2m10s elapsed]
oci_database_autonomous_database.autonomous_data_warehouse: Still creating... [2m10s elapsed]

oci_database_autonomous_database.autonomous_data_warehouse: Still creating... [2m20s elapsed]
......
local_file.autonomous_data_warehouse_wallet_file: Creating...
local_file.autonomous_data_warehouse_wallet_file: Creation complete after 0s [id=a2cfa32740b9dcdffb8524d72fc703f62c5cfcdf]

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

Outputs:

[4] Play with ADB

#List of all the ADBs
$ oci db autonomous-database list –compartment-id $TF_VAR_compartment_ocid

#List of all the ADWs
$ oci db autonomous-data-warehouse list –compartment-id $TF_VAR_compartment_ocid

#Stop ADW
$ oci db autonomous-data-warehouse stop –autonomous-data-warehouse-id ocid1.autonomousdatabase.oc1.xyz

#Start ADW
$ oci db autonomous-data-warehouse start –autonomous-data-warehouse-id ocid1.autonomousdatabase.oc1.xyz

#Get details about specific ADW
$ oci db autonomous-data-warehouse get –autonomous-data-warehouse-id ocid1.autonomousdatabase.oc1.xyz

[5] Destroy Terraform-managed infrastructure
$ terraform destroy

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

local_file.autonomous_database_wallet_file: Destroying... [id=6c0eff5f4d195f0e38a364ef7d414dc7c39ed027]
local_file.autonomous_data_warehouse_wallet_file: Destroying... [id=a2cfa32740b9dcdffb8524d72fc703f62c5cfcdf]
local_file.autonomous_data_warehouse_wallet_file: Destruction complete after 0s
local_file.autonomous_database_wallet_file: Destruction complete after 0s
random_string.autonomous_data_warehouse_wallet_password: Destroying... [id=a?*>RADQ+]Uv[Xgt]
random_string.autonomous_data_warehouse_wallet_password: Destruction complete after 0s
.....
Destroy complete! Resources: 8 destroyed.

Thank you for visit this blog.
Happy Learning. 🙂