-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.tf
165 lines (139 loc) · 5.04 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
provider "oci" {
tenancy_ocid = var.tenancy_ocid
user_ocid = var.user_ocid
fingerprint = var.fingerprint
private_key_path = var.private_key_path
region = var.region
}
terraform {
required_providers {
oci = {
source = "oracle/oci"
version = ">= 4.108.0"
}
}
}
resource "oci_core_instance" "atlas_instance" {
count = var.num_instances
availability_domain = data.oci_identity_availability_domain.ad.name
compartment_id = var.compartment_ocid
display_name = format("%s${count.index}", replace(title(var.instance_name), "/\\s/", ""))
shape = var.instance_shape
shape_config {
ocpus = var.instance_ocpus
memory_in_gbs = var.instance_shape_config_memory_in_gbs
}
create_vnic_details {
subnet_id = oci_core_subnet.atlas_subnet.id
display_name = format("%sVNIC", replace(title(var.instance_name), "/\\s/", ""))
assign_public_ip = true
assign_private_dns_record = true
hostname_label = format("%s${count.index}", lower(replace(var.instance_name, "/\\s/", "")))
}
source_details {
source_type = var.instance_source_type
source_id = var.instance_image_ocid[var.region]
boot_volume_size_in_gbs = var.boot_volume_size_in_gbs
}
metadata = {
ssh_authorized_keys = var.ssh_public_keys
}
timeouts {
create = "60m"
}
}
data "oci_core_instance_devices" "atlas_instance_devices" {
count = var.num_instances
instance_id = oci_core_instance.atlas_instance[count.index].id
}
output "instance_private_ips" {
value = [oci_core_instance.atlas_instance.*.private_ip]
}
output "instance_public_ips" {
value = [oci_core_instance.atlas_instance.*.public_ip]
}
output "boot_volume_ids" {
value = [oci_core_instance.atlas_instance.*.boot_volume_id]
}
output "instance_devices" {
value = [data.oci_core_instance_devices.atlas_instance_devices.*.devices]
}
resource "oci_core_vcn" "atlas_vcn" {
cidr_block = "10.1.0.0/16"
compartment_id = var.compartment_ocid
display_name = format("%sVCN", replace(title(var.instance_name), "/\\s/", ""))
dns_label = format("%svcn", lower(replace(var.instance_name, "/\\s/", "")))
}
resource "oci_core_security_list" "atlas_security_list" {
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.atlas_vcn.id
display_name = format("%sSecurityList", replace(title(var.instance_name), "/\\s/", ""))
# Allow outbound traffic on all ports for all protocols
egress_security_rules {
destination = "0.0.0.0/0"
protocol = "all"
stateless = false
}
# Allow inbound traffic on all ports for all protocols
ingress_security_rules {
protocol = "all"
source = "0.0.0.0/0"
stateless = false
}
# Allow inbound icmp traffic of a specific type
ingress_security_rules {
protocol = 1
source = "0.0.0.0/0"
stateless = false
icmp_options {
type = 3
code = 4
}
}
}
resource "oci_core_internet_gateway" "atlas_internet_gateway" {
compartment_id = var.compartment_ocid
display_name = format("%sIGW", replace(title(var.instance_name), "/\\s/", ""))
vcn_id = oci_core_vcn.atlas_vcn.id
}
resource "oci_core_default_route_table" "default_route_table" {
manage_default_resource_id = oci_core_vcn.atlas_vcn.default_route_table_id
display_name = "DefaultRouteTable"
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.atlas_internet_gateway.id
}
}
resource "oci_core_subnet" "atlas_subnet" {
availability_domain = data.oci_identity_availability_domain.ad.name
cidr_block = "10.1.20.0/24"
display_name = format("%sSubnet", replace(title(var.instance_name), "/\\s/", ""))
dns_label = format("%ssubnet", lower(replace(var.instance_name, "/\\s/", "")))
security_list_ids = [oci_core_security_list.atlas_security_list.id]
compartment_id = var.compartment_ocid
vcn_id = oci_core_vcn.atlas_vcn.id
route_table_id = oci_core_vcn.atlas_vcn.default_route_table_id
dhcp_options_id = oci_core_vcn.atlas_vcn.default_dhcp_options_id
}
data "oci_identity_availability_domain" "ad" {
compartment_id = var.tenancy_ocid
ad_number = var.availability_domain
}
resource "null_resource" "remote-exec" {
count = var.auto_iptables ? var.num_instances : 0
connection {
agent = false
timeout = "30m"
host = element(oci_core_instance.atlas_instance.*.public_ip, count.index)
user = "ubuntu"
private_key = file(var.ssh_private_key)
}
provisioner "remote-exec" {
inline = [
"export DATE=$(date +%Y%m%d); sudo iptables -L > \"/home/ubuntu/iptables-$DATE.bak\"",
"sudo sh -c 'iptables -D INPUT -j REJECT --reject-with icmp-host-prohibited 2> /dev/null; iptables-save > /etc/iptables/rules.v4;'",
"sudo sh -c 'iptables -D FORWARD -j REJECT --reject-with icmp-host-prohibited 2> /dev/null; iptables-save > /etc/iptables/rules.v4;'",
]
}
}