Terraform
このトピックでは、個々のOracle Database@AWSリソースをプロビジョニングするためのサンプルTerraform構成を示します。これらのコード・スニペットは、基本的な設定および使用方法を理解するのに役立つ例です。
詳細な構成オプションについては、後述するTerraformのドキュメントを参照してください。
表1-1
| リソース | ハシコープ/aws | オラクル/アソシエーション |
|---|---|---|
| ODBネットワーク | aws_odb_network | 該当なし |
| Exadataインフラ | aws_odb_cloud_exadata_infrastructure | oci_database_cloud_exadata_infrastructure (更新のみ) |
| Exadata VMクラスタ | aws_odb_cloud_vm_cluster | oci_database_cloud_vm_cluster (更新のみ) |
| Autonomous VMクラスタ | aws_odb_cloud_autonomous_vm_cluster | oci_database_cloud_autonomous_vm_cluster (更新のみ) |
| ODBピアリング接続 | aws_odb_network_peering_connection | 該当なし |
| データベース・ホーム | 該当なし | oci_database_db_home |
| コンテナ・データベース(CDB) | 該当なし | oci_database_database |
| プラガブル・データベース(PDB) | 該当なし | oci_database_pluggable_database |
| Autonomous Container Database | 該当なし | oci_database_autonomous_container_database |
| Autonomous Database | 該当なし | oci_database_autonomous_database |
エンドツーエンドの例については、OCIマルチクラウド・ランディング・ゾーンfor AWS GitHub repoを参照してください。
AWS Terraform Providerを使用するリソース
AWS Terraform Providerの構成
# Configure the Terraform AWS Provider, version 6.15.0 or higher for ODB resources
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 6.15.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-west-2"
shared_config_files = ["~/.aws/config"]
profile = "OCI-Demo"
} ODBネットワークのプロビジョニング
# Create an ODB Network
resource "aws_odb_network" "this" {
# Required Arguments
display_name = "odb-network"
availability_zone_id = "usw2-az3"
client_subnet_cidr = "10.33.1.0/24"
backup_subnet_cidr = "10.33.0.0/24"
s3_access = "DISABLED"
zero_etl_access = "DISABLED"
# Optional Arguments
availability_zone = "us-west-2c"
region = "us-west-2"
tags = {
env = "demo"
}
} Exadataインフラストラクチャのプロビジョニング
# Create an Exadata Infrastructure
resource "aws_odb_cloud_exadata_infrastructure" "this" {
# Required Arguments
display_name = "exadb-inf-demo"
shape = "Exadata.X11M"
compute_count = 2
storage_count = 3
availability_zone_id = "usw2-az3"
# Optional Arguments
customer_contacts_to_send_to_oci = [
{ email = "demo@example.com" }
]
region = "us-west-2"
availability_zone = "us-west-2c"
database_server_type = "X11M"
storage_server_type = "X11M-HC"
tags = {
"env" = "dev"
}
maintenance_window {
patching_mode = "ROLLING"
preference = "NO_PREFERENCE"
is_custom_action_timeout_enabled = false
custom_action_timeout_in_mins = 15
days_of_week = null
hours_of_day = null
lead_time_in_weeks = null
months = null
weeks_of_month = null
}
}
# Get list of DB Servers for provisioning VM Cluster / Autonomous VM Cluster
data "aws_odb_db_servers" "this" {
cloud_exadata_infrastructure_id = aws_odb_cloud_exadata_infrastructure.this.id
}
# Optional output of OCIDs for configurations using OCI Terraform Provider
output "oci_region" {
value = regex("(?i:region=)([^?&/]+)", aws_odb_cloud_exadata_infrastructure.this.oci_url)[0]
}
output "oci_compartment_ocid" {
value = regex("(?i:compartmentId=)([^?&/]+)", aws_odb_cloud_exadata_infrastructure.this.oci_url)[0]
}
output "oci_tenant" {
value = regex("(?i:tenant=)([^?&/]+)", aws_odb_cloud_exadata_infrastructure.this.oci_url)[0]
}
output "oci_cloud_exadata_infrastructure_ocid" {
value = aws_odb_cloud_exadata_infrastructure.this.ocid
}Exadata VMクラスタのプロンプト
locals {
# IDs of depending resources
cloud_exadata_infrastructure_id = aws_odb_cloud_exadata_infrastructure.this.id
db_servers = data.aws_odb_db_servers.this.db_servers[*].id
odb_network_id = aws_odb_network.this.id
}
# Create a VM Cluster in the Exadata Infrastructure
resource "aws_odb_cloud_vm_cluster" "this" {
# Optional explicit dependencies
depends_on = [aws_odb_cloud_exadata_infrastructure.this, aws_odb_network.this]
# Required Arguments
cloud_exadata_infrastructure_id = local.cloud_exadata_infrastructure_id
cpu_core_count = 16
db_servers = local.db_servers
display_name = "tf-vmc-demo"
gi_version = "23.0.0.0"
hostname_prefix = "vm"
odb_network_id = local.odb_network_id
ssh_public_keys = [
file("~/.ssh/demo-ssh-key.pub")
]
data_collection_options {
is_diagnostics_events_enabled = true
is_health_monitoring_enabled = true
is_incident_logs_enabled = true
}
# Optional Arguments
cluster_name = "gic-demo"
data_storage_size_in_tbs = 2
db_node_storage_size_in_gbs = 120
is_local_backup_enabled = false
is_sparse_diskgroup_enabled = false
license_model = "BRING_YOUR_OWN_LICENSE"
memory_size_in_gbs = 60
scan_listener_port_tcp = 1521
timezone = "UTC"
region = "us-west-2"
tags = {
created_via = "terraform"
env = "demo"
}
# timeouts for long running operations
timeouts {
create = "24h"
update = "2h"
delete = "8h"
}
# gi_version will be updated with minor version, e.g. "23.8.0.25.04"
lifecycle {
ignore_changes = [
gi_version
]
}
}
# Output OCID of Exadata VM Cluster for Database Home creation (Optional)
output "oci_cloud_vm_cluster_ocid" {
value = aws_odb_cloud_vm_cluster.this.ocid
}Autonomous VMクラスタのプロビジョニング
locals {
# IDs of depending resources
cloud_exadata_infrastructure_id = aws_odb_cloud_exadata_infrastructure.this.id
db_servers = data.aws_odb_db_servers.this.db_servers[*].id
odb_network_id = aws_odb_network.this.id
}
# Create an Autonomous VM Cluster in the Exadata Infrastructure
resource "aws_odb_cloud_autonomous_vm_cluster" "this" {
# Optional explicit dependencies
depends_on = [aws_odb_cloud_exadata_infrastructure.this, aws_odb_network.this]
# Required Arguments
cloud_exadata_infrastructure_id = local.cloud_exadata_infrastructure_id
autonomous_data_storage_size_in_tbs = 5
cpu_core_count_per_node = 40
db_servers = local.db_servers
display_name = "tf-avmc-demo"
memory_per_oracle_compute_unit_in_gbs = 2
odb_network_id = local.odb_network_id
scan_listener_port_non_tls = 1521
scan_listener_port_tls = 2484
total_container_databases = 2
maintenance_window {
preference = "NO_PREFERENCE"
lead_time_in_weeks = null
days_of_week = null
hours_of_day = null
months = null
weeks_of_month = null
}
# Optional Arguments
description = "Autonomous VM Cluster"
is_mtls_enabled_vm_cluster = true
license_model = "BRING_YOUR_OWN_LICENSE"
time_zone = "UTC"
region = "us-west-2"
tags = {
created_via = "terraform"
env = "demo"
}
}
# Output OCID of Autonomous VM Cluster for the creation of Autonomous Container Database (Optional)
output "oci_cloud_autonomous_vm_cluster_ocid" {
value = aws_odb_cloud_autonomous_vm_cluster.this.ocid
}ODBピアリング接続のプロビジョニング
locals {
# IDs of depending resources
peer_network_id = module.app_vpc.vpc_attributes.id
odb_network_id = aws_odb_network.this.id
}
# Create a Peering Connection between the ODB Network and the VPC
resource "aws_odb_network_peering_connection" "this" {
# Optional explicit dependencies
depends_on = [ aws_odb_network.this, module.app_vpc ]
# Required Arguments
odb_network_id = local.odb_network_id
peer_network_id = local.peer_network_id
display_name = "tf-odb-peering-conn-demo"
# Optional Arguments
region = "us-west-2"
tags = {
created_via = "terraform"
env = "demo"
}
}OCI Terraform Providerを使用するリソース
OCI Terraform Providerの構成
# https://docs.oracle.com/en-us/iaas/Content/terraform/configuring.htm
provider "oci" {
auth = "APIKey"
region = "us-boardman-1"
tenancy_ocid = "ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
user_ocid = "ocid1.user.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
fingerprint = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
private_key_path = "your_private_key_path"
private_key_password = var.oci_private_key_password
}
# Protect sensitive input variables
variable "oci_private_key_password" {
type = string
sensitive = true
}Exadata Databaseのプロンプト
locals {
# IDs of depending resources
oci_cloud_vm_cluster_ocid = aws_odb_cloud_vm_cluster.this.ocid
}
# Protect sensitive input variables
variable "db_admin_password" {
description = "Database administrator password"
type = string
sensitive = true
}
# Create Database Home
resource "oci_database_db_home" "this" {
vm_cluster_id = var.oci_cloud_vm_cluster_ocid
source = "VM_CLUSTER_NEW"
display_name = "dbh19"
db_version = "19.28.0.0.0"
}
# Create Container Database
resource "oci_database_database" "this" {
db_home_id = oci_database_db_home.this.id
database {
db_name = "democdb"
admin_password = var.db_admin_password
}
source = "NONE"
}
# Create Pluggable Database
resource "oci_database_pluggable_database" "this" {
container_database_id = oci_database_database.this.id
pdb_name = "demopdb"
pdb_admin_password = var.db_admin_password
tde_wallet_password = var.db_admin_password
}Autonomous Databaseのプロビジョニング
locals {
# IDs of depending resources
oci_cloud_autonomous_vm_cluster_ocid = aws_odb_cloud_autonomous_vm_cluster.this.ocid
oci_compartment_ocid = regex("(?i:compartmentId=)([^?&/]+)", aws_odb_cloud_autonomous_vm_cluster.this.oci_url)[0]
}
# Protect sensitive input variables
variable "db_admin_password" {
description = "Database administrator password"
type = string
sensitive = true
}
# Create the Autonomous Container Database
resource "oci_database_autonomous_container_database" "this" {
cloud_autonomous_vm_cluster_id = local.oci_cloud_autonomous_vm_cluster_ocid
display_name = "demo-acd-01"
patch_model = "RELEASE_UPDATES"
}
# Create the Autonomous Database
resource "oci_database_autonomous_database" "this" {
admin_password = var.db_admin_password
compartment_id = local.oci_compartment_ocid
db_name = "demo-adbd-01"
compute_model = "ECPU"
compute_count = 2
data_storage_size_in_tbs = 1
}OCI Terraform Providerを使用した構成更新のリソースのインポート
- Oracle Database@AWSリソースのTerraformインポート・ブロックの宣言
# import Exadata Infrastructure import { to = oci_database_cloud_exadata_infrastructure.this id = "ocid1.cloudexadatainfrastructure.oc1.us-boardman-1.xxxxxxxxxxxxxxxxxx" } # import Cloud VM Cluster import { to = oci_database_cloud_vm_cluster.this id = ""ocid1.cloudvmcluster.oc1.us-boardman-1.xxxxxxxxxxxxxxxxxx" } # import Cloud Autonomous VM Cluster import { to = oci_database_cloud_autonomous_vm_cluster.this id = "ocid1.cloudautonomousvmcluster.oc1.us-boardman-1.xxxxxxxxxxxxxxxxxx" } - OCIリソースとしてのTerraform構成の生成
terraform plan -generate-config-out=generated.tf - Terraformインポートまたは構成更新の適用
terraform apply
破棄前の状態からのインポート済OCIリソースの削除(AWS Terraform Provider)
terraform state rm oci_database_cloud_exadata_infrastructure.this
terraform state rm oci_database_cloud_vm_cluster.this
terraform state rm oci_database_cloud_autonomous_vm_cluster.this