|
| 1 | +variable "tenancy_ocid" {} |
| 2 | +variable "user_ocid" {} |
| 3 | +variable "fingerprint" {} |
| 4 | +variable "private_key_path" {} |
| 5 | +variable "region" {} |
| 6 | + |
| 7 | +variable "compartment_ocid" {} |
| 8 | +variable "ssh_public_key" {} |
| 9 | +variable "ssh_private_key" {} |
| 10 | + |
| 11 | +# Choose an Availability Domain |
| 12 | +variable "AD" { |
| 13 | + default = "1" |
| 14 | +} |
| 15 | + |
| 16 | +variable "InstanceImageOCID" { |
| 17 | + type = "map" |
| 18 | + default = { |
| 19 | + // See https://docs.us-phoenix-1.oraclecloud.com/images/ |
| 20 | + // Oracle-provided image "Oracle-Linux-7.4-2018.02.21-1" |
| 21 | + us-phoenix-1 = "ocid1.image.oc1.phx.aaaaaaaaupbfz5f5hdvejulmalhyb6goieolullgkpumorbvxlwkaowglslq" |
| 22 | + us-ashburn-1 = "ocid1.image.oc1.iad.aaaaaaaajlw3xfie2t5t52uegyhiq2npx7bqyu4uvi2zyu3w3mqayc2bxmaa" |
| 23 | + eu-frankfurt-1 = "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaa7d3fsb6272srnftyi4dphdgfjf6gurxqhmv6ileds7ba3m2gltxq" |
| 24 | + uk-london-1 = "ocid1.image.oc1.uk-london-1.aaaaaaaaa6h6gj6v4n56mqrbgnosskq63blyv2752g36zerymy63cfkojiiq" |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +variable "InstanceShape" { |
| 29 | + default = "VM.Standard1.2" |
| 30 | +} |
| 31 | + |
| 32 | +provider "oci" { |
| 33 | + tenancy_ocid = "${var.tenancy_ocid}" |
| 34 | + user_ocid = "${var.user_ocid}" |
| 35 | + fingerprint = "${var.fingerprint}" |
| 36 | + private_key_path = "${var.private_key_path}" |
| 37 | + region = "${var.region}" |
| 38 | +} |
| 39 | + |
| 40 | +# Gets a list of Availability Domains |
| 41 | +data "oci_identity_availability_domains" "ADs" { |
| 42 | + compartment_id = "${var.tenancy_ocid}" |
| 43 | +} |
| 44 | + |
| 45 | +resource "oci_core_virtual_network" "ExampleVCN" { |
| 46 | + cidr_block = "10.1.0.0/16" |
| 47 | + compartment_id = "${var.compartment_ocid}" |
| 48 | + display_name = "TFExampleVCN" |
| 49 | + dns_label = "tfexamplevcn" |
| 50 | +} |
| 51 | + |
| 52 | +resource "oci_core_subnet" "ExampleSubnet" { |
| 53 | + availability_domain = "${lookup(data.oci_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}" |
| 54 | + cidr_block = "10.1.20.0/24" |
| 55 | + display_name = "TFExampleSubnet" |
| 56 | + dns_label = "tfexamplesubnet" |
| 57 | + security_list_ids = ["${oci_core_virtual_network.ExampleVCN.default_security_list_id}"] |
| 58 | + compartment_id = "${var.compartment_ocid}" |
| 59 | + vcn_id = "${oci_core_virtual_network.ExampleVCN.id}" |
| 60 | + route_table_id = "${oci_core_virtual_network.ExampleVCN.default_route_table_id}" |
| 61 | + dhcp_options_id = "${oci_core_virtual_network.ExampleVCN.default_dhcp_options_id}" |
| 62 | +} |
| 63 | + |
| 64 | +resource "oci_core_internet_gateway" "ExampleIG" { |
| 65 | + compartment_id = "${var.compartment_ocid}" |
| 66 | + display_name = "TFExampleIG" |
| 67 | + vcn_id = "${oci_core_virtual_network.ExampleVCN.id}" |
| 68 | +} |
| 69 | + |
| 70 | +resource "oci_core_default_route_table" "ExampleRT" { |
| 71 | + manage_default_resource_id = "${oci_core_virtual_network.ExampleVCN.default_route_table_id}" |
| 72 | + route_rules { |
| 73 | + cidr_block = "0.0.0.0/0" |
| 74 | + network_entity_id = "${oci_core_internet_gateway.ExampleIG.id}" |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +# Gets a list of vNIC attachments on the instance |
| 79 | +data "oci_core_vnic_attachments" "InstanceVnics" { |
| 80 | + compartment_id = "${var.compartment_ocid}" |
| 81 | + availability_domain = "${lookup(data.oci_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}" |
| 82 | + instance_id = "${oci_core_instance.TFInstance.id}" |
| 83 | +} |
| 84 | + |
| 85 | +# Gets the OCID of the first (default) vNIC |
| 86 | +data "oci_core_vnic" "InstanceVnic" { |
| 87 | + vnic_id = "${lookup(data.oci_core_vnic_attachments.InstanceVnics.vnic_attachments[0],"vnic_id")}" |
| 88 | +} |
| 89 | + |
| 90 | +resource "oci_core_instance" "TFInstance" { |
| 91 | + availability_domain = "${lookup(data.oci_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}" |
| 92 | + compartment_id = "${var.compartment_ocid}" |
| 93 | + display_name = "TFInstance" |
| 94 | + shape = "${var.InstanceShape}" |
| 95 | + |
| 96 | + create_vnic_details { |
| 97 | + subnet_id = "${oci_core_subnet.ExampleSubnet.id}" |
| 98 | + display_name = "primaryvnic" |
| 99 | + assign_public_ip = true |
| 100 | + hostname_label = "tfexampleinstance" |
| 101 | + } |
| 102 | + |
| 103 | + source_details { |
| 104 | + source_type = "image" |
| 105 | + source_id = "${var.InstanceImageOCID[var.region]}" |
| 106 | + } |
| 107 | + |
| 108 | + metadata { |
| 109 | + ssh_authorized_keys = "${var.ssh_public_key}" |
| 110 | + } |
| 111 | + |
| 112 | + timeouts { |
| 113 | + create = "60m" |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +resource "oci_core_instance_console_connection" "test_instance_console_connection" { |
| 118 | + #Required |
| 119 | + instance_id = "${oci_core_instance.TFInstance.id}" |
| 120 | + public_key = "${var.ssh_public_key}" |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +output "connect_with_ssh" { |
| 125 | + value = "${oci_core_instance_console_connection.test_instance_console_connection.connection_string}" |
| 126 | +} |
| 127 | + |
| 128 | +output "connect_with_vnc" { |
| 129 | + value = "${oci_core_instance_console_connection.test_instance_console_connection.vnc_connection_string}" |
| 130 | +} |
| 131 | + |
| 132 | + |
0 commit comments