-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
167 lines (144 loc) · 4.89 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
terraform {
required_providers {
oci = {
source = "oracle/oci"
version = "5.36.0"
}
}
}
provider "oci" {
region = var.region
auth = "SecurityToken" # Ensure this is the intended auth method
config_file_profile = "terraform" # Profile from OCI config file
}
# Select instance shape based on workspace
locals {
ssh_username = terraform.workspace == "prod" ? "opc" : "ubuntu"
instance_shape = terraform.workspace == "prod" ? "VM.Standard.A1.Flex" : "VM.Standard.E2.1.Micro"
cpu_cores_count = terraform.workspace == "prod" ? "4" : "1"
memory_in_gbs = terraform.workspace == "prod" ? "24" : "1"
boot_volume_vpus_per_gb = terraform.workspace == "prod" ? "120" : "80"
os_image_ocid = terraform.workspace == "prod" ? "ocid1.image.oc1.ap-hyderabad-1.aaaaaaaathmmgma5b3mcjauheqmanwmz6xrmqpj6bviqgmhoyga6tfrthfna" : "ocid1.image.oc1.ap-hyderabad-1.aaaaaaaa2klsv3o5lxeyrpz32brdp4gy5wo4cgq6fxazxcxynptut3dxllwa"
server_type = terraform.workspace == "prod" ? "arm" : "x86"
instance_public_ip = oci_core_instance.instance.public_ip
instance_ids = oci_core_instance.instance[*].id
}
data "oci_identity_availability_domains" "ads" {
compartment_id = var.tenancy_ocid
}
resource "oci_core_vcn" "terraform_vcn" {
cidr_block = "10.0.0.0/16"
compartment_id = var.compartment_ocid
display_name = "Terraform VCN"
dns_label = "tfvcn"
}
resource "oci_core_internet_gateway" "internet_gateway" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.terraform_vcn.id
display_name = "Internet Gateway"
}
resource "oci_core_route_table" "public_route_table" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.terraform_vcn.id
display_name = "Public Route Table"
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.internet_gateway.id
}
}
resource "oci_core_security_list" "allow_ssh_http_https" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.terraform_vcn.id
display_name = "Allow SSH"
egress_security_rules {
destination = "0.0.0.0/0"
protocol = "all"
}
ingress_security_rules {
source = "0.0.0.0/0" # Or a specific IP range
protocol = "6" # TCP
tcp_options {
min = 22
max = 22
}
}
ingress_security_rules {
source = "0.0.0.0/0" # Or a specific IP range
protocol = "6" # TCP
tcp_options {
min = 80
max = 80
}
}
ingress_security_rules {
source = "0.0.0.0/0" # Or a specific IP range
protocol = "6" # TCP
tcp_options {
min = 443
max = 443
}
}
}
resource "oci_core_subnet" "terraform_subnet" {
cidr_block = "10.0.1.0/24"
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.terraform_vcn.id
display_name = "Terraform Subnet"
security_list_ids = [oci_core_security_list.allow_ssh_http_https.id]
route_table_id = oci_core_route_table.public_route_table.id
}
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}-${terraform.workspace}"
shape = local.instance_shape
source_details {
source_type = "image"
source_id = local.os_image_ocid
boot_volume_vpus_per_gb = local.boot_volume_vpus_per_gb
}
shape_config {
memory_in_gbs = local.memory_in_gbs
ocpus = local.cpu_cores_count
}
create_vnic_details {
subnet_id = oci_core_subnet.terraform_subnet.id
assign_public_ip = true # Assign a public IP address to the instance
}
metadata = {
ssh_authorized_keys = file(var.ssh_public_key)
}
}
output "instance_public_ip" {
value = local.instance_public_ip
}
module "ansible" {
source = "./modules/ansible"
ssh_private_key = var.ssh_private_key
ssh_username = local.ssh_username
instance_ip = local.instance_public_ip
}
module "ddns" {
count = var.ddns ? 1 : 0
source = "./modules/ddns"
domain = var.domain
namecheap_api_key = var.namecheap_api_key
subdomains = var.subdomains
}
module "setup" {
count = var.setup ? 1 : 0
source = "./modules/setup"
ssh_private_key = var.ssh_private_key
ssh_username = local.ssh_username
instance_ids = local.instance_ids
instance_public_ip = local.instance_public_ip
}
module "rebuild" {
count = var.rebuild ? 1 : 0
source = "./modules/rebuild"
instance_public_ip = local.instance_public_ip
instance_ids = local.instance_ids
ssh_private_key = var.ssh_private_key
nixos_flake = var.nixos_flake
}