Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Terraform For DBAs: Deploy an Autonomous Databa...

Terraform For DBAs: Deploy an Autonomous Database in Minutes with OCI Resource Manager

Cloud services offer inexpensive, limitless resources—ideal for database teams practicing and validating recovery, upgrades, patches, failover to standby systems, and performance changes. But getting started in the cloud can feel intimidating and time-consuming. Enter Terraform, an automation tool for streamlining and simplifying infrastructure deployment and management in cloud environments!

This session teaches by example, using Terraform to perform a live deployment of an Oracle Autonomous Database. During the demonstration, attendees will discover what makes Terraform different from other Infrastructure as Code tools, and learn the techniques and best practices that will help them jump-start their Terraform journey. No prior experience with Terraform or Oracle Cloud is required!

Sean Scott

August 17, 2024
Tweet

More Decks by Sean Scott

Other Decks in Technology

Transcript

  1. LAOUC Community Tour 2024 2 a 23 de Agosto 2024

    Terraform for DBAs Implement an Oracle Autonomous Database in minutes with OCI Resource Manager
  2. Database Reliability Engineering MAA ⁘ RAC ⁘ RMAN Data Guard

    ⁘ Sharding ⁘ Partitioning Information Lifecycle Management Exadata & Engineered Systems Database Modernization Upgrades ⁘ Patching ⁘ Migrations Cloud ⁘ Hybrid Automation DevOps ⁘ IaC ⁘ Containers ⁘ Terraform Vagrant ⁘ Ansible Observability AHF ⁘ TFA ⁘ CHA ⁘ CHM
  3. www.viscosityna.com @ViscosityNA Oracle on Docker Running Oracle Databases in Linux

    Containers Free sample chapter: https://oraclesean.com
  4. km mi Mexico 3,584 2,227 Guatemala 1,056 656 Costa Rica

    854 531 Panama 1,813 1,127 Chile 4,985 3,098 Brazil 2,617 1,626 Uruguay 1,567 974 Argentina 221 137 Paraguay 1,037 644 Home 9,753 6,061 ------ ------ 27,487 17,081 ¡Me gustan los aviones! ¡Me gustas tú! ¡Me gusta viajar! ¡Me gustas tú! ~ Manu Chao
  5. @ViscosityNA www.viscosityna.com @ViscosityNA www.viscosityna.com Latin "terra" (Earth) English "form" (form)

    ter•ra•form verb Transform an alien environment to sustain life. www.viscosityna.com @ViscosityNA
  6. An Infrastructure as Code (IaC) tool from Hashicorp. Terraform defines,

    provisions and tracks local and cloud infrastructure. @ViscosityNA www.viscosityna.com Ter•ra•form noun
  7. www.viscosityna.com @ViscosityNA Terraform is like the metro, not a roller

    coaster Without excitement Without surprises Without thrills
  8. @ViscosityNA www.viscosityna.com Building a 10-liter fish tank with imperative language

    • Get tank A, pump B, heater C... • Set them up... • Add exactly X liters of water... • Add exactly Y grams of salt... • Adjust the temperature to Z °C... • Add specific fish...
  9. @ViscosityNA www.viscosityna.com Building a 10-liter fish tank with imperative language

    • Imperative languages scale poorly: • Changing tank size redefines all assets • Resources have dependencies
  10. @ViscosityNA www.viscosityna.com Building a 10-liter fish tank with imperative language

    • Changes require deep domain experience • Does heater capacity change linearly with water volume? • Does a tall, skinny tank have different requirements than short and wide? • Does surface area affect salt concentration? • Should intermediate values round up or down? • Are salinity ratios fixed according to water volume?
  11. @ViscosityNA www.viscosityna.com Building a 10-liter fish tank with declarative language

    "I want a 10-liter fish tank" @ViscosityNA www.viscosityna.com
  12. @ViscosityNA www.viscosityna.com Providers are implementation experts • They understand dependencies

    • They interpret configurations • They build the declared infrastructure Terraform provider
  13. @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
  14. @ViscosityNA www.viscosityna.com • Infrastructure objects available to Terraform • Properties

    defined in the Terraform API • Created and managed by assigning values to properties • Configured via simple text files Terraform resource
  15. @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
  16. @ViscosityNA www.viscosityna.com Terraform projects @ViscosityNA • A collection of files

    for defining infrastructure • Saved under a single directory • Usually managed through version control (GitHub, etc.)
  17. @ViscosityNA www.viscosityna.com Start a new Terraform project Create a project

    directory and create files: • providers.tf • variables.tf • terraform.tfvars • main.tf • outputs.tf @ViscosityNA Project files: https://github.com/oraclesean/terraform-for-oracle-dbas
  18. @ViscosityNA www.viscosityna.com @ViscosityNA Everything in one file • main.tf •

    Can be difficult to maintain Separate files for each resource • Modular and more portable code • compute.tf • storage.tf • variables.tf Terraform project styles
  19. @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
  20. @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 assignments could go here
  21. @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 The same variables defined in variables.tf Variable assignments go here Assigning variables in the terraform.tfvars means we only need to change a single file to scale the infrastructure, run the same configuration in a different environment, etc.!
  22. @ViscosityNA www.viscosityna.com Prepare the project • From the project directory,

    run: terraform init terraform plan terraform apply @ViscosityNA www.viscosityna.com
  23. @ViscosityNA www.viscosityna.com terraform init > terraform init Initializing the backend...

    Initializing provider plugins... - Finding oracle/oci versions matching ">= 4.0.0"... - Installing oracle/oci v6.4.0... - Installed oracle/oci v6.4.0 (signed by a HashiCorp partner, key ID 1533A49284137CEB) Partner and community providers are signed by their developers. If you'd like to know more about provider signing, you can read about it here: https://www.terraform.io/docs/cli/plugins/signing.html 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.
  24. @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.
  25. @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.
  26. @ViscosityNA www.viscosityna.com Add a resource for the Autonomous Database •

    Add the ADB resource in main.tf • Add the ADB variables to variables.tf • Assign values in terraform.tfvars • Add output variables to output.tf @ViscosityNA
  27. @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 ADB properties Values used to create the ADB
  28. @ViscosityNA www.viscosityna.com Add DB variables to variables.tf # Autonomous DB

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

    type = string default = "19c" # Option is 19c } variable "db_workload" { type = string default = "OLTP" # Options are: OLTP, DW, AJD, APEX } Variable declaration block Define the variable type Assign a default value
  30. @ViscosityNA www.viscosityna.com Add DB variables to variables.tf variable "cpu_core_count" {

    type = number default = 1 } variable "data_storage_size_in_tbs" { type = number default = 1 }
  31. @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 DB variables to variables.tf
  32. @ViscosityNA www.viscosityna.com Assign variable values in terraform.tfvars # Autonomous database

    variable values db_name = "ADB19C" display_name = "ADB19C" admin_password = "XXXXXXXXXXXXXXXXXXXXXX" # Default overrides #db_version = #db_workload = #cpu_core_count = #data_storage_size_in_tbs = #is_free_tier = #license_model = The values most likely to change every time we create a database To override default values uncomment the line and assign a value
  33. @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 ... }
  34. @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) ...
  35. @ViscosityNA www.viscosityna.com terraform plan (continued) ... Plan: 1 to add,

    0 to change, 0 to destroy. Changes to Outputs: + db_name = "ADB19C" + 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.
  36. @ViscosityNA www.viscosityna.com terraform apply > terraform apply ... Plan: 1

    to add, 0 to change, 0 to destroy. Changes to Outputs: + db_name = "ADB19C" + 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
  37. @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 = "ADB19C" db_state = "AVAILABLE" terraform apply (continued)
  38. @ViscosityNA www.viscosityna.com How does Terraform help DBAs? @ViscosityNA • Simplifies

    and speeds database creation • Codifies and standardizes processes • Ensures consistency across environments • Reduces DBA workload • Reduces dependencies/waiting for DBA resources • Accelerates development and innovation