Slide 1

Slide 1 text

Terraform on Oracle Cloud A Primer for Database Administrators APAC Tour '23 December 11, 2023 Auckland, New Zealand @ViscosityNA www.viscosityna.com Sean Scott

Slide 2

Slide 2 text

Database Reliability Engineering 
 Business Continuity ⁘ HA & DR 
 Automation ⁘ Observability Real Application Clusters ⁘ Data Guard ⁘ Sharding Containerization ⁘ Terraform ⁘ Ansible Exadata & Engineered Systems AHF ⁘ TFA ⁘ GIMR ⁘ CHA Sean Scott Oracle ACE Director Data on Kubernetes Community Ambassador Managing Principal Consultant Viscosity North America @ViscosityNA www.viscosityna.com

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

An Open Community for 
 Data on Kubernetes https://dok.community

Slide 5

Slide 5 text

@ViscosityNA www.viscosityna.com Oracle on Docker Running Oracle Databases in Linux Containers Free sample chapter: 
 https://oraclesean.com

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

BOI - SEA 642 km SEA - ICN 8,394 km ICN - NRT 1,260 km NRT - MEL 8,144 km MEL - AKL 2,644 km AKL - LAX 10,467 km LAX - BOI 1,085 km 32,635 km

Slide 8

Slide 8 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com Latin "terra" (earth), English "form" ter•ra•form verb To transform an environment to support life www.viscosityna.com @ViscosityNA

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

An Infrastructure as Code (IaC) tool from Hashicorp. Terraform defines, provisions and manages cloud & on-premises infrastructure. @ViscosityNA www.viscosityna.com Ter•ra•form noun

Slide 16

Slide 16 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com Terraform is a declarative language. Imperative languages provide instruction. Declarative languages define an intent.

Slide 17

Slide 17 text

@ViscosityNA www.viscosityna.com Build a 10-liter aquarium with imperative language • Get tank a, pump b, heater c... • Configure them... • Add x liters water... • Add y grams salt... • Set temperature to z°C... • Add n fish...

Slide 18

Slide 18 text

@ViscosityNA www.viscosityna.com Build a 10-liter aquarium with imperative language • Imperative languages scale poorly: • Changing the tank size redefines the assets • Resources may have dependencies • Change requires domain expertise • Should heater capacity change linearly or exponentially to water volume? • Will a tall, skinny tank have different requirements than a short, wide one? • Does surface area affect monitoring and maintenance schedules? • Which intermediate values should round up or down? • Are salinity ratios fixed for all water volumes?

Slide 19

Slide 19 text

@ViscosityNA www.viscosityna.com Build a 10-liter aquarium with declarative language "I want a 10-liter aquarium." @ViscosityNA www.viscosityna.com

Slide 20

Slide 20 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com The expert understands the differences between this:

Slide 21

Slide 21 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com ...and this:

Slide 22

Slide 22 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com In Terraform, that expert is called a provider. @ViscosityNA www.viscosityna.com

Slide 23

Slide 23 text

@ViscosityNA www.viscosityna.com Providers are implementation experts • Understand dependencies • Interpret configurations • Build the declared infrastructure Terraform provider

Slide 24

Slide 24 text

@ViscosityNA www.viscosityna.com We tell the provider what we want. The provider deploys resources for us. Different providers for OCI, Azure, AWS, GCP, etc. Terraform provider

Slide 25

Slide 25 text

@ViscosityNA www.viscosityna.com • Infrastructure objects available to Terraform • Properties defined in the Terraform API • Created & managed by assigning 
 values to properties • Configured via simple text files Terraform resource

Slide 26

Slide 26 text

@ViscosityNA www.viscosityna.com Resources can be: • Physical: compute, storage, network • Dependent: queries against the environment • Dynamic: functions, expressions, loops • Config: security lists, rules, operations • Code: scripts, payloads Terraform resource

Slide 27

Slide 27 text

@ViscosityNA www.viscosityna.com Terraform projects @ViscosityNA • Files with infrastructure definitions • Stored in a common directory • Often managed in a repository (GitHub, etc.)

Slide 28

Slide 28 text

@ViscosityNA www.viscosityna.com Start a new Terraform project Create a project directory & add files: • providers.tf • variables.tf • terraform.tfvars • main.tf • outputs.tf @ViscosityNA Project files: https://github.com/oraclesean/terraform-for-oracle-dbas

Slide 29

Slide 29 text

@ViscosityNA www.viscosityna.com @ViscosityNA Terraform project styles Everything in one file • Can be difficult to read, maintain • main.tf Separate files for each resource • More portable, modular code • compute.tf • storage.tf • variables.tf

Slide 30

Slide 30 text

@ViscosityNA www.viscosityna.com providers.tf provider "oci" { tenancy_ocid = var.tenancy_ocid region = var.region user_ocid = var.user_ocid fingerprint = var.fingerprint private_key_path = var.private_key_path } Resource variables Value assignments Value assignments could go here

Slide 31

Slide 31 text

@ViscosityNA www.viscosityna.com variables.tf # Terraform tenancy variables variable "tenancy_ocid" {} variable "region" {} variable "user_ocid" {} variable "fingerprint" {} variable "private_key_path" {} Variable declarations Value assignment could go here

Slide 32

Slide 32 text

@ViscosityNA www.viscosityna.com terraform.tfvars # Terraform tenancy variable values tenancy_ocid = Your tenancy_ocid region = Your region identifier user_ocid = Your user_ocid fingerprint = Your fingerprint private_key_path = Your private_key_path Same variables as defined in variables.tf Hard-coded variable assignments Limiting hard-coded assignments to terraform.tfvars means no changes are needed elsewhere to run the same configuration on different tenancies, scale, etc.!

