--operating-system "Canonical Ubuntu" --shape "VM.Standard.A1.Flex"
121 | {
122 | "data": [
123 | {
124 | "agent-features": null,
125 | "base-image-id": null,
126 | "billable-size-in-gbs": 2,
127 | "compartment-id": null,
128 | "create-image-allowed": true,
129 | "defined-tags": {},
130 | "display-name": "Canonical-Ubuntu-20.04-aarch64-2022.01.18-0",
131 | "freeform-tags": {},
132 | "id": "ocid1.image.oc1.eu-zurich-1.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q",
133 | "launch-mode": "NATIVE",
134 | "launch-options": {
135 | "boot-volume-type": "PARAVIRTUALIZED",
136 | "firmware": "UEFI_64",
137 | "is-consistent-volume-naming-enabled": true,
138 | "is-pv-encryption-in-transit-enabled": true,
139 | "network-type": "PARAVIRTUALIZED",
140 | "remote-data-volume-type": "PARAVIRTUALIZED"
141 | },
142 | "lifecycle-state": "AVAILABLE",
143 | "listing-type": null,
144 | "operating-system": "Canonical Ubuntu",
145 | "operating-system-version": "20.04",
146 | "size-in-mbs": 47694,
147 | "time-created": "2022-01-27T22:53:34.270000+00:00"
148 | },
149 | ```
150 |
151 | **Note:** this setup was only tested with Ubuntu 20.04
152 |
153 | ### Firewall
154 |
155 | By default firewall on the compute instances is disabled (except for the nat instance).
156 |
157 | ### Software installed
158 |
159 | In the simple-instance example and in the instance-pool example nginx will be installed by default.
160 | Nginx is used for testing the security list rules an the correct setup of the Load Balancer.
161 |
162 | On the k3s-cluster example, k3s will be automatically installed on all the machines. **NOTE** k3s-cluster setup has moved to [this](https://github.com/garutilorenzo/k3s-oci-cluster) repository.
163 |
164 | ### OS
165 |
166 | The operating system used is Ubuntu 20.04
167 |
168 | ### Shape
169 |
170 | All the provisioned instances are VM.Standard.A1.Flex (Arm processor) with 6GB of ram and 1 CPU.
171 |
172 | With the Oracle always free you can run 4 VM.Standard.A1.Flex instances for free (24 GB of ram an 4 CPU).
173 |
174 | **Note** choose a region with enough ARM capacity
175 |
176 | ### Useful documentation
177 |
178 | Setup the [default vcn resources](https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/terraformbestpractices_topic-vcndefaults.htm) documentation.
179 |
--------------------------------------------------------------------------------
/examples/.terraform.lock.hcl:
--------------------------------------------------------------------------------
1 | # This file is maintained automatically by "terraform init".
2 | # Manual edits may be lost in future updates.
3 |
4 | provider "registry.terraform.io/hashicorp/cloudinit" {
5 | version = "2.2.0"
6 | hashes = [
7 | "h1:tQLNREqesrdCQ/bIJnl0+yUK+XfdWzAG0wo4lp10LvM=",
8 | "zh:76825122171f9ea2287fd27e23e80a7eb482f6491a4f41a096d77b666896ee96",
9 | "zh:795a36dee548e30ca9c9d474af9ad6d29290e0a9816154ad38d55381cd0ab12d",
10 | "zh:9200f02cb917fb99e44b40a68936fd60d338e4d30a718b7e2e48024a795a61b9",
11 | "zh:a33cf255dc670c20678063aa84218e2c1b7a67d557f480d8ec0f68bc428ed472",
12 | "zh:ba3c1b2cd0879286c1f531862c027ec04783ece81de67c9a3b97076f1ce7f58f",
13 | "zh:bd575456394428a1a02191d2e46af0c00e41fd4f28cfe117d57b6aeb5154a0fb",
14 | "zh:c68dd1db83d8437c36c92dc3fc11d71ced9def3483dd28c45f8640cfcd59de9a",
15 | "zh:cbfe34a90852ed03cc074601527bb580a648127255c08589bc3ef4bf4f2e7e0c",
16 | "zh:d6ffd7398c6d1f359b96f5b757e77b99b339fbb91df1b96ac974fe71bc87695c",
17 | "zh:d9c15285f847d7a52df59e044184fb3ba1b7679fd0386291ed183782683d9517",
18 | "zh:f7dd02f6d36844da23c9a27bb084503812c29c1aec4aba97237fec16860fdc8c",
19 | ]
20 | }
21 |
22 | provider "registry.terraform.io/hashicorp/template" {
23 | version = "2.2.0"
24 | hashes = [
25 | "h1:94qn780bi1qjrbC3uQtjJh3Wkfwd5+tTtJHOb7KTg9w=",
26 | "zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386",
27 | "zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53",
28 | "zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603",
29 | "zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16",
30 | "zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776",
31 | "zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451",
32 | "zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae",
33 | "zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde",
34 | "zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d",
35 | "zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2",
36 | ]
37 | }
38 |
39 | provider "registry.terraform.io/oracle/oci" {
40 | version = "4.105.0"
41 | constraints = ">= 4.64.0"
42 | hashes = [
43 | "h1:/CC+3yPQLRp37dJSfcL/Tr5U1MqlnIfQ4G9VoB18gn4=",
44 | "zh:1af82982ed188d52d88af21d392d7f5284ffe855100edc1aec5821c47513e5af",
45 | "zh:1dfda986b069491070027ef235b6b985d8bcc8f250c8c9760ee05e710137f5a1",
46 | "zh:1e23c53207211846f52d0eb8cfbf32a9d67f07432a36228424242363ff2c689e",
47 | "zh:23f878c3e8cd2b0f2017b4b703f6956dae45596412443f257cdf21d41873d2f7",
48 | "zh:272a723136f309fc05616504bba35d8679e6ef895ed133b0ccd2345975c07ed9",
49 | "zh:47a7e36da90caed20bd960aa85b95f331506153dd6e3a65f12e6afc2a3bca2f2",
50 | "zh:765d793456a09a38af537fae363ec2ef0a464d412c562f538c654b28d57bb69f",
51 | "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
52 | "zh:9ee8c6e5aa8279c9cea5c47b81da6114b8f22848fc74b00dc4fb0fc89b71ec7f",
53 | "zh:a098d77dfc9b1123180f3beab3ce728303b67cae54e7d5ee40fc75156b3f4ee7",
54 | "zh:a607f78b8e6017968bf651b4b19039849bd06ef335633ee33e8dd6bc6341307a",
55 | "zh:b446b3d95112b535993ba0c3b309b4a235c7bd2003e49204d48bc3945bc729ba",
56 | "zh:b9e0527f6660674adacb036b6b7295f6061cdacb9c2db8333d9461228e37bcd0",
57 | "zh:bd933cd3f3463c6c566aca4e3349eab2949cc51331eaae82fa5b64be8a0b28cc",
58 | "zh:f0090a7556c80f5ad0e73bc53982e2844a381416d9bc35d4d27ff75ed24c87b8",
59 | ]
60 | }
61 |
--------------------------------------------------------------------------------
/examples/README.md:
--------------------------------------------------------------------------------
1 | # Examples
2 |
3 | In this folder there are two examples:
4 |
5 | * main.tf - Use a private subnet with a nat instance, all services are deployed on the pivate subnet. (Default example)
6 | * main.tf-public - Use a public subnet, all the services are deployed in the public subnet. (Disabled example)
7 |
8 | If you want to use the public example, rename the *main.tf-public* in *main.tf*. Keep **ONLY ONE** *.tf file.
9 |
10 | Now adjust all the *change-me* variables inside the main.tf file. Once you have setup your environment, we are ready to init terraform:
11 |
12 | ```
13 | Initializing modules...
14 |
15 | Initializing the backend...
16 |
17 | Initializing provider plugins...
18 | - Reusing previous version of hashicorp/oci from the dependency lock file
19 | - Reusing previous version of hashicorp/template from the dependency lock file
20 | - Using previously-installed hashicorp/oci v4.65.0
21 | - Using previously-installed hashicorp/template v2.2.0
22 |
23 | Terraform has been successfully initialized!
24 |
25 | You may now begin working with Terraform. Try running "terraform plan" to see
26 | any changes that are required for your infrastructure. All Terraform commands
27 | should now work.
28 |
29 | If you ever set or change modules or backend configuration for Terraform,
30 | rerun this command to reinitialize your working directory. If you forget, other
31 | commands will detect it and remind you to do so if necessary.
32 | ```
33 |
34 | ### Deploy
35 |
36 | We are now ready to deploy our infrastructure. First we ask terraform to plan the execution with:
37 |
38 | ```
39 | terraform plan
40 |
41 | ...
42 | ...
43 | ...
44 |
45 | + source_details {
46 | + boot_volume_size_in_gbs = (known after apply)
47 | + kms_key_id = (known after apply)
48 | + source_id = "ocid1.image.oc1.REGION.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q"
49 | + source_type = "image"
50 | }
51 | }
52 |
53 | Plan: 24 to add, 0 to change, 0 to destroy.
54 |
55 | Changes to Outputs:
56 | + instance_ip = (known after apply)
57 | + instance_pool_id = (known after apply)
58 | + instance_pool_ips = [
59 | + (known after apply),
60 | + (known after apply),
61 | ]
62 | + instance_pool_size = 2
63 | + internal_lb_ip = (known after apply)
64 | + lb_ip = (known after apply)
65 | + nat_instance_id = (known after apply)
66 | + nat_instance_public_ip = (known after apply)
67 | + private_subnet_id = (known after apply)
68 | + public_subnet_cidr = "10.0.0.0/24"
69 | + public_subnet_id = (known after apply)
70 | + security_list_id = (known after apply)
71 | + vcn_id = (known after apply)
72 |
73 | ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
74 |
75 | Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
76 | ```
77 |
78 | now we can deploy our resources with:
79 |
80 | ```
81 | terraform apply
82 |
83 | ...
84 | ...
85 | ...
86 |
87 | Plan: 24 to add, 0 to change, 0 to destroy.
88 |
89 | Changes to Outputs:
90 | + instance_ip = (known after apply)
91 | + instance_pool_id = (known after apply)
92 | + instance_pool_ips = [
93 | + (known after apply),
94 | + (known after apply),
95 | ]
96 | + instance_pool_size = 2
97 | + internal_lb_ip = (known after apply)
98 | + lb_ip = (known after apply)
99 | + nat_instance_id = (known after apply)
100 | + nat_instance_public_ip = (known after apply)
101 | + private_subnet_id = (known after apply)
102 | + public_subnet_cidr = "10.0.0.0/24"
103 | + public_subnet_id = (known after apply)
104 | + security_list_id = (known after apply)
105 | + vcn_id = (known after apply)
106 |
107 | Do you want to perform these actions?
108 | Terraform will perform the actions described above.
109 | Only 'yes' will be accepted to approve.
110 |
111 | Enter a value: yes
112 |
113 | ...
114 | ...
115 | ...
116 |
117 | Apply complete! Resources: 24 added, 0 changed, 0 destroyed.
118 |
119 | Outputs:
120 |
121 | instance_ip = "10..X.X.X"
122 | instance_pool_id = "ocid1.instancepool.oc1.REGION.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
123 | instance_pool_ips = [
124 | "10..X.X.X",
125 | "10..X.X.X",
126 | ]
127 | instance_pool_size = 2
128 | internal_lb_ip = tolist([
129 | {
130 | "ip_address" = "10..X.X.X"
131 | "ip_version" = "IPV4"
132 | "is_public" = false
133 | "reserved_ip" = tolist([])
134 | },
135 | ])
136 | lb_ip = tolist([
137 | "144.X.X.X",
138 | ])
139 | nat_instance_id = "ocid1.instance.oc1.REGION.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
140 | nat_instance_public_ip = "152.X.X.X"
141 | private_subnet_id = "ocid1.subnet.oc1.REGION.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
142 | public_subnet_cidr = "10.0.0.0/24"
143 | public_subnet_id = "ocid1.subnet.oc1.REGION.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
144 | security_list_id = "ocid1.securitylist.oc1.REGION.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
145 | vcn_id = "ocid1.vcn.oc1.REGION.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
146 |
147 | ```
148 |
149 | ### Resources test
150 |
151 | #### Public LB Test
152 |
153 | ```
154 | curl http://144.X.X.X
155 |
156 |
157 |
158 |
159 | Welcome to nginx!
160 |
167 |
168 |
169 | Welcome to nginx!
170 | If you see this page, the nginx web server is successfully installed and
171 | working. Further configuration is required.
172 |
173 | For online documentation and support please refer to
174 | nginx.org.
175 | Commercial support is available at
176 | nginx.com.
177 |
178 | Thank you for using nginx.
179 | Hello from: inst-ikv6i-ubuntu-instance-pool
180 |
181 |
182 | ```
183 |
184 | #### Private LB Test
185 |
186 | First we need to [connect](#connect-to-private-instances) via ssh on a private instance. **NOTE** we need to connect to the instance that is not part of the instance group.
187 |
188 | Now we can call the public LB ip:
189 |
190 | ```
191 | curl http://10.X.X.X
192 |
193 |
194 |
195 |
196 | Welcome to nginx!
197 |
204 |
205 |
206 | Welcome to nginx!
207 | If you see this page, the nginx web server is successfully installed and
208 | working. Further configuration is required.
209 |
210 | For online documentation and support please refer to
211 | nginx.org.
212 | Commercial support is available at
213 | nginx.com.
214 |
215 | Thank you for using nginx.
216 | Hello from: inst-ikv6i-ubuntu-instance-pool
217 |
218 |
219 | ```
220 |
221 | ### Connect to private instances
222 |
223 | We can connect to the private instances using the nat instance as Jump server:
224 |
225 | ```
226 | ssh -J bastion@ ubuntu@
227 |
228 | Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.11.0-1027-oracle aarch64)
229 |
230 | * Documentation: https://help.ubuntu.com
231 | * Management: https://landscape.canonical.com
232 | * Support: https://ubuntu.com/advantage
233 |
234 | System information as of Thu Mar 3 14:34:39 UTC 2022
235 |
236 | System load: 0.0 Processes: 151
237 | Usage of /: 3.9% of 44.97GB Users logged in: 0
238 | Memory usage: 4% IPv4 address for enp0s3: 10.0.1.239
239 | Swap usage: 0%
240 |
241 |
242 | 7 updates can be applied immediately.
243 | 7 of these updates are standard security updates.
244 | To see these additional updates run: apt list --upgradable
245 |
246 |
247 | *** System restart required ***
248 | Last login: Thu Mar 3 14:34:20 2022 from 10.0.0.37
249 | To run a command as administrator (user "root"), use "sudo ".
250 | See "man sudo_root" for details.
251 |
252 | ubuntu@inst-a73cs-ubuntu-instance-pool:~$
253 | ```
254 |
255 | ### Start a project from scratch
256 |
257 | If you want to create a new project from scratch you need three files:
258 |
259 | * terraform.tfvars - More details in [Oracle provider setup](../README.md#oracle-provider-setup)
260 | * main.tf - download main.tf file or main.tf-public based on your needs. If you choose main.tf-public **remember** to rename the file in main.tf
261 | * provider.tf - download the file from this directory
262 |
263 | ### Cleanup
264 |
265 | ```
266 | terraform destroy
267 | ```
--------------------------------------------------------------------------------
/examples/main.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 | type = string
3 | }
4 |
5 | variable "tenancy_ocid" {
6 | type = string
7 | }
8 |
9 | variable "user_ocid" {
10 | type = string
11 | }
12 |
13 | variable "fingerprint" {
14 | type = string
15 | }
16 |
17 | variable "private_key_path" {
18 | type = string
19 | }
20 |
21 | variable "region" {
22 | default = ""
23 | }
24 |
25 | variable "environment" {
26 | default = "staging"
27 | }
28 |
29 | variable "availability_domain" {
30 | default = ""
31 | }
32 |
33 | module "private-vcn" {
34 | region = var.region
35 | compartment_ocid = var.compartment_ocid
36 | my_public_ip_cidr = ""
37 | environment = var.environment
38 | source = "../private-vcn"
39 | }
40 |
41 | output "vcn_id" {
42 | value = module.private-vcn.vcn_id
43 | }
44 |
45 | output "public_subnet_id" {
46 | value = module.private-vcn.public_subnet_id
47 | }
48 |
49 | output "private_subnet_id" {
50 | value = module.private-vcn.private_subnet_id
51 | }
52 |
53 | output "security_list_id" {
54 | value = module.private-vcn.security_list_id
55 | }
56 |
57 | output "public_subnet_cidr" {
58 | value = module.private-vcn.public_subnet_cidr
59 | }
60 |
61 | module "nat-instance" {
62 | region = var.region
63 | compartment_ocid = var.compartment_ocid
64 | availability_domain = var.availability_domain
65 | vcn_id = module.private-vcn.vcn_id
66 | private_subnet_id = module.private-vcn.private_subnet_id
67 | public_subnet_id = module.private-vcn.public_subnet_id
68 | environment = var.environment
69 | source = "../nat-instance"
70 | }
71 |
72 | output "nat_instance_id" {
73 | value = module.nat-instance.nat_instance_id
74 | }
75 |
76 | output "nat_instance_public_ip" {
77 | value = module.nat-instance.nat_instance_public_ip
78 | }
79 |
80 | module "simple-instance" {
81 | region = var.region
82 | compartment_ocid = var.compartment_ocid
83 | availability_domain = var.availability_domain
84 | is_private = true
85 | private_subnet_id = module.private-vcn.private_subnet_id
86 | public_subnet_id = module.private-vcn.public_subnet_id
87 | environment = var.environment
88 | source = "../simple-instance"
89 | }
90 |
91 | output "instance_ip" {
92 | value = module.simple-instance.instance_ip
93 | }
94 |
95 | module "instance-pool" {
96 | region = var.region
97 | compartment_ocid = var.compartment_ocid
98 | availability_domain = var.availability_domain
99 | is_private = true
100 | private_subnet_id = module.private-vcn.private_subnet_id
101 | public_subnet_id = module.private-vcn.public_subnet_id
102 | public_subnet_cidr = module.private-vcn.public_subnet_cidr
103 | environment = var.environment
104 | source = "../instance-pool"
105 | }
106 |
107 | output "instance_pool_ips" {
108 | value = module.instance-pool.instances_ips
109 | }
110 |
111 | output "instance_pool_id" {
112 | value = module.instance-pool.instance_pool_id
113 | }
114 |
115 | output "instance_pool_size" {
116 | value = module.instance-pool.instance_pool_size
117 | }
118 |
119 |
120 | module "load-balancer" {
121 | region = var.region
122 | compartment_ocid = var.compartment_ocid
123 | is_private = false
124 | instance_pool_id = module.instance-pool.instance_pool_id
125 | instance_pool_size = module.instance-pool.instance_pool_size
126 | vcn_id = module.private-vcn.vcn_id
127 | private_subnet_id = module.private-vcn.private_subnet_id
128 | public_subnet_id = module.private-vcn.public_subnet_id
129 | environment = var.environment
130 | source = "../load-balancer"
131 | }
132 |
133 | output "lb_ip" {
134 | value = module.load-balancer.lb_ip
135 | }
136 |
137 | module "network-load-balancer" {
138 | region = var.region
139 | compartment_ocid = var.compartment_ocid
140 | is_private = true
141 | instance_pool_id = module.instance-pool.instance_pool_id
142 | instance_pool_size = module.instance-pool.instance_pool_size
143 | vcn_id = module.private-vcn.vcn_id
144 | private_subnet_id = module.private-vcn.private_subnet_id
145 | public_subnet_id = module.private-vcn.public_subnet_id
146 | environment = var.environment
147 | source = "../network-load-balancer"
148 | }
149 |
150 | output "internal_lb_ip" {
151 | value = module.network-load-balancer.lb_ip
152 | }
--------------------------------------------------------------------------------
/examples/main.tf-public:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 | type = string
3 | }
4 |
5 | variable "tenancy_ocid" {
6 | type = string
7 | }
8 |
9 | variable "user_ocid" {
10 | type = string
11 | }
12 |
13 | variable "fingerprint" {
14 | type = string
15 | }
16 |
17 | variable "private_key_path" {
18 | type = string
19 | }
20 |
21 | variable "region" {
22 | default = ""
23 | }
24 |
25 | variable "environment" {
26 | default = "staging"
27 | }
28 |
29 | variable "availability_domain" {
30 | default = ""
31 | }
32 |
33 | module "simple-vcn" {
34 | region = var.region
35 | compartment_ocid = var.compartment_ocid
36 | my_public_ip_cidr = ""
37 | environment = var.environment
38 | source = "../simple-vcn"
39 | }
40 |
41 | output "vcn_id" {
42 | value = module.simple-vcn.vcn_id
43 | }
44 |
45 | output "public_subnet_id" {
46 | value = module.simple-vcn.public_subnet_id
47 | }
48 |
49 | output "secondary_public_subnet_id" {
50 | value = module.simple-vcn.secondary_public_subnet_id
51 | }
52 |
53 | output "security_list_id" {
54 | value = module.simple-vcn.security_list_id
55 | }
56 |
57 | output "public_subnet_cidr" {
58 | value = module.simple-vcn.public_subnet_cidr
59 | }
60 |
61 | module "simple-instance" {
62 | region = var.region
63 | compartment_ocid = var.compartment_ocid
64 | availability_domain = var.availability_domain
65 | private_subnet_id = module.simple-vcn.secondary_public_subnet_id
66 | public_subnet_id = module.simple-vcn.public_subnet_id
67 | environment = var.environment
68 | source = "../simple-instance"
69 | }
70 |
71 | output "instance_ip" {
72 | value = module.simple-instance.instance_ip
73 | }
74 |
75 | module "instance-pool" {
76 | region = var.region
77 | compartment_ocid = var.compartment_ocid
78 | availability_domain = var.availability_domain
79 | private_subnet_id = module.simple-vcn.secondary_public_subnet_id
80 | public_subnet_id = module.simple-vcn.public_subnet_id
81 | public_subnet_cidr = module.simple-vcn.public_subnet_cidr
82 | environment = var.environment
83 | source = "../instance-pool"
84 | }
85 |
86 | output "instance_pool_ips" {
87 | value = module.instance-pool.instances_ips
88 | }
89 |
90 | output "instance_pool_id" {
91 | value = module.instance-pool.instance_pool_id
92 | }
93 |
94 | output "instance_pool_size" {
95 | value = module.instance-pool.instance_pool_size
96 | }
97 |
98 | module "load-balancer" {
99 | region = var.region
100 | compartment_ocid = var.compartment_ocid
101 | is_private = false
102 | instance_pool_id = module.instance-pool.instance_pool_id
103 | instance_pool_size = module.instance-pool.instance_pool_size
104 | vcn_id = module.simple-vcn.vcn_id
105 | private_subnet_id = module.simple-vcn.secondary_public_subnet_id
106 | public_subnet_id = module.simple-vcn.public_subnet_id
107 | environment = var.environment
108 | source = "../load-balancer"
109 | }
110 |
111 | output "lb_ip" {
112 | value = module.load-balancer.lb_ip
113 | }
114 |
115 | module "network-load-balancer" {
116 | region = var.region
117 | compartment_ocid = var.compartment_ocid
118 | is_private = true
119 | instance_pool_id = module.instance-pool.instance_pool_id
120 | instance_pool_size = module.instance-pool.instance_pool_size
121 | vcn_id = module.simple-vcn.vcn_id
122 | private_subnet_id = module.simple-vcn.secondary_public_subnet_id
123 | public_subnet_id = module.simple-vcn.public_subnet_id
124 | environment = var.environment
125 | source = "../network-load-balancer"
126 | }
127 |
128 | output "internal_lb_ip" {
129 | value = module.network-load-balancer.lb_ip
130 | }
--------------------------------------------------------------------------------
/examples/provider.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
9 |
10 | provider "oci" {
11 | tenancy_ocid = var.tenancy_ocid
12 | user_ocid = var.user_ocid
13 | private_key_path = var.private_key_path
14 | fingerprint = var.fingerprint
15 | region = var.region
16 | }
--------------------------------------------------------------------------------
/instance-pool/README.md:
--------------------------------------------------------------------------------
1 | # Instance pool
2 |
3 | This module will deploy an Instance pool made by two compute instances. Also this module will create one instance configuration used by the instance pool.
4 |
5 | If you choose to publish this compute instances in a private subnet, you need a NAT instance (refer to nat-instance module). The nat instance can be used also as bation host to reach the private instance, or if you prefer you can deploy a dedicated bastion host (refer to bastion-host module).
6 |
7 | ### Requirements
8 |
9 | * One vcn with a public or private subnet (simple-vcn or private-vcn module)
10 | * One nat instance if the instance pool *is_private* (nat-instance module)
11 |
12 | ### Module variables
13 |
14 | | Var | Required | Desc |
15 | | ------- | ------- | ----------- |
16 | | `region` | `yes` | set the correct OCI region based on your needs |
17 | | `availability_domain` | `yes` | Set the correct availability domain. See [how](../README.md#how-to-find-the-availability-doamin-name) to find the availability domain|
18 | | `compartment_ocid` | `yes` | Set the correct compartment ocid. See [how](../README.md#oracle-provider-setup) to find the compartment ocid |
19 | | `environment` | `yes` | Current work environment (Example: staging/dev/prod). This value is used for tag all the deployed resources |
20 | | `private_subnet_id` | `yes` | Private subnet OCID |
21 | | `public_subnet_id` | `yes` | Public subnet OCID |
22 | | `public_subnet_cidr` | `yes` | Public subnet CIDR |
23 | | `instance_pool_size` | `no` | Number of instances in the instance pool. Default: 2 |
24 | | `fault_domains` | `no` | Fault list. Default: FAULT-DOMAIN-1, FAULT-DOMAIN-2, FAULT-DOMAIN-3 |
25 | | `PATH_TO_PUBLIC_KEY` | `no` | Path to your public ssh key (Default: "~/.ssh/id_rsa.pub) |
26 | | `is_private` | `no` | Bool value. If true the instance pool will be deployed in a private subnet. Default: false |
27 | | `os_image_id` | `no` | OS image OCID. Default: ocid1.image.oc1.eu-zurich-1.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q - Canonical-Ubuntu-20.04-aarch64-2022.01.18-0 |
28 |
29 | ### Output
30 |
31 | The module will output:
32 |
33 | * instances_ips, IPs of the instances
34 | * instance_pool_id, Instance pool OCID
--------------------------------------------------------------------------------
/instance-pool/data.tf:
--------------------------------------------------------------------------------
1 | data "cloudinit_config" "ubuntu_init" {
2 | gzip = true
3 | base64_encode = true
4 |
5 | part {
6 | content_type = "text/x-shellscript"
7 | content = templatefile("${path.module}/files/oci-ubuntu-install.sh", { public_subnet_cidr = var.public_subnet_cidr })
8 | }
9 | }
10 |
11 | data "oci_core_instance_pool_instances" "ubuntu_instance_pool_instances" {
12 | depends_on = [
13 | oci_core_instance_pool.ubuntu_instance_pool,
14 | ]
15 | compartment_id = var.compartment_ocid
16 | instance_pool_id = oci_core_instance_pool.ubuntu_instance_pool.id
17 | }
18 |
19 | data "oci_core_instance" "ubuntu_instance_pool_instances_ips" {
20 | count = var.instance_pool_size
21 | instance_id = data.oci_core_instance_pool_instances.ubuntu_instance_pool_instances.instances[count.index].id
22 | }
--------------------------------------------------------------------------------
/instance-pool/files/oci-ubuntu-install.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | apt-get update
4 | apt-get -y upgrade
5 | apt-get -y install nginx
6 |
7 | systemctl enable nginx
8 | systemctl start nginx
9 |
10 | hostname=$(hostname)
11 |
12 | cat < /etc/nginx/nginx.conf
13 | user www-data;
14 | worker_processes auto;
15 | pid /run/nginx.pid;
16 | include /etc/nginx/modules-enabled/*.conf;
17 |
18 | events {
19 | worker_connections 768;
20 | # multi_accept on;
21 | }
22 |
23 | http {
24 |
25 | ##
26 | # Basic Settings
27 | ##
28 |
29 | sendfile on;
30 | tcp_nopush on;
31 | tcp_nodelay on;
32 | keepalive_timeout 65;
33 | types_hash_max_size 2048;
34 | # server_tokens off;
35 |
36 | # server_names_hash_bucket_size 64;
37 | # server_name_in_redirect off;
38 |
39 | include /etc/nginx/mime.types;
40 | default_type application/octet-stream;
41 |
42 | ##
43 | # SSL Settings
44 | ##
45 |
46 | ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
47 | ssl_prefer_server_ciphers on;
48 |
49 | ##
50 | # Set real ip
51 | ##
52 |
53 | set_real_ip_from 127.0.0.1;
54 | set_real_ip_from ${public_subnet_cidr};
55 | real_ip_header X-Forwarded-For;
56 | real_ip_recursive on;
57 |
58 | ##
59 | # Logging Settings
60 | ##
61 |
62 | access_log /var/log/nginx/access.log;
63 | error_log /var/log/nginx/error.log;
64 |
65 | ##
66 | # Gzip Settings
67 | ##
68 |
69 | gzip on;
70 | gzip_disable "msie6";
71 |
72 | gzip_vary on;
73 | gzip_proxied any;
74 | gzip_comp_level 6;
75 | gzip_buffers 16 8k;
76 | gzip_http_version 1.1;
77 | gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
78 |
79 | ##
80 | # Virtual Host Configs
81 | ##
82 |
83 | include /etc/nginx/conf.d/*.conf;
84 | include /etc/nginx/sites-enabled/*;
85 | }
86 | EOF
87 |
88 | systemctl restart nginx.service
89 |
90 | cat < /var/www/html/index.nginx-debian.html
91 |
92 |
93 |
94 | Welcome to nginx!
95 |
102 |
103 |
104 | Welcome to nginx!
105 | If you see this page, the nginx web server is successfully installed and
106 | working. Further configuration is required.
107 |
108 | For online documentation and support please refer to
109 | nginx.org.
110 | Commercial support is available at
111 | nginx.com.
112 |
113 | Thank you for using nginx.
114 | Hello from: $hostname
115 |
116 |
117 | EOF
118 |
119 | # Disable firewall
120 | /usr/sbin/netfilter-persistent stop
121 | /usr/sbin/netfilter-persistent flush
122 |
123 | systemctl stop netfilter-persistent.service
124 | systemctl disable netfilter-persistent.service
--------------------------------------------------------------------------------
/instance-pool/instancepool.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_instance_pool" "ubuntu_instance_pool" {
2 |
3 | lifecycle {
4 | create_before_destroy = true
5 | ignore_changes = [load_balancers, freeform_tags]
6 | }
7 |
8 | display_name = var.instance_pool_name
9 | compartment_id = var.compartment_ocid
10 | instance_configuration_id = oci_core_instance_configuration.ubuntu_template.id
11 |
12 | placement_configurations {
13 | availability_domain = var.availability_domain
14 | primary_subnet_id = var.is_private == true ? var.private_subnet_id : var.public_subnet_id
15 | fault_domains = var.fault_domains
16 | }
17 |
18 | size = var.instance_pool_size
19 |
20 | freeform_tags = local.tags
21 | }
--------------------------------------------------------------------------------
/instance-pool/local.tf:
--------------------------------------------------------------------------------
1 | locals {
2 | tags = {
3 | "oracle-tutorial" = "terraform"
4 | "environment" = "${var.environment}"
5 | }
6 | }
--------------------------------------------------------------------------------
/instance-pool/output.tf:
--------------------------------------------------------------------------------
1 | output "instances_ips" {
2 | depends_on = [
3 | data.oci_core_instance_pool_instances.ubuntu_instance_pool_instances,
4 | ]
5 | value = var.is_private == true ? data.oci_core_instance.ubuntu_instance_pool_instances_ips.*.private_ip : data.oci_core_instance.ubuntu_instance_pool_instances_ips.*.public_ip
6 | }
7 |
8 | output "instance_pool_id" {
9 | value = oci_core_instance_pool.ubuntu_instance_pool.id
10 | }
11 |
12 | output "instance_pool_size" {
13 | value = oci_core_instance_pool.ubuntu_instance_pool.size
14 | }
--------------------------------------------------------------------------------
/instance-pool/template.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_instance_configuration" "ubuntu_template" {
2 |
3 | compartment_id = var.compartment_ocid
4 | display_name = "Ubuntu 20.04 instance config"
5 |
6 | instance_details {
7 |
8 | instance_type = "compute"
9 |
10 | launch_details {
11 |
12 | agent_config {
13 | is_management_disabled = "false"
14 | is_monitoring_disabled = "false"
15 |
16 | plugins_config {
17 | desired_state = "DISABLED"
18 | name = "Vulnerability Scanning"
19 | }
20 |
21 | plugins_config {
22 | desired_state = "ENABLED"
23 | name = "Compute Instance Monitoring"
24 | }
25 |
26 | plugins_config {
27 | desired_state = "DISABLED"
28 | name = "Bastion"
29 | }
30 | }
31 |
32 | availability_domain = var.availability_domain
33 | compartment_id = var.compartment_ocid
34 |
35 | create_vnic_details {
36 | assign_public_ip = var.is_private == true ? false : true
37 | subnet_id = var.is_private == true ? var.private_subnet_id : var.public_subnet_id
38 | }
39 |
40 | display_name = "Ubuntu Template"
41 |
42 | metadata = {
43 | "ssh_authorized_keys" = file(var.PATH_TO_PUBLIC_KEY)
44 | "user_data" = data.cloudinit_config.ubuntu_init.rendered
45 | }
46 |
47 | shape = "VM.Standard.A1.Flex"
48 | shape_config {
49 | memory_in_gbs = "6"
50 | ocpus = "1"
51 | }
52 | source_details {
53 | image_id = var.os_image_id
54 | source_type = "image"
55 | }
56 | }
57 | }
58 |
59 | freeform_tags = local.tags
60 | }
--------------------------------------------------------------------------------
/instance-pool/terraform.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/instance-pool/vars.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 |
3 | }
4 |
5 | variable "region" {
6 |
7 | }
8 |
9 | variable "availability_domain" {
10 |
11 | }
12 |
13 | variable "fault_domains" {
14 | type = list(any)
15 | default = ["FAULT-DOMAIN-1", "FAULT-DOMAIN-2", "FAULT-DOMAIN-3"]
16 | }
17 |
18 | variable "PATH_TO_PUBLIC_KEY" {
19 | type = string
20 | default = "~/.ssh/id_rsa.pub"
21 | description = "Path to your public key"
22 | }
23 |
24 | variable "environment" {
25 | type = string
26 | }
27 |
28 | variable "is_private" {
29 | type = bool
30 | default = false
31 | }
32 |
33 | variable "public_subnet_id" {
34 | type = string
35 | }
36 |
37 | variable "private_subnet_id" {
38 | type = string
39 | }
40 |
41 | variable "public_subnet_cidr" {
42 | type = string
43 | }
44 |
45 | variable "os_image_id" {
46 | default = "ocid1.image.oc1.eu-zurich-1.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q" # Canonical-Ubuntu-20.04-aarch64-2022.01.18-0
47 | }
48 |
49 | variable "instance_pool_size" {
50 | type = number
51 | default = 2
52 | }
53 |
54 | variable "instance_pool_name" {
55 | type = string
56 | default = "ubuntu-instance-pool"
57 | }
--------------------------------------------------------------------------------
/k3s-cluster/README.md:
--------------------------------------------------------------------------------
1 | # k3s cluster
2 |
3 | Please refer to [this](https://github.com/garutilorenzo/k3s-oci-cluster) repository
--------------------------------------------------------------------------------
/load-balancer/README.md:
--------------------------------------------------------------------------------
1 | # Load balancer (Layer 7)
2 |
3 | This module will deploy a Load Balancer (L7). The LB can be public or private (*is_private* variable). The LB will be attached to the instances in a instance pool (*instance_pool_id* variable). Also this module will deploy one Network security group that will be attached to the Load Balancer, this NSG will allow traffic on port 80 only to the LB.
4 |
5 | ### Requirements
6 |
7 | * One vcn with a public or private subnet (simple-vcn or private-vcn module)
8 |
9 | ### Module variables
10 |
11 | | Var | Required | Desc |
12 | | ------- | ------- | ----------- |
13 | | `region` | `yes` | set the correct OCI region based on your needs |
14 | | `compartment_ocid` | `yes` | Set the correct compartment ocid. See [how](../README.md#oracle-provider-setup) to find the compartment ocid |
15 | | `vcn_id` | `yes` | The VCN OCID |
16 | | `private_subnet_id` | `yes` | Private subnet OCID |
17 | | `public_subnet_id` | `yes` | Public subnet OCID |
18 | | `instance_pool_size` | `yes` | Instance pool size |
19 | | `instance_pool_id` | `yes` | Instance pool ocid OCID |
20 | | `is_private` | `no` | Bool value. If true the LB will be a private LB (no public ip) Default: false |
21 | | `lb_shape` | `no` | Load balancer shape. Default: flexible |
22 |
23 | ### Output
24 |
25 | lb_ip, LB public or private ip address
--------------------------------------------------------------------------------
/load-balancer/data.tf:
--------------------------------------------------------------------------------
1 | data "oci_core_instance_pool_instances" "ubuntu_instance_pool_instances" {
2 | compartment_id = var.compartment_ocid
3 | instance_pool_id = var.instance_pool_id
4 | }
5 |
6 | data "oci_core_instance" "ubuntu_instance_pool_instances_ips" {
7 | count = var.instance_pool_size
8 | instance_id = data.oci_core_instance_pool_instances.ubuntu_instance_pool_instances.instances[count.index].id
9 | }
--------------------------------------------------------------------------------
/load-balancer/lb.tf:
--------------------------------------------------------------------------------
1 | resource "oci_load_balancer_load_balancer" "load_balancer_l7" {
2 | compartment_id = var.compartment_ocid
3 | display_name = "LB Layer 7"
4 | shape = var.lb_shape
5 | subnet_ids = [var.public_subnet_id]
6 | network_security_group_ids = [oci_core_network_security_group.public_lb_nsg.id]
7 |
8 | ip_mode = "IPV4"
9 | is_private = var.is_private
10 |
11 | shape_details {
12 | maximum_bandwidth_in_mbps = 10
13 | minimum_bandwidth_in_mbps = 10
14 | }
15 |
16 | freeform_tags = local.tags
17 | }
18 |
19 | # HTTP
20 | resource "oci_load_balancer_listener" "http_listener" {
21 | default_backend_set_name = oci_load_balancer_backend_set.http_backend_set.name
22 | load_balancer_id = oci_load_balancer_load_balancer.load_balancer_l7.id
23 | name = "http_listener"
24 | port = 80
25 | protocol = "HTTP"
26 | }
27 |
28 | resource "oci_load_balancer_backend_set" "http_backend_set" {
29 | health_checker {
30 | protocol = "HTTP"
31 | port = 80
32 | url_path = "/"
33 | return_code = 200
34 | }
35 |
36 | load_balancer_id = oci_load_balancer_load_balancer.load_balancer_l7.id
37 | name = "http_backend_set"
38 | policy = "ROUND_ROBIN"
39 | }
40 |
41 | resource "oci_load_balancer_backend" "http_backend" {
42 | count = var.instance_pool_size
43 | backendset_name = oci_load_balancer_backend_set.http_backend_set.name
44 | ip_address = data.oci_core_instance.ubuntu_instance_pool_instances_ips[count.index].private_ip
45 | load_balancer_id = oci_load_balancer_load_balancer.load_balancer_l7.id
46 | port = 80
47 | }
--------------------------------------------------------------------------------
/load-balancer/local.tf:
--------------------------------------------------------------------------------
1 | locals {
2 | tags = {
3 | "oracle-tutorial" = "terraform"
4 | "environment" = "${var.environment}"
5 | }
6 | }
--------------------------------------------------------------------------------
/load-balancer/nsg.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_network_security_group" "public_lb_nsg" {
2 | compartment_id = var.compartment_ocid
3 | vcn_id = var.vcn_id
4 | display_name = "Public LB nsg"
5 |
6 | freeform_tags = local.tags
7 | }
8 |
9 | resource "oci_core_network_security_group_security_rule" "allow_http_from_all" {
10 | network_security_group_id = oci_core_network_security_group.public_lb_nsg.id
11 | direction = "INGRESS"
12 | protocol = 6 # tcp
13 |
14 | description = "Allow HTTP from all"
15 |
16 | source = "0.0.0.0/0"
17 | source_type = "CIDR_BLOCK"
18 | stateless = false
19 |
20 | tcp_options {
21 | destination_port_range {
22 | max = 80
23 | min = 80
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/load-balancer/output.tf:
--------------------------------------------------------------------------------
1 | output "lb_ip" {
2 | value = oci_load_balancer_load_balancer.load_balancer_l7.ip_addresses
3 | }
--------------------------------------------------------------------------------
/load-balancer/terraform.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/load-balancer/vars.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 |
3 | }
4 |
5 | variable "region" {
6 |
7 | }
8 |
9 | variable "environment" {
10 | type = string
11 | }
12 |
13 | variable "instance_pool_id" {
14 | type = string
15 | }
16 |
17 | variable "is_private" {
18 | type = bool
19 | default = false
20 | }
21 |
22 | variable "lb_shape" {
23 | type = string
24 | default = "flexible"
25 | }
26 |
27 | variable "vcn_id" {
28 | type = string
29 | }
30 |
31 | variable "public_subnet_id" {
32 | type = string
33 | }
34 |
35 | variable "private_subnet_id" {
36 | type = string
37 | }
38 |
39 | variable "instance_pool_size" {
40 | type = number
41 | default = 2
42 | }
--------------------------------------------------------------------------------
/nat-instance/README.md:
--------------------------------------------------------------------------------
1 | # NAT instance
2 |
3 | This module will deploy a NAT instance. If you use a private subnet with an always free account you will need a nat instance to give internet access to the private subnet. With the always free account you can't use/deploy a NAT gateway.
4 |
5 | Also this module will setup a new route table and will attach this new route to the private subnet.
6 |
7 | ### Requirements
8 |
9 | * One vcn with a public or private subnet (simple-vcn or private-vcn module)
10 |
11 | ### Module variables
12 |
13 | | Var | Required | Desc |
14 | | ------- | ------- | ----------- |
15 | | `region` | `yes` | set the correct OCI region based on your needs |
16 | | `availability_domain` | `yes` | Set the correct availability domain. See [how](../README.md#how-to-find-the-availability-doamin-name) to find the availability domain|
17 | | `compartment_ocid` | `yes` | Set the correct compartment ocid. See [how](../README.md#oracle-provider-setup) to find the compartment ocid |
18 | | `vcn_id` | `yes` | The VCN OCID |
19 | | `private_subnet_id` | `yes` | Private subnet OCID |
20 | | `public_subnet_id` | `yes` | Public subnet OCID |
21 | | `default_fault_domain` | `no` | Fault domain where the instance will be deployed. Default: FAULT-DOMAIN-1 |
22 | | `PATH_TO_PUBLIC_KEY` | `no` | Path to your public ssh key (Default: "~/.ssh/id_rsa.pub) |
23 | | `os_image_id` | `no` | OS image OCID. Default: ocid1.image.oc1.eu-zurich-1.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q - Canonical-Ubuntu-20.04-aarch64-2022.01.18-0 |
24 | | `setup_bastion` | `no` | Bool variable. Setup the nat instance as bastion host. Default: true |
25 | | `bastion_user` | `no` | Bastion username. Default: bastion |
26 | | `bastion_group` | `no` | Bastion group. Default: bastion |
27 | | `ssh_keys_path` | `no` | List of ssh keys allowed to connect to the nat instance as bastion user. Default: ["~/.ssh/id_rsa.pub"] |
28 |
29 |
30 | ### Output
31 |
32 | The module will output:
33 |
34 | * nat_instance_id, NAT instance OCID
35 | * nat_instance_public_ip, NAT instance public ip
--------------------------------------------------------------------------------
/nat-instance/data.tf:
--------------------------------------------------------------------------------
1 | data "cloudinit_config" "nat_instance_init" {
2 | gzip = true
3 | base64_encode = true
4 |
5 | part {
6 | content_type = "text/x-shellscript"
7 | content = templatefile("${path.module}/files/setup_nat.sh", {})
8 | }
9 |
10 | part {
11 | content_type = "text/x-shellscript"
12 | content = templatefile("${path.module}/files/setup_bastion.sh", {
13 | ssh_keys = local.ssh_keys,
14 | setup_bastion = var.setup_bastion,
15 | bastion_user = var.bastion_user,
16 | bastion_group = var.bastion_group
17 | })
18 | }
19 | }
20 |
21 | data "oci_core_vnic_attachments" "nat_instance_vnics" {
22 | depends_on = [
23 | oci_core_instance.nat_instance
24 | ]
25 |
26 | compartment_id = var.compartment_ocid
27 | availability_domain = var.availability_domain
28 | instance_id = oci_core_instance.nat_instance.id
29 | }
30 |
31 | data "oci_core_private_ips" "nat_instance_private_ips_by_nic" {
32 | depends_on = [
33 | oci_core_instance.nat_instance
34 | ]
35 | ip_address = oci_core_instance.nat_instance.private_ip
36 | subnet_id = var.public_subnet_id
37 | vnic_id = data.oci_core_vnic_attachments.nat_instance_vnics.vnic_id
38 | }
--------------------------------------------------------------------------------
/nat-instance/files/setup_bastion.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | %{ if setup_bastion }
4 |
5 |
6 | groupadd ${bastion_group}
7 | useradd -d /home/${bastion_user} -r -g ${bastion_group} ${bastion_user}
8 | mkdir -p /home/${bastion_user}/.ssh
9 | touch /home/${bastion_user}/.ssh/authorized_keys
10 | %{ for ssh_key in ssh_keys }
11 | echo "${ssh_key}" >> /home/${bastion_user}/.ssh/authorized_keys
12 | %{ endfor }
13 | chown -R ${bastion_group}:${bastion_user} /home/${bastion_user}/
14 | chmod 400 /home/${bastion_user}/.ssh/authorized_keys
15 |
16 | cat <> /etc/ssh/sshd_config
17 | Match Group ${bastion_group}
18 | AllowAgentForwarding no
19 | AllowTcpForwarding yes
20 | X11Forwarding no
21 | PermitTunnel no
22 | GatewayPorts no
23 | ForceCommand echo 'This account can only be used for ProxyJump (ssh -J)'
24 | EOD
25 |
26 | systemctl restart sshd.service
27 |
28 | %{ endif }
--------------------------------------------------------------------------------
/nat-instance/files/setup_nat.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | apt-get update
4 | apt install -y firewalld
5 |
6 | # Firewall rules
7 | default_iface=$(ip route get 8.8.8.8 | grep -Po '(?<=(dev ))(\S+)')
8 | firewall-offline-cmd --direct --add-rule ipv4 nat POSTROUTING 0 -o $default_iface -j MASQUERADE
9 | firewall-offline-cmd --direct --add-rule ipv4 filter FORWARD 0 -i $default_iface -j ACCEPT
10 | /bin/systemctl restart firewalld
11 |
12 | # sysctl conf
13 | echo "net.ipv4.ip_forward = 1" > /etc/sysctl.d/98-ip-forward.conf
14 | sysctl -p /etc/sysctl.d/98-ip-forward.conf
--------------------------------------------------------------------------------
/nat-instance/local.tf:
--------------------------------------------------------------------------------
1 | locals {
2 | tags = {
3 | "oracle-tutorial" = "terraform"
4 | "environment" = "${var.environment}"
5 | }
6 |
7 | ssh_keys = [for ssh_key in var.ssh_keys_path : file(ssh_key)]
8 | }
--------------------------------------------------------------------------------
/nat-instance/nat_instance.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_instance" "nat_instance" {
2 | agent_config {
3 | is_management_disabled = "false"
4 | is_monitoring_disabled = "false"
5 |
6 | plugins_config {
7 | desired_state = "DISABLED"
8 | name = "Vulnerability Scanning"
9 | }
10 |
11 | plugins_config {
12 | desired_state = "ENABLED"
13 | name = "Compute Instance Monitoring"
14 | }
15 |
16 | plugins_config {
17 | desired_state = "DISABLED"
18 | name = "Bastion"
19 | }
20 | }
21 |
22 | availability_config {
23 | recovery_action = "RESTORE_INSTANCE"
24 | }
25 |
26 | availability_domain = var.availability_domain
27 | compartment_id = var.compartment_ocid
28 | fault_domain = var.default_fault_domain
29 |
30 | create_vnic_details {
31 | assign_private_dns_record = true
32 | assign_public_ip = true
33 | subnet_id = var.public_subnet_id
34 | skip_source_dest_check = true
35 | }
36 |
37 | display_name = "NATINSTANCE"
38 |
39 | instance_options {
40 | are_legacy_imds_endpoints_disabled = false
41 | }
42 |
43 | is_pv_encryption_in_transit_enabled = true
44 |
45 | metadata = {
46 | "ssh_authorized_keys" = file(var.PATH_TO_PUBLIC_KEY)
47 | "user_data" = data.cloudinit_config.nat_instance_init.rendered
48 | }
49 |
50 | shape = "VM.Standard.A1.Flex"
51 | shape_config {
52 | memory_in_gbs = "6"
53 | ocpus = "1"
54 | }
55 |
56 | source_details {
57 | source_id = var.os_image_id
58 | source_type = "image"
59 | }
60 |
61 | freeform_tags = local.tags
62 | }
--------------------------------------------------------------------------------
/nat-instance/output.tf:
--------------------------------------------------------------------------------
1 | output "nat_instance_id" {
2 | value = oci_core_instance.nat_instance.id
3 | }
4 |
5 | output "nat_instance_public_ip" {
6 | value = oci_core_instance.nat_instance.public_ip
7 | }
--------------------------------------------------------------------------------
/nat-instance/route_table.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_route_table" "nat_instance_route_table" {
2 |
3 | depends_on = [
4 | oci_core_instance.nat_instance
5 | ]
6 |
7 | compartment_id = var.compartment_ocid
8 | vcn_id = var.vcn_id
9 | display_name = "NAT instance route table"
10 |
11 | route_rules {
12 | network_entity_id = data.oci_core_private_ips.nat_instance_private_ips_by_nic.private_ips[0].id
13 |
14 | description = "Route internet traffic via nat instance"
15 | destination = "0.0.0.0/0"
16 | destination_type = "CIDR_BLOCK"
17 | }
18 | }
19 |
20 | resource "oci_core_route_table_attachment" "attach_route_table" {
21 | subnet_id = var.private_subnet_id
22 | route_table_id = oci_core_route_table.nat_instance_route_table.id
23 | }
--------------------------------------------------------------------------------
/nat-instance/terraform.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/nat-instance/vars.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 |
3 | }
4 |
5 | variable "region" {
6 |
7 | }
8 |
9 | variable "availability_domain" {
10 |
11 | }
12 |
13 | variable "environment" {
14 | type = string
15 | }
16 |
17 | variable "PATH_TO_PUBLIC_KEY" {
18 | type = string
19 | default = "~/.ssh/id_rsa.pub"
20 | description = "Path to your public key"
21 | }
22 |
23 | variable "setup_bastion" {
24 | type = bool
25 | default = true
26 | }
27 |
28 | variable "bastion_user" {
29 | type = string
30 | default = "bastion"
31 | }
32 |
33 | variable "bastion_group" {
34 | type = string
35 | default = "bastion"
36 | }
37 |
38 | variable "ssh_keys_path" {
39 | type = list(any)
40 | default = ["~/.ssh/id_rsa.pub"]
41 | }
42 |
43 | variable "default_fault_domain" {
44 | default = "FAULT-DOMAIN-1"
45 | }
46 |
47 | variable "fault_domains" {
48 | type = list(any)
49 | default = ["FAULT-DOMAIN-1", "FAULT-DOMAIN-2", "FAULT-DOMAIN-3"]
50 | }
51 |
52 | variable "vcn_id" {
53 | type = string
54 | }
55 |
56 | variable "private_subnet_id" {
57 | type = string
58 | }
59 |
60 | variable "public_subnet_id" {
61 | type = string
62 | }
63 |
64 | variable "os_image_id" {
65 | type = string
66 | default = "ocid1.image.oc1.eu-zurich-1.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q" # Canonical-Ubuntu-20.04-aarch64-2022.01.18-0
67 | }
--------------------------------------------------------------------------------
/network-load-balancer/README.md:
--------------------------------------------------------------------------------
1 | # Network load balancer (Layer 4)
2 |
3 | This module will deploy a Network Load Balancer (L4). The LB can be public or private (*is_private* variable). The LB will be attached to the instances in a instance pool (*instance_pool_id* variable).
4 |
5 | ### Requirements
6 |
7 | * One vcn with a public or private subnet (simple-vcn or private-vcn module)
8 |
9 | ### Module variables
10 |
11 | | Var | Required | Desc |
12 | | ------- | ------- | ----------- |
13 | | `region` | `yes` | set the correct OCI region based on your needs |
14 | | `compartment_ocid` | `yes` | Set the correct compartment ocid. See [how](../README.md#oracle-provider-setup) to find the compartment ocid |
15 | | `vcn_id` | `yes` | The VCN OCID |
16 | | `private_subnet_id` | `yes` | Private subnet OCID |
17 | | `public_subnet_id` | `yes` | Public subnet OCID |
18 | | `instance_pool_size` | `yes` | Instance pool size |
19 | | `instance_pool_id` | `yes` | Instance pool ocid OCID |
20 | | `is_private` | `no` | Bool value. If true the LB will be a private LB (no public ip) Default: true |
21 |
22 | ### Output
23 |
24 | lb_ip, LB public or private ip address
--------------------------------------------------------------------------------
/network-load-balancer/data.tf:
--------------------------------------------------------------------------------
1 | data "oci_core_instance_pool_instances" "ubuntu_instance_pool_instances" {
2 | compartment_id = var.compartment_ocid
3 | instance_pool_id = var.instance_pool_id
4 | }
5 |
6 | data "oci_core_instance" "ubuntu_instance_pool_instances_ips" {
7 | count = var.instance_pool_size
8 | instance_id = data.oci_core_instance_pool_instances.ubuntu_instance_pool_instances.instances[count.index].id
9 | }
--------------------------------------------------------------------------------
/network-load-balancer/lb.tf:
--------------------------------------------------------------------------------
1 | resource "oci_network_load_balancer_network_load_balancer" "load_balancer_l4" {
2 | compartment_id = var.compartment_ocid
3 | display_name = "Network LB Layer 4"
4 | subnet_id = var.private_subnet_id
5 |
6 | is_private = var.is_private
7 | is_preserve_source_destination = false
8 |
9 | freeform_tags = local.tags
10 | }
11 |
12 | resource "oci_network_load_balancer_listener" "http_listener_l4" {
13 | default_backend_set_name = oci_network_load_balancer_backend_set.http_backend_set_l4.name
14 | name = "LB test listener"
15 | network_load_balancer_id = oci_network_load_balancer_network_load_balancer.load_balancer_l4.id
16 | port = 80
17 | protocol = "TCP"
18 | }
19 |
20 | resource "oci_network_load_balancer_backend_set" "http_backend_set_l4" {
21 | health_checker {
22 | protocol = "TCP"
23 | port = 80
24 | }
25 |
26 | name = "Backend set test"
27 | network_load_balancer_id = oci_network_load_balancer_network_load_balancer.load_balancer_l4.id
28 | policy = "FIVE_TUPLE"
29 | is_preserve_source = true
30 | }
31 |
32 | resource "oci_network_load_balancer_backend" "http_backend_l4" {
33 | count = var.instance_pool_size
34 | backend_set_name = oci_network_load_balancer_backend_set.http_backend_set_l4.name
35 | network_load_balancer_id = oci_network_load_balancer_network_load_balancer.load_balancer_l4.id
36 | port = 80
37 |
38 | target_id = data.oci_core_instance_pool_instances.ubuntu_instance_pool_instances.instances[count.index].id
39 | }
--------------------------------------------------------------------------------
/network-load-balancer/local.tf:
--------------------------------------------------------------------------------
1 | locals {
2 | tags = {
3 | "oracle-tutorial" = "terraform"
4 | "environment" = "${var.environment}"
5 | }
6 | }
--------------------------------------------------------------------------------
/network-load-balancer/output.tf:
--------------------------------------------------------------------------------
1 | output "lb_ip" {
2 | value = oci_network_load_balancer_network_load_balancer.load_balancer_l4.ip_addresses
3 | }
--------------------------------------------------------------------------------
/network-load-balancer/terraform.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/network-load-balancer/vars.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 |
3 | }
4 |
5 | variable "region" {
6 |
7 | }
8 |
9 | variable "environment" {
10 | type = string
11 | }
12 |
13 | variable "instance_pool_id" {
14 | type = string
15 | }
16 |
17 | variable "is_private" {
18 | type = bool
19 | default = true
20 | }
21 |
22 | variable "vcn_id" {
23 | type = string
24 | }
25 |
26 | variable "public_subnet_id" {
27 | type = string
28 | }
29 |
30 | variable "private_subnet_id" {
31 | type = string
32 | }
33 |
34 | variable "instance_pool_size" {
35 | type = number
36 | default = 2
37 | }
--------------------------------------------------------------------------------
/private-vcn/README.md:
--------------------------------------------------------------------------------
1 | # VCN with private and public subnet
2 |
3 | This example will deploy a one VCN with one public subnet ad one private subnet. Also this modules deploy a security list with the following rueles:
4 |
5 | * egress, all traffic allowed
6 | * ingress, traffica allowed on port 22 only from *my_public_ip_cidr*
7 |
8 | ### Requirements
9 |
10 | No Requirement
11 |
12 | ### Module variables
13 |
14 | | Var | Required | Desc |
15 | | ------- | ------- | ----------- |
16 | | `region` | `yes` | set the correct OCI region based on your needs |
17 | | `compartment_ocid` | `yes` | Set the correct compartment ocid. See [how](../README.md#oracle-provider-setup) to find the compartment ocid |
18 | | `my_public_ip_cidr` | `yes` | A public ip CIDR allowed to reach the OCI resources |
19 | | `environment` | `yes` | Current work environment (Example: staging/dev/prod). This value is used for tag all the deployed resources |
20 | | `oci_core_vcn_dns_label` | `no` | VCN DNS label. Default: defaultvcn |
21 | | `oci_core_subnet_dns_label10` | `no` | First subnet DNS label. Default: publicsubnet10 |
22 | | `oci_core_subnet_dns_label11` | `no` | Second subnet DNS label. Default: privatesubnet11 |
23 | | `oci_core_vcn_cidr` | `no` | VCN CIDR. Default: 10.0.0.0/16 |
24 | | `oci_core_subnet_cidr10` | `no` | First subnet CIDR. Default: 10.0.0.0/24 |
25 | | `oci_core_subnet_cidr11` | `no` | Second subnet CIDR. Default: 10.0.1.0/24 |
26 |
27 | ### Output
28 |
29 | The module will output:
30 |
31 | * vcn_id, the VCN OCID
32 | * public_subnet_id, the public subnet OCID
33 | * private_subnet_id, the private subnet OCID
34 | * security_list_id, the security list OCID
35 | * public_subnet_cidr, the public subnet CIDR
--------------------------------------------------------------------------------
/private-vcn/local.tf:
--------------------------------------------------------------------------------
1 | locals {
2 | tags = {
3 | "oracle-tutorial" = "terraform"
4 | "environment" = "${var.environment}"
5 | }
6 | }
--------------------------------------------------------------------------------
/private-vcn/network.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_vcn" "default_oci_core_vcn" {
2 | cidr_block = var.oci_core_vcn_cidr
3 | compartment_id = var.compartment_ocid
4 | display_name = "Default OCI core vcn"
5 | dns_label = var.oci_core_vcn_dns_label
6 | freeform_tags = local.tags
7 | }
8 |
9 | resource "oci_core_subnet" "default_oci_core_subnet10" {
10 | cidr_block = var.oci_core_subnet_cidr10
11 | compartment_id = var.compartment_ocid
12 | display_name = "${var.oci_core_subnet_cidr10} (default) PUBLIC OCI core subnet"
13 | dns_label = var.oci_core_subnet_dns_label10
14 | route_table_id = oci_core_vcn.default_oci_core_vcn.default_route_table_id
15 | vcn_id = oci_core_vcn.default_oci_core_vcn.id
16 | security_list_ids = [oci_core_default_security_list.default_security_list.id]
17 | freeform_tags = local.tags
18 | }
19 |
20 | resource "oci_core_subnet" "oci_core_subnet11" {
21 | cidr_block = var.oci_core_subnet_cidr11
22 | compartment_id = var.compartment_ocid
23 | display_name = "${var.oci_core_subnet_cidr11} PRIVATE OCI core subnet"
24 | dns_label = var.oci_core_subnet_dns_label11
25 | vcn_id = oci_core_vcn.default_oci_core_vcn.id
26 | prohibit_public_ip_on_vnic = true
27 | prohibit_internet_ingress = true
28 | security_list_ids = [oci_core_default_security_list.default_security_list.id]
29 | freeform_tags = local.tags
30 | }
31 |
32 | resource "oci_core_internet_gateway" "default_oci_core_internet_gateway" {
33 | compartment_id = var.compartment_ocid
34 | display_name = "Internet Gateway Default OCI core vcn"
35 | enabled = "true"
36 | vcn_id = oci_core_vcn.default_oci_core_vcn.id
37 | freeform_tags = local.tags
38 | }
39 |
40 | resource "oci_core_default_route_table" "default_oci_core_default_route_table" {
41 | route_rules {
42 | destination = "0.0.0.0/0"
43 | destination_type = "CIDR_BLOCK"
44 | network_entity_id = oci_core_internet_gateway.default_oci_core_internet_gateway.id
45 | }
46 | manage_default_resource_id = oci_core_vcn.default_oci_core_vcn.default_route_table_id
47 | }
--------------------------------------------------------------------------------
/private-vcn/output.tf:
--------------------------------------------------------------------------------
1 | output "vcn_id" {
2 | value = oci_core_vcn.default_oci_core_vcn.id
3 | }
4 |
5 | output "public_subnet_id" {
6 | value = oci_core_subnet.default_oci_core_subnet10.id
7 | }
8 |
9 | output "private_subnet_id" {
10 | value = oci_core_subnet.oci_core_subnet11.id
11 | }
12 |
13 | output "security_list_id" {
14 | value = oci_core_default_security_list.default_security_list.id
15 | }
16 |
17 | output "public_subnet_cidr" {
18 | value = oci_core_subnet.default_oci_core_subnet10.cidr_block
19 | }
--------------------------------------------------------------------------------
/private-vcn/security.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_default_security_list" "default_security_list" {
2 | compartment_id = var.compartment_ocid
3 | manage_default_resource_id = oci_core_vcn.default_oci_core_vcn.default_security_list_id
4 |
5 | display_name = "Default security list"
6 | egress_security_rules {
7 | destination = "0.0.0.0/0"
8 | protocol = "all"
9 | }
10 |
11 | ingress_security_rules {
12 | protocol = 1 # icmp
13 | source = var.my_public_ip_cidr
14 |
15 | description = "Allow icmp from ${var.my_public_ip_cidr}"
16 |
17 | }
18 |
19 | ingress_security_rules {
20 | protocol = 6 # tcp
21 | source = var.my_public_ip_cidr
22 |
23 | description = "Allow SSH from ${var.my_public_ip_cidr}"
24 |
25 | tcp_options {
26 | min = 22
27 | max = 22
28 | }
29 | }
30 |
31 | ingress_security_rules {
32 | protocol = "all"
33 | source = var.oci_core_vcn_cidr
34 |
35 | description = "Allow all from vcn subnet"
36 | }
37 |
38 | freeform_tags = local.tags
39 | }
--------------------------------------------------------------------------------
/private-vcn/terraform.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/private-vcn/vars.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 |
3 | }
4 |
5 | variable "region" {
6 |
7 | }
8 |
9 | variable "fault_domains" {
10 | type = list(any)
11 | default = ["FAULT-DOMAIN-1", "FAULT-DOMAIN-2", "FAULT-DOMAIN-3"]
12 | }
13 |
14 | variable "oci_core_vcn_cidr" {
15 | default = "10.0.0.0/16"
16 | }
17 |
18 | variable "oci_core_subnet_cidr10" {
19 | default = "10.0.0.0/24"
20 | }
21 |
22 | variable "oci_core_subnet_cidr11" {
23 | default = "10.0.1.0/24"
24 | }
25 |
26 | variable "oci_core_subnet_dns_label10" {
27 | default = "publicsubnet10"
28 | }
29 |
30 | variable "oci_core_vcn_dns_label" {
31 | default = "defaultvcn"
32 | }
33 |
34 | variable "oci_core_subnet_dns_label11" {
35 | default = "privatesubnet11"
36 | }
37 |
38 | variable "my_public_ip_cidr" {
39 | type = string
40 | description = "My public ip CIDR"
41 | }
42 |
43 | variable "environment" {
44 | type = string
45 | }
--------------------------------------------------------------------------------
/simple-instance/README.md:
--------------------------------------------------------------------------------
1 | # Simple compute instance
2 |
3 | This module will deploy a single Oracle compute instance, in a private or in a public subnet.
4 |
5 | If you choose to publish this compute instance in a private subnet, you need a NAT instance (refer to nat-instance module). The nat instance can be used also as bation host to reach the private instance, or if you prefer you can deploy a dedicated bastion host (refer to bastion-host module).
6 |
7 | ### Requirements
8 |
9 | * One vcn with a public or private subnet (simple-vcn or private-vcn module)
10 | * One nat instance if the instance *is_private* (nat-instance module)
11 |
12 | ### Module variables
13 |
14 | | Var | Required | Desc |
15 | | ------- | ------- | ----------- |
16 | | `region` | `yes` | set the correct OCI region based on your needs |
17 | | `availability_domain` | `yes` | Set the correct availability domain. See [how](../README.md#how-to-find-the-availability-doamin-name) to find the availability domain|
18 | | `compartment_ocid` | `yes` | Set the correct compartment ocid. See [how](../README.md#oracle-provider-setup) to find the compartment ocid |
19 | | `vcn_id` | `yes` | The VCN OCID |
20 | | `private_subnet_id` | `yes` | Private subnet OCID |
21 | | `public_subnet_id` | `yes` | Public subnet OCID |
22 | | `environment` | `yes` | Current work environment (Example: staging/dev/prod). This value is used for tag all the deployed resources |
23 | | `default_fault_domain` | `no` | Fault domain where the instance will be deployed. Default: FAULT-DOMAIN-1 |
24 | | `is_private` | `no` | Bool value. If true the instance will be deployed in a private subnet. Default: false |
25 | | `os_image_id` | `no` | OS image OCID. Default: ocid1.image.oc1.eu-zurich-1.aaaaaaaag2uyozo7266bmg26j5ixvi42jhaujso2pddpsigtib6vfnqy5f6q - Canonical-Ubuntu-20.04-aarch64-2022.01.18-0 |
26 |
27 | ### Output
28 |
29 | The module will show the instane ip: public or private (based on the value of the variable *is_private*)
--------------------------------------------------------------------------------
/simple-instance/compute.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_instance" "ubuntu_oci_instance" {
2 | agent_config {
3 | is_management_disabled = "false"
4 | is_monitoring_disabled = "false"
5 |
6 | plugins_config {
7 | desired_state = "DISABLED"
8 | name = "Vulnerability Scanning"
9 | }
10 |
11 | plugins_config {
12 | desired_state = "ENABLED"
13 | name = "Compute Instance Monitoring"
14 | }
15 |
16 | plugins_config {
17 | desired_state = "DISABLED"
18 | name = "Bastion"
19 | }
20 | }
21 |
22 | availability_config {
23 | recovery_action = "RESTORE_INSTANCE"
24 | }
25 |
26 | availability_domain = var.availability_domain
27 | compartment_id = var.compartment_ocid
28 | fault_domain = var.default_fault_domain
29 |
30 | create_vnic_details {
31 | assign_private_dns_record = true
32 | assign_public_ip = var.is_private == true ? false : true
33 | subnet_id = var.is_private == true ? var.private_subnet_id : var.public_subnet_id
34 | }
35 |
36 | display_name = "Ubuntu Instance"
37 |
38 | instance_options {
39 | are_legacy_imds_endpoints_disabled = false
40 | }
41 |
42 | is_pv_encryption_in_transit_enabled = true
43 |
44 | metadata = {
45 | "ssh_authorized_keys" = file(var.PATH_TO_PUBLIC_KEY)
46 | "user_data" = data.cloudinit_config.ubuntu_init.rendered
47 | }
48 |
49 | shape = var.shape
50 | shape_config {
51 | memory_in_gbs = var.memory_in_gbs
52 | ocpus = var.ocpus
53 | }
54 |
55 | source_details {
56 | source_id = var.os_image_id
57 | source_type = "image"
58 | }
59 |
60 | freeform_tags = local.tags
61 | }
--------------------------------------------------------------------------------
/simple-instance/data.tf:
--------------------------------------------------------------------------------
1 | data "cloudinit_config" "ubuntu_init" {
2 | gzip = true
3 | base64_encode = true
4 |
5 | part {
6 | content_type = "text/x-shellscript"
7 | content = templatefile("${path.module}/files/oci-ubuntu-install.sh", {})
8 | }
9 | }
--------------------------------------------------------------------------------
/simple-instance/files/oci-ubuntu-install.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | apt-get update
4 | apt-get -y upgrade
5 | apt-get -y install nginx
6 |
7 | systemctl enable nginx
8 | systemctl start nginx
9 |
10 | hostname=$(hostname)
11 |
12 | cat < /var/www/html/index.nginx-debian.html
13 |
14 |
15 |
16 | Welcome to nginx!
17 |
24 |
25 |
26 | Welcome to nginx!
27 | If you see this page, the nginx web server is successfully installed and
28 | working. Further configuration is required.
29 |
30 | For online documentation and support please refer to
31 | nginx.org.
32 | Commercial support is available at
33 | nginx.com.
34 |
35 | Thank you for using nginx.
36 | Hello from: $hostname
37 |
38 |
39 | EOF
40 |
41 | # Disable firewall
42 | /usr/sbin/netfilter-persistent stop
43 | /usr/sbin/netfilter-persistent flush
44 |
45 | systemctl stop netfilter-persistent.service
46 | systemctl disable netfilter-persistent.service
--------------------------------------------------------------------------------
/simple-instance/local.tf:
--------------------------------------------------------------------------------
1 | locals {
2 | tags = {
3 | "oracle-tutorial" = "terraform"
4 | "environment" = "${var.environment}"
5 | }
6 | }
--------------------------------------------------------------------------------
/simple-instance/output.tf:
--------------------------------------------------------------------------------
1 | output "instance_ip" {
2 | value = var.is_private == true ? oci_core_instance.ubuntu_oci_instance.private_ip : oci_core_instance.ubuntu_oci_instance.public_ip
3 | }
--------------------------------------------------------------------------------
/simple-instance/terraform.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/simple-instance/vars.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 |
3 | }
4 |
5 | variable "region" {
6 |
7 | }
8 |
9 | variable "availability_domain" {
10 |
11 | }
12 |
13 | variable "PATH_TO_PUBLIC_KEY" {
14 | type = string
15 | default = "~/.ssh/id_rsa.pub"
16 | description = "Path to your public key"
17 | }
18 |
19 | variable "environment" {
20 | type = string
21 | }
22 |
23 | variable "is_private" {
24 | type = bool
25 | default = false
26 | }
27 |
28 | variable "public_subnet_id" {
29 | type = string
30 | }
31 |
32 | variable "private_subnet_id" {
33 | type = string
34 | }
35 |
36 | variable "default_fault_domain" {
37 | default = "FAULT-DOMAIN-1"
38 | }
39 |
40 | variable "os_image_id" {
41 | default = "ocid1.image.oc1.eu-zurich-1.aaaaaaaabt5i2qa7sdt65orrb66anzyljybm3furr2q7ykxodt5zmfxqbyzq" # Canonical-Ubuntu-22.04-aarch64-2023.07.20-0
42 | }
43 |
44 | variable "shape" {
45 | default = "VM.Standard.A1.Flex" # VM.Standard.E2.1.Micro
46 | }
47 |
48 | variable "memory_in_gbs" {
49 | default = "6"
50 | }
51 |
52 | variable "ocpus" {
53 | default = "1"
54 | }
--------------------------------------------------------------------------------
/simple-vcn/README.md:
--------------------------------------------------------------------------------
1 | # VCN with two public subnets
2 |
3 | This module will deploy a one VCN with two public subnets. Also this modules deploy a security list with the following rueles:
4 |
5 | * egress, all traffic allowed
6 | * ingress, traffica allowed on port 22 only from *my_public_ip_cidr*
7 |
8 | ### Requirements
9 |
10 | No Requirement
11 |
12 | ### Module variables
13 |
14 | | Var | Required | Desc |
15 | | ------- | ------- | ----------- |
16 | | `region` | `yes` | set the correct OCI region based on your needs |
17 | | `compartment_ocid` | `yes` | Set the correct compartment ocid. See [how](../README.md#oracle-provider-setup) to find the compartment ocid |
18 | | `my_public_ip_cidr` | `yes` | A public ip CIDR allowed to reach the OCI resources |
19 | | `environment` | `yes` | Current work environment (Example: staging/dev/prod). This value is used for tag all the deployed resources |
20 | | `oci_core_vcn_dns_label` | `no` | VCN DNS label. Default: defaultvcn |
21 | | `oci_core_subnet_dns_label10` | `no` | First subnet DNS label. Default: publicsubnet10 |
22 | | `oci_core_subnet_dns_label11` | `no` | Second subnet DNS label. Default: publicsubnet11 |
23 | | `oci_core_vcn_cidr` | `no` | VCN CIDR. Default: 10.0.0.0/16 |
24 | | `oci_core_subnet_cidr10` | `no` | First subnet CIDR. Default: 10.0.0.0/24 |
25 | | `oci_core_subnet_cidr11` | `no` | Second subnet CIDR. Default: 10.0.1.0/24 |
26 |
27 | ### Output
28 |
29 | The module will output:
30 |
31 | * vcn_id, the VCN OCID
32 | * public_subnet_id, the public subnet OCID
33 | * secondary_public_subnet_id, the secondary public subnet OCID
34 | * security_list_id, the security list OCID
35 | * public_subnet_cidr, the public subnet CIDR
--------------------------------------------------------------------------------
/simple-vcn/local.tf:
--------------------------------------------------------------------------------
1 | locals {
2 | tags = {
3 | "oracle-tutorial" = "terraform"
4 | "environment" = "${var.environment}"
5 | }
6 | }
--------------------------------------------------------------------------------
/simple-vcn/network.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_vcn" "default_oci_core_vcn" {
2 | cidr_block = var.oci_core_vcn_cidr
3 | compartment_id = var.compartment_ocid
4 | display_name = "Default OCI core vcn"
5 | dns_label = var.oci_core_vcn_dns_label
6 | freeform_tags = local.tags
7 | }
8 |
9 | resource "oci_core_subnet" "default_oci_core_subnet10" {
10 | cidr_block = var.oci_core_subnet_cidr10
11 | compartment_id = var.compartment_ocid
12 | display_name = "${var.oci_core_subnet_cidr10} (default) PUBLIC OCI core subnet"
13 | dns_label = var.oci_core_subnet_dns_label10
14 | route_table_id = oci_core_vcn.default_oci_core_vcn.default_route_table_id
15 | vcn_id = oci_core_vcn.default_oci_core_vcn.id
16 | security_list_ids = [oci_core_default_security_list.default_security_list.id]
17 | freeform_tags = local.tags
18 | }
19 |
20 | resource "oci_core_subnet" "oci_core_subnet11" {
21 | cidr_block = var.oci_core_subnet_cidr11
22 | compartment_id = var.compartment_ocid
23 | display_name = "${var.oci_core_subnet_cidr11} PUBLIC OCI core subnet"
24 | dns_label = var.oci_core_subnet_dns_label11
25 | route_table_id = oci_core_vcn.default_oci_core_vcn.default_route_table_id
26 | vcn_id = oci_core_vcn.default_oci_core_vcn.id
27 | security_list_ids = [oci_core_default_security_list.default_security_list.id]
28 | freeform_tags = local.tags
29 | }
30 |
31 | resource "oci_core_internet_gateway" "default_oci_core_internet_gateway" {
32 | compartment_id = var.compartment_ocid
33 | display_name = "Internet Gateway Default OCI core vcn"
34 | enabled = "true"
35 | vcn_id = oci_core_vcn.default_oci_core_vcn.id
36 | freeform_tags = local.tags
37 | }
38 |
39 | resource "oci_core_default_route_table" "default_oci_core_default_route_table" {
40 | route_rules {
41 | destination = "0.0.0.0/0"
42 | destination_type = "CIDR_BLOCK"
43 | network_entity_id = oci_core_internet_gateway.default_oci_core_internet_gateway.id
44 | }
45 | manage_default_resource_id = oci_core_vcn.default_oci_core_vcn.default_route_table_id
46 | }
--------------------------------------------------------------------------------
/simple-vcn/output.tf:
--------------------------------------------------------------------------------
1 | output "vcn_id" {
2 | value = oci_core_vcn.default_oci_core_vcn.id
3 | }
4 |
5 | output "public_subnet_id" {
6 | value = oci_core_subnet.default_oci_core_subnet10.id
7 | }
8 |
9 | output "secondary_public_subnet_id" {
10 | value = oci_core_subnet.oci_core_subnet11.id
11 | }
12 |
13 | output "security_list_id" {
14 | value = oci_core_default_security_list.default_security_list.id
15 | }
16 |
17 | output "public_subnet_cidr" {
18 | value = oci_core_subnet.default_oci_core_subnet10.cidr_block
19 | }
--------------------------------------------------------------------------------
/simple-vcn/security.tf:
--------------------------------------------------------------------------------
1 | resource "oci_core_default_security_list" "default_security_list" {
2 | compartment_id = var.compartment_ocid
3 | manage_default_resource_id = oci_core_vcn.default_oci_core_vcn.default_security_list_id
4 |
5 | display_name = "Default security list"
6 | egress_security_rules {
7 | destination = "0.0.0.0/0"
8 | protocol = "all"
9 | }
10 |
11 | ingress_security_rules {
12 | protocol = 1 # icmp
13 | source = var.my_public_ip_cidr
14 |
15 | description = "Allow icmp from ${var.my_public_ip_cidr}"
16 |
17 | }
18 |
19 | ingress_security_rules {
20 | protocol = 6 # tcp
21 | source = var.my_public_ip_cidr
22 |
23 | description = "Allow SSH from ${var.my_public_ip_cidr}"
24 |
25 | tcp_options {
26 | min = 22
27 | max = 22
28 | }
29 | }
30 |
31 | ingress_security_rules {
32 | protocol = "all"
33 | source = var.oci_core_vcn_cidr
34 |
35 | description = "Allow all from vcn subnet"
36 | }
37 |
38 | freeform_tags = local.tags
39 | }
--------------------------------------------------------------------------------
/simple-vcn/terraform.tf:
--------------------------------------------------------------------------------
1 | terraform {
2 | required_providers {
3 | oci = {
4 | source = "oracle/oci"
5 | version = ">= 4.64.0"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/simple-vcn/vars.tf:
--------------------------------------------------------------------------------
1 | variable "compartment_ocid" {
2 |
3 | }
4 |
5 | variable "region" {
6 |
7 | }
8 |
9 | variable "fault_domains" {
10 | type = list(any)
11 | default = ["FAULT-DOMAIN-1", "FAULT-DOMAIN-2", "FAULT-DOMAIN-3"]
12 | }
13 |
14 | variable "oci_core_vcn_cidr" {
15 | default = "10.0.0.0/16"
16 | }
17 |
18 | variable "oci_core_subnet_cidr10" {
19 | default = "10.0.0.0/24"
20 | }
21 |
22 | variable "oci_core_subnet_cidr11" {
23 | default = "10.0.1.0/24"
24 | }
25 |
26 | variable "oci_core_vcn_dns_label" {
27 | default = "defaultvcn"
28 | }
29 |
30 | variable "oci_core_subnet_dns_label10" {
31 | default = "publicsubnet10"
32 | }
33 |
34 | variable "oci_core_subnet_dns_label11" {
35 | default = "publicsubnet11"
36 | }
37 |
38 | variable "my_public_ip_cidr" {
39 | type = string
40 | description = "My public ip CIDR"
41 | }
42 |
43 | variable "environment" {
44 | type = string
45 | }
--------------------------------------------------------------------------------