-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
111 lines (98 loc) · 5.33 KB
/
Copy pathmain.tf
File metadata and controls
111 lines (98 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -----------------------------------------------------------------------------
# Provider: Oracle Cloud Infrastructure (OCI)
# Documentation: https://registry.terraform.io/providers/oracle/oci/latest/docs
# -----------------------------------------------------------------------------
provider "oci" {
tenancy_ocid = var.tenancy_ocid # Find in OCI: https://docs.oracle.com/en-us/iaas/Content/General/Concepts/identifiers.htm#tenancy_ocid
user_ocid = var.user_ocid # Find in OCI: https://docs.oracle.com/en-us/iaas/Content/General/Concepts/identifiers.htm#user_ocid
fingerprint = var.fingerprint # How to generate: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm#How
private_key_path = "${path.cwd}/private-key.pem" # How to create: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm#how
region = var.region # List of region names: https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm
}
# -----------------------------------------------------------------------------
# Data Source: Get Availability Domains
# https://docs.oracle.com/en-us/iaas/Content/General/Concepts/regions.htm#AboutAD
# -----------------------------------------------------------------------------
data "oci_identity_availability_domains" "ads" {
compartment_id = var.compartment_ocid # Use your tenancy OCID (see above) if unsure.
}
# -----------------------------------------------------------------------------
# Data Source: Latest Ubuntu 24.04 ARM64 image
# Image list: https://docs.oracle.com/iaas/images/
# -----------------------------------------------------------------------------
data "oci_core_images" "ubuntu" {
compartment_id = var.compartment_ocid
operating_system = "Canonical Ubuntu"
operating_system_version = "24.04"
shape = "VM.Standard.A1.Flex"
sort_by = "TIMECREATED"
sort_order = "DESC"
}
# -----------------------------------------------------------------------------
# Virtual Cloud Network (VCN): https://docs.oracle.com/en-us/iaas/Content/Network/Tasks/managingVCNs.htm
# -----------------------------------------------------------------------------
resource "oci_core_vcn" "main" {
compartment_id = var.compartment_ocid
display_name = "main-vcn"
cidr_block = "10.0.0.0/16"
}
# -----------------------------------------------------------------------------
# Internet Gateway: https://docs.oracle.com/en-us/iaas/Content/Network/Tasks/managingIGs.htm
# -----------------------------------------------------------------------------
resource "oci_core_internet_gateway" "igw" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.main.id
display_name = "main-internet-gateway"
}
# -----------------------------------------------------------------------------
# Subnet: https://docs.oracle.com/en-us/iaas/Content/Network/Tasks/managingsubnets.htm
# -----------------------------------------------------------------------------
resource "oci_core_subnet" "public_subnet" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.main.id
cidr_block = "10.0.1.0/24"
display_name = "public-subnet"
availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
route_table_id = oci_core_vcn.main.default_route_table_id
dhcp_options_id = oci_core_vcn.main.default_dhcp_options_id
security_list_ids = [oci_core_vcn.main.default_security_list_id]
prohibit_public_ip_on_vnic = false
}
# -----------------------------------------------------------------------------
# Compute Instance: https://docs.oracle.com/en-us/iaas/Content/Compute/Tasks/launchinginstance.htm
# -----------------------------------------------------------------------------
resource "oci_core_instance" "ampere_a1" {
compartment_id = var.compartment_ocid
availability_domain = data.oci_identity_availability_domains.ads.availability_domains[2].name
shape = "VM.Standard.A1.Flex" # Always Free Ampere A1 shape
shape_config {
ocpus = 4 # Max Always Free for A1
memory_in_gbs = 24 # Max Always Free for A1
}
create_vnic_details {
assign_public_ip = true
subnet_id = oci_core_subnet.public_subnet.id
}
metadata = {
ssh_authorized_keys = file(var.public_key_path) # Generate/view SSH key: https://docs.oracle.com/en-us/iaas/Content/Compute/Tasks/managingkeypairs.htm
}
source_details {
source_type = "image"
source_id = data.oci_core_images.ubuntu.images[0].id
}
display_name = "ubuntu24-ampere-fullfree"
}
# -----------------------------------------------------------------------------
# Output: Public IP
# -----------------------------------------------------------------------------
output "instance_public_ip" {
value = oci_core_instance.ampere_a1.public_ip
description = "The public IP address of your Ubuntu 24.04 Always Free Ampere VM."
}
# -----------------------------------------------------------------------------
# Output: Ubuntu 24.04 Image OCID used
# -----------------------------------------------------------------------------
output "ubuntu_image_id" {
value = data.oci_core_images.ubuntu.images[0].id
description = "OCID of the Ubuntu 24.04 ARM image used."
}