Slide 33

Slide 33 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com variables.tf terraform.tfvars providers.tf etc. Declare Assign Inherit Understanding variable hierarchy

Slide 34

Slide 34 text

@ViscosityNA www.viscosityna.com Test the configuration • In the project directory, run: terraform init terraform plan terraform apply @ViscosityNA www.viscosityna.com

Slide 35

Slide 35 text

@ViscosityNA www.viscosityna.com terraform init > terraform init Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/oci... - Installing hashicorp/oci v4.76.0... - Installed hashicorp/oci v4.76.0 (signed by HashiCorp) 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.

Slide 36

Slide 36 text

@ViscosityNA www.viscosityna.com terraform plan > terraform plan No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Slide 37

Slide 37 text

@ViscosityNA www.viscosityna.com terraform apply > terraform apply No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Slide 38

Slide 38 text

@ViscosityNA www.viscosityna.com Add an Autonomous Database resource • Add the ADB resource in main.tf • Add ADB variables to variables.tf • Add ADB values to terraform.tfvars • Add output variables to output.tf @ViscosityNA

Slide 39

Slide 39 text

@ViscosityNA www.viscosityna.com main.tf # Autonomous database resource resource "oci_database_autonomous_database" "autonomous_db" { compartment_id = var.tenancy_ocid db_name = var.db_name display_name = var.display_name db_version = var.db_version db_workload = var.db_workload cpu_core_count = var.cpu_core_count data_storage_size_in_tbs = var.data_storage_size_in_tbs is_free_tier = var.is_free_tier license_model = var.license_model admin_password = var.admin_password } Type of resource Name assigned to the resource Properties for ADB Values used to create the ADB

Slide 40

Slide 40 text

@ViscosityNA www.viscosityna.com Add database variables to variables.tf # Autonomous DB variables variable "db_name" { type = string } variable "display_name" { type = string } variable "admin_password" { type = string } Variable declarations

Slide 41

Slide 41 text

@ViscosityNA www.viscosityna.com Add database variables to variables.tf variable "db_version" { type = string default = "21c" # Options are 19c, 21c } variable "db_workload" { type = string default = "OLTP" # Options are: OLTP, DW, AJD, APEX } Variable declaration block Set variable type Assign a default value

Slide 42

Slide 42 text

@ViscosityNA www.viscosityna.com Add database variables to variables.tf variable "cpu_core_count" { type = number default = 1 } variable "data_storage_size_in_tbs" { type = number default = 1 }

Slide 43

Slide 43 text

@ViscosityNA www.viscosityna.com variable "is_free_tier" { type = string default = "true" # Must be false for AJD, APEX } variable "license_model" { type = string default = "LICENSE_INCLUDED" } Add database variables to variables.tf

Slide 44

Slide 44 text

@ViscosityNA www.viscosityna.com Add variable assignments to terraform.tfvars # Autonomous database variable values db_name = "ADB21C" display_name = "ADB21C" admin_password = "XXXXXXXXXXXXXXXXXXXXXX" # Default overrides #db_version = #db_workload = #cpu_core_count = #data_storage_size_in_tbs = #is_free_tier = #license_model = ADB values likely to change for each DB To override defaults, un-comment the line and set a value

Slide 45

Slide 45 text

@ViscosityNA www.viscosityna.com outputs.tf output "db_name" { value = oci_database_autonomous_database.autonomous_db.display_name } output "db_state" { value = oci_database_autonomous_database.autonomous_db.state } resource "oci_database_autonomous_database" "autonomous_db" { compartment_id = var.tenancy_ocid db_name = var.db_name display_name = var.display_name ... }

Slide 46

Slide 46 text

@ViscosityNA www.viscosityna.com Create the database! • Run: terraform plan terraform apply @ViscosityNA www.viscosityna.com

Slide 47

Slide 47 text

@ViscosityNA www.viscosityna.com terraform plan > terraform plan Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # oci_database_autonomous_database.autonomous_db will be created + resource "oci_database_autonomous_database" "autonomous_db" { + actual_used_data_storage_size_in_tbs = (known after apply) + admin_password = (sensitive value) ...

Slide 48

Slide 48 text

@ViscosityNA www.viscosityna.com terraform plan (Continued) ... Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + db_name = "ADB21C" + db_state = (known after apply) ──────────────────────────────────────────────────────────────────────────── Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

Slide 49

Slide 49 text

@ViscosityNA www.viscosityna.com terraform apply > terraform apply ... Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + db_name = "ADB21C" + db_state = (known after 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

Slide 50

Slide 50 text

@ViscosityNA www.viscosityna.com ... Enter a value: yes oci_database_autonomous_database.autonomous_db: Creating... oci_database_autonomous_database.autonomous_db: Still creating... [10s elapsed] ... oci_database_autonomous_database.autonomous_db: Creation complete after 1m31s Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: db_name = "ADB21C" db_state = "AVAILABLE" terraform apply (Continued)

Slide 51

Slide 51 text

@ViscosityNA www.viscosityna.com How does Terraform help the DBA? @ViscosityNA • Streamlines & simplifies database creation • Codifies & standardizes processes • Consistent environments, from dev to prod • Reduces DBA workload • Lowers dependence/waiting for DBA resources • Accelerates development, innovation

Slide 52

Slide 52 text

@ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com Questions

Slide 53

Slide 53 text

@ViscosityNA www.viscosityna.com Contact Me: sean.scott@viscosityna.com https://linktr.ee/oraclesean