Terraform : Oracle Cloud Infrastructure (OCI) Compute Instance
This article describes how to create a compute instance on Oracle Cloud Infrastructure (OCI) using Terraform.
oracle miscconfigurationintermediate
by OracleDba
13 views
This article describes how to create a compute instance on Oracle Cloud Infrastructure (OCI) using Terraform.
12345
mkdir \git\oraclebase\terraform\oci\oci_compute
cd \git\oraclebase\terraform\oci\oci_compute
copy \git\oraclebase\terraform\oci\oci_provider\*.tf .
copy \git\oraclebase\terraform\oci\oci_provider\*.tfvars .123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
# Variables
variable "compartment_id" { type = string }
variable "compute_name" { type = string }
variable "compute_subnet_id" { type = string }
variable "compute_image_id" { type = string }
variable "compute_ssh_authorized_keys" { type = string }
variable "compute_shape" {
type = string
default = "VM.Standard.E2.1.Micro"
}
variable "compute_cpus" {
type = string
default = "1"
}
variable "compute_memory_in_gbs" {
type = string
default = "1"
}
# Resources
data "oci_identity_availability_domains" "ads" {
compartment_id = var.compartment_id
}
resource "oci_core_instance" "tf_compute" {
# Required
availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
compartment_id = var.compartment_id
shape = var.compute_shape
source_details {
source_id = var.compute_image_id
source_type = "image"
}
# Optional
display_name = var.compute_name
shape_config {
ocpus = var.compute_cpus
memory_in_gbs = var.compute_memory_in_gbs
}
create_vnic_details {
subnet_id = var.compute_subnet_id
assign_public_ip = true
}
metadata = {
ssh_authorized_keys = file(var.compute_ssh_authorized_keys)
}
preserve_boot_volume = false
agent_config {
#Optional
is_management_disabled = false
plugins_config {
#Required
desired_state = "ENABLED"
name = "Block Volume Management"
}
}
}
# Outputs
output "compute_id" {
value = oci_core_instance.tf_compute.id
}
output "db_state" {
value = oci_core_instance.tf_compute.state
}
output "compute_public_ip" {
value = oci_core_instance.tf_compute.public_ip
}123456
compartment_id = "ocid1.compartment.oc1..aaaaaaaa..."
compute_shape = "VM.Standard.E2.1.Micro"
compute_name = "obvm1"
compute_subnet_id = "ocid1.subnet.oc1.uk-london-1.aaaaaaaa..."
compute_image_id = "ocid1.image.oc1.uk-london-1.aaaaaaaa..."
compute_ssh_authorized_keys = "./myOracleCloudKey.pub"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
terraform init
terraform plan
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# oci_core_instance.tf_compute will be created
+ resource "oci_core_instance" "tf_compute" {
+ availability_domain = "oVQK:UK-LONDON-1-AD-1"
+ boot_volume_id = (known after apply)
+ compartment_id = "ocid1.compartment.oc1..aaaaaaaa.."
+ dedicated_vm_host_id = (known after apply)
+ defined_tags = (known after apply)
+ display_name = "obvm2"
+ fault_domain = (known after apply)
+ freeform_tags = (known after apply)
+ hostname_label = (known after apply)
+ id = (known after apply)
+ image = (known after apply)
+ ipxe_script = (known after apply)
+ is_pv_encryption_in_transit_enabled = (known after apply)
+ launch_mode = (known after apply)
+ metadata = {
+ "ssh_authorized_keys" = <<-EOT
ssh-rsa AAAAB3Nza...nElEbgK/ username@machine-name
EOT
}
+ preserve_boot_volume = false
+ private_ip = (known after apply)
+ public_ip = (known after apply)
+ region = (known after apply)
+ shape = "VM.Standard.E2.1.Micro"
+ state = (known after apply)
+ subnet_id = (known after apply)
+ system_tags = (known after apply)
+ time_created = (known after apply)
+ time_maintenance_reboot_due = (known after apply)
+ agent_config {
+ are_all_plugins_disabled = (known after apply)
+ is_management_disabled = (known after apply)
+ is_monitoring_disabled = (known after apply)
+ plugins_config {
+ desired_state = (known after apply)
+ name = (known after apply)
}
}
+ availability_config {
+ recovery_action = (known after apply)
}
+ create_vnic_details {
+ assign_public_ip = "true"
+ defined_tags = (known after apply)
+ display_name = (known after apply)
+ freeform_tags = (known after apply)
+ hostname_label = (known after apply)
+ private_ip = (known after apply)
+ skip_source_dest_check = (known after apply)
+ subnet_id = "ocid1.subnet.oc1.uk-london-1.aaaaaaaa..."
+ vlan_id = (known after apply)
}
+ instance_options {
+ are_legacy_imds_endpoints_disabled = (known after apply)
}
+ launch_options {
+ boot_volume_type = (known after apply)
+ firmware = (known after apply)
+ is_consistent_volume_naming_enabled = (known after apply)
+ is_pv_encryption_in_transit_enabled = (known after apply)
+ network_type = (known after apply)
+ remote_data_volume_type = (known after apply)
}
+ platform_config {
+ numa_nodes_per_socket = (known after apply)
+ type = (known after apply)
}
+ shape_config {
+ gpu_description = (known after apply)
+ gpus = (known after apply)
+ local_disk_description = (known after apply)
+ local_disks = (known after apply)
+ local_disks_total_size_in_gbs = (known after apply)
+ max_vnic_attachments = (known after apply)
+ memory_in_gbs = 1
+ networking_bandwidth_in_gbps = (known after apply)
+ ocpus = 1
+ processor_description = (known after apply)
}
+ source_details {
+ boot_volume_size_in_gbs = (known after apply)
+ kms_key_id = (known after apply)
+ source_id = "ocid1.image.oc1.uk-london-1.aaaaaaaa..."
+ source_type = "image"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ compute_id = (known after apply)
+ compute_public_ip = (known after apply)
+ db_state = (known after apply)
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# oci_core_instance.tf_compute will be created
+ resource "oci_core_instance" "tf_compute" {
+ availability_domain = "oVQK:UK-LONDON-1-AD-1"
+ boot_volume_id = (known after apply)
+ compartment_id = "ocid1.compartment.oc1..aaaaaaaa..."
+ dedicated_vm_host_id = (known after apply)
+ defined_tags = (known after apply)
+ display_name = "obvm2"
+ fault_domain = (known after apply)
+ freeform_tags = (known after apply)
+ hostname_label = (known after apply)
+ id = (known after apply)
+ image = (known after apply)
+ ipxe_script = (known after apply)
+ is_pv_encryption_in_transit_enabled = (known after apply)
+ launch_mode = (known after apply)
+ metadata = {
+ "ssh_authorized_keys" = <<-EOT
ssh-rsa AAAAB3Nza...nElEbgK/ username@machine-name
EOT
}
+ preserve_boot_volume = false
+ private_ip = (known after apply)
+ public_ip = (known after apply)
+ region = (known after apply)
+ shape = "VM.Standard.E2.1.Micro"
+ state = (known after apply)
+ subnet_id = (known after apply)
+ system_tags = (known after apply)
+ time_created = (known after apply)
+ time_maintenance_reboot_due = (known after apply)
+ agent_config {
+ are_all_plugins_disabled = (known after apply)
+ is_management_disabled = (known after apply)
+ is_monitoring_disabled = (known after apply)
+ plugins_config {
+ desired_state = (known after apply)
+ name = (known after apply)
}
}
+ availability_config {
+ recovery_action = (known after apply)
}
+ create_vnic_details {
+ assign_public_ip = "true"
+ defined_tags = (known after apply)
+ display_name = (known after apply)
+ freeform_tags = (known after apply)
+ hostname_label = (known after apply)
+ private_ip = (known after apply)
+ skip_source_dest_check = (known after apply)
+ subnet_id = "ocid1.subnet.oc1.uk-london-1.aaaaaaaa..."
+ vlan_id = (known after apply)
}
+ instance_options {
+ are_legacy_imds_endpoints_disabled = (known after apply)
}
+ launch_options {
+ boot_volume_type = (known after apply)
+ firmware = (known after apply)
+ is_consistent_volume_naming_enabled = (known after apply)
+ is_pv_encryption_in_transit_enabled = (known after apply)
+ network_type = (known after apply)
+ remote_data_volume_type = (known after apply)
}
+ platform_config {
+ numa_nodes_per_socket = (known after apply)
+ type = (known after apply)
}
+ shape_config {
+ gpu_description = (known after apply)
+ gpus = (known after apply)
+ local_disk_description = (known after apply)
+ local_disks = (known after apply)
+ local_disks_total_size_in_gbs = (known after apply)
+ max_vnic_attachments = (known after apply)
+ memory_in_gbs = 1
+ networking_bandwidth_in_gbps = (known after apply)
+ ocpus = 1
+ processor_description = (known after apply)
}
+ source_details {
+ boot_volume_size_in_gbs = (known after apply)
+ kms_key_id = (known after apply)
+ source_id = "ocid1.image.oc1.uk-london-1.aaaaaaaa..."
+ source_type = "image"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ compute_id = (known after apply)
+ compute_public_ip = (known after apply)
+ 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
oci_core_instance.tf_compute: Creating...
oci_core_instance.tf_compute: Still creating... [10s elapsed]
oci_core_instance.tf_compute: Still creating... [20s elapsed]
oci_core_instance.tf_compute: Still creating... [30s elapsed]
oci_core_instance.tf_compute: Still creating... [40s elapsed]
oci_core_instance.tf_compute: Still creating... [50s elapsed]
oci_core_instance.tf_compute: Still creating... [1m0s elapsed]
oci_core_instance.tf_compute: Still creating... [1m10s elapsed]
oci_core_instance.tf_compute: Still creating... [1m20s elapsed]
oci_core_instance.tf_compute: Still creating... [1m30s elapsed]
oci_core_instance.tf_compute: Creation complete after 1m37s [id=ocid1.instance.oc1.uk-london-1.anwgiljt...]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
compute_id = "ocid1.instance.oc1.uk-london-1.anwgiljt..."
compute_public_ip = "XXX.XXX.XX.XX"
db_state = "RUNNING"Please to add comments
No comments yet. Be the first to comment!