-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.tf
129 lines (106 loc) · 3.7 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
# Create datasource of images from the image list
data "oci_core_images" "images" {
compartment_id = var.compartment_ocid
operating_system = "Oracle Linux"
operating_system_version = "8"
filter {
name = "display_name"
values = ["^.*-GPU-.*$"]
regex = true
}
}
# Create a compute instance with a public IP address using oci provider
resource "oci_core_instance" "instance" {
availability_domain = data.oci_identity_availability_domains.ADs.availability_domains[0].name
compartment_id = var.compartment_ocid
display_name = var.instance_name
shape = var.instance_shape
source_details {
source_type = "image"
source_id = data.oci_core_images.images.images[0].id
boot_volume_size_in_gbs = 100
}
create_vnic_details {
assign_public_ip = "true"
subnet_id = oci_core_subnet.subnet.id
}
# Add private key
metadata = {
ssh_authorized_keys = file(var.ssh_public_key_path)
user_data = base64encode(file("setup-instance.sh"))
}
}
# Create datasource for availability domains
data "oci_identity_availability_domains" "ADs" {
compartment_id = var.compartment_ocid
}
# Create internet gateway
resource "oci_core_internet_gateway" "internet_gateway" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_virtual_network.generative_ai_vcn.id
display_name = "generative-ai-internet-gateway"
}
# Create route table
resource "oci_core_route_table" "generative_ai_route_table" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_virtual_network.generative_ai_vcn.id
display_name = "generative-ai-route-table"
route_rules {
destination = "0.0.0.0/0"
network_entity_id = oci_core_internet_gateway.internet_gateway.id
}
}
# Create security list with ingress and egress rules
resource "oci_core_security_list" "generative_ai_security_list" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_virtual_network.generative_ai_vcn.id
display_name = "generative-ai-security-list"
egress_security_rules {
destination = "0.0.0.0/0"
protocol = "all"
description = "Allow all outbound traffic"
}
ingress_security_rules {
protocol = "all"
source = "0.0.0.0/0"
description = "Allow all inbound traffic"
}
# ingress rule for ssh
ingress_security_rules {
protocol = "6" # tcp
source = "0.0.0.0/0"
description = "Allow ssh"
tcp_options {
max = 22
min = 22
}
}
}
# Create a subnet
resource "oci_core_subnet" "subnet" {
cidr_block = var.subnet_cidr
compartment_id = var.compartment_ocid
display_name = "generative-ai-subnet"
vcn_id = oci_core_virtual_network.generative_ai_vcn.id
route_table_id = oci_core_route_table.generative_ai_route_table.id
security_list_ids = ["${oci_core_security_list.generative_ai_security_list.id}"]
dhcp_options_id = oci_core_virtual_network.generative_ai_vcn.default_dhcp_options_id
}
# Create a virtual network
resource "oci_core_virtual_network" "generative_ai_vcn" {
cidr_block = var.vcn_cidr
compartment_id = var.compartment_ocid
display_name = "generative-ai-vcn"
}
output "instance_public_ip" {
value = <<EOF
Wait 25 minutes for the instance to be ready.
ssh -i server.key opc@${oci_core_instance.instance.public_ip}
ssh tunnel =>
ssh -i server.key -L 7860:localhost:7860 -L 5000:localhost:5000 -L 3000:localhost:3000 -L 4000:localhost:4000 opc@${oci_core_instance.instance.public_ip}
Setup and dreambooth => http://localhost:3000
stable diffusion => http://localhost:7860
bloom => http://localhost:5000
automatic-image-processing => http://localhost:4000
EOF
}