forked from frennky/terraform-oci-free
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
174 lines (142 loc) · 5 KB
/
main.tf
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
terraform {
required_version = ">= 1.10.4"
required_providers {
oci = {
source = "oracle/oci"
version = ">= 6.21.0"
}
tls = {
source = "hashicorp/tls"
version = "4.0.6"
}
}
backend "s3" {
bucket = "tfstate"
key = "free/terraform.tfstate"
shared_credentials_files = ["credentials"]
profile = "default"
skip_region_validation = true
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
skip_s3_checksum = true
use_path_style = true
}
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs
provider "oci" {
user_ocid = var.user
fingerprint = var.fingerprint
tenancy_ocid = var.tenancy
region = var.region
private_key_path = var.key_file
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/identity_compartment
resource "oci_identity_compartment" "free_compartment" {
compartment_id = var.tenancy
description = "Oracle Cloud Free Tier compartment"
name = "free"
enable_delete = true
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_vcn
resource "oci_core_vcn" "free_vcn" {
cidr_block = "10.0.0.0/16"
ipv6private_cidr_blocks = ["fd00:10::/48"]
is_ipv6enabled = true
compartment_id = oci_identity_compartment.free_compartment.id
display_name = "freeVCN"
dns_label = "freevcn"
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_subnet
resource "oci_core_subnet" "free_subnet" {
cidr_block = "10.0.20.0/24"
ipv6cidr_block = cidrsubnet(oci_core_vcn.free_vcn.ipv6cidr_blocks[0], 8, 0) # Expand /56 to /64
display_name = "freeSubnet"
dns_label = "freesubnet"
security_list_ids = [oci_core_security_list.free_security_list.id]
compartment_id = oci_identity_compartment.free_compartment.id
vcn_id = oci_core_vcn.free_vcn.id
route_table_id = oci_core_route_table.free_route_table.id
dhcp_options_id = oci_core_vcn.free_vcn.default_dhcp_options_id
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_internet_gateway
resource "oci_core_internet_gateway" "free_internet_gateway" {
compartment_id = oci_identity_compartment.free_compartment.id
display_name = "freeIG"
vcn_id = oci_core_vcn.free_vcn.id
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_route_table
resource "oci_core_route_table" "free_route_table" {
compartment_id = oci_identity_compartment.free_compartment.id
vcn_id = oci_core_vcn.free_vcn.id
display_name = "freeRouteTable"
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.free_internet_gateway.id
}
route_rules {
destination = "::/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.free_internet_gateway.id
}
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_security_list
resource "oci_core_security_list" "free_security_list" {
compartment_id = oci_identity_compartment.free_compartment.id
vcn_id = oci_core_vcn.free_vcn.id
display_name = "freeSecurityList"
egress_security_rules {
protocol = "6"
destination = "0.0.0.0/0"
}
egress_security_rules {
protocol = "6"
destination = "::/0"
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
tcp_options {
max = "22"
min = "22"
}
}
ingress_security_rules {
protocol = "6"
source = "::/0"
tcp_options {
max = "22"
min = "22"
}
}
}
resource "tls_private_key" "instance_ssh_key" {
count = var.instance_public_key_path != "" ? 1 : 0
algorithm = "ED25519"
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_instance
resource "oci_core_instance" "free_instance" {
availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
compartment_id = oci_identity_compartment.free_compartment.id
shape = var.instance_shape
display_name = "freeInstance"
shape_config {
memory_in_gbs = var.instance_memory
ocpus = var.instance_ocpus
}
source_details {
source_id = data.oci_core_images.instance_images.images[0].id
source_type = "image"
boot_volume_size_in_gbs = var.instance_boot_volume_size
}
create_vnic_details {
assign_public_ip = true
assign_ipv6ip = true
subnet_id = oci_core_subnet.free_subnet.id
display_name = var.instance_hostname
}
metadata = {
ssh_authorized_keys = (var.instance_public_key_path != "") ? file("${var.instance_public_key_path}") : tls_private_key.instance_ssh_key[1].public_key_openssh
}
}