Skip to content

Commit 4337399

Browse files
committed
Initial scripts
1 parent fe921df commit 4337399

13 files changed

+737
-0
lines changed

compute.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
#
4+
5+
resource "oci_core_instance" "app_instance" {
6+
availability_domain = random_shuffle.compute_ad.result[count.index % length(random_shuffle.compute_ad.result)]
7+
compartment_id = var.compartment_ocid
8+
display_name = "DotNet-${random_string.deploy_id.result}-${count.index}"
9+
shape = var.instance_shape
10+
freeform_tags = local.common_tags
11+
12+
create_vnic_details {
13+
subnet_id = oci_core_subnet.dotnet_main_subnet.id
14+
display_name = "primaryvnic"
15+
assign_public_ip = (var.instance_visibility == "Private") ? false : true
16+
hostname_label = "dotnet-${random_string.deploy_id.result}-${count.index}"
17+
}
18+
19+
source_details {
20+
source_type = "image"
21+
source_id = lookup(data.oci_core_images.compute_images.images[0], "id")
22+
}
23+
24+
metadata = {
25+
ssh_authorized_keys = var.generate_public_ssh_key ? tls_private_key.compute_ssh_key.public_key_openssh : var.public_ssh_key
26+
user_data = data.template_cloudinit_config.instances.rendered
27+
}
28+
29+
count = var.num_instances
30+
}
31+
32+
### Important Security Notice ###
33+
# The private key generated by this resource will be stored unencrypted in your Terraform state file.
34+
# Use of this resource for production deployments is not recommended.
35+
# Instead, generate a private key file outside of Terraform and distribute it securely to the system where Terraform will be run.
36+
37+
# Generate ssh keys to access Compute Nodes, if generate_public_ssh_key=true, applies to the Compute
38+
resource "tls_private_key" "compute_ssh_key" {
39+
algorithm = "RSA"
40+
rsa_bits = 2048
41+
}

datasources.tf

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
#
4+
5+
# Gets a list of Availability Domains
6+
data "oci_identity_availability_domains" "ADs" {
7+
compartment_id = var.tenancy_ocid
8+
}
9+
10+
# Randoms
11+
resource "random_string" "deploy_id" {
12+
length = 4
13+
special = false
14+
}
15+
16+
# Check for resource limits
17+
## Check available compute shape
18+
data "oci_limits_services" "compute_services" {
19+
compartment_id = var.tenancy_ocid
20+
21+
filter {
22+
name = "name"
23+
values = ["compute"]
24+
}
25+
}
26+
data "oci_limits_limit_definitions" "compute_limit_definitions" {
27+
compartment_id = var.tenancy_ocid
28+
service_name = data.oci_limits_services.compute_services.services.0.name
29+
30+
filter {
31+
name = "description"
32+
values = [var.instance_shape]
33+
}
34+
}
35+
data "oci_limits_resource_availability" "compute_resource_availability" {
36+
compartment_id = var.tenancy_ocid
37+
limit_name = data.oci_limits_limit_definitions.compute_limit_definitions.limit_definitions[0].name
38+
service_name = data.oci_limits_services.compute_services.services.0.name
39+
availability_domain = data.oci_identity_availability_domains.ADs.availability_domains[count.index].name
40+
41+
count = length(data.oci_identity_availability_domains.ADs.availability_domains)
42+
}
43+
resource "random_shuffle" "compute_ad" {
44+
input = local.compute_available_limit_ad_list
45+
result_count = length(local.compute_available_limit_ad_list)
46+
}
47+
locals {
48+
compute_available_limit_ad_list = [for limit in data.oci_limits_resource_availability.compute_resource_availability : limit.availability_domain if(limit.available - var.num_instances) >= 0]
49+
compute_available_limit_error = length(local.compute_available_limit_ad_list) == 0 ? (
50+
file("ERROR: No limits available for the chosen compute shape and number of nodes")) : 0
51+
}
52+
53+
# Gets a list of supported images based on the shape, operating_system and operating_system_version provided
54+
data "oci_core_images" "compute_images" {
55+
compartment_id = var.compartment_ocid
56+
operating_system = var.image_operating_system
57+
operating_system_version = var.image_operating_system_version
58+
shape = var.instance_shape
59+
sort_by = "TIMECREATED"
60+
sort_order = "DESC"
61+
}
62+
63+
data "oci_identity_tenancy" "tenant_details" {
64+
tenancy_id = var.tenancy_ocid
65+
66+
provider = oci.current_region
67+
}
68+
69+
data "oci_identity_regions" "home_region" {
70+
filter {
71+
name = "key"
72+
values = [data.oci_identity_tenancy.tenant_details.home_region_key]
73+
}
74+
75+
provider = oci.current_region
76+
}
77+
78+
# Available Services
79+
data "oci_core_services" "all_services" {
80+
filter {
81+
name = "name"
82+
values = ["All .* Services In Oracle Services Network"]
83+
regex = true
84+
}
85+
}
86+
87+
locals {
88+
common_tags = {
89+
Reference = "Created by OCI QuickStart for DotNet sample"
90+
}
91+
}
92+
93+
# Cloud Init
94+
data "template_cloudinit_config" "instances" {
95+
gzip = true
96+
base64_encode = true
97+
98+
part {
99+
filename = "cloud-config.yaml"
100+
content_type = "text/cloud-config"
101+
content = data.template_file.cloud_init.rendered
102+
}
103+
}
104+
data "template_file" "cloud_init" {
105+
template = file("${path.module}/scripts/cloud-config.template.yaml")
106+
107+
vars = {
108+
setup_preflight_sh_content = base64gzip(data.template_file.setup_preflight.rendered)
109+
setup_template_sh_content = base64gzip(data.template_file.setup_template.rendered)
110+
deploy_template_content = base64gzip(data.template_file.deploy_template.rendered)
111+
}
112+
}
113+
data "template_file" "setup_preflight" {
114+
template = file("${path.module}/scripts/setup.preflight.sh")
115+
}
116+
data "template_file" "setup_template" {
117+
template = file("${path.module}/scripts/setup.template.sh")
118+
}
119+
data "template_file" "deploy_template" {
120+
template = file("${path.module}/scripts/deploy.template.sh")
121+
}

loadbalancer.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
#
4+
5+
resource "oci_load_balancer_load_balancer" "dotnet_lb" {
6+
compartment_id = var.compartment_ocid
7+
display_name = "DotNet-${random_string.deploy_id.result}"
8+
shape = var.lb_shape
9+
subnet_ids = [oci_core_subnet.dotnet_lb_subnet.id]
10+
is_private = "false"
11+
freeform_tags = local.common_tags
12+
}
13+
14+
resource "oci_load_balancer_backend_set" "dotnet_bes" {
15+
name = "dotnet-${random_string.deploy_id.result}"
16+
load_balancer_id = oci_load_balancer_load_balancer.dotnet_lb.id
17+
policy = "IP_HASH"
18+
19+
health_checker {
20+
port = local.app_port_number
21+
protocol = "HTTP"
22+
response_body_regex = ".*"
23+
url_path = "/"
24+
return_code = 200
25+
interval_ms = 5000
26+
timeout_in_millis = 2000
27+
retries = 10
28+
}
29+
}
30+
31+
resource "oci_load_balancer_backend" "dotnet-be" {
32+
load_balancer_id = oci_load_balancer_load_balancer.dotnet_lb.id
33+
backendset_name = oci_load_balancer_backend_set.dotnet_bes.name
34+
ip_address = element(oci_core_instance.app_instance.*.private_ip, count.index)
35+
port = local.app_port_number
36+
backup = false
37+
drain = false
38+
offline = false
39+
weight = 1
40+
41+
count = var.num_instances
42+
}
43+
44+
resource "oci_load_balancer_listener" "dotnet_listener_80" {
45+
load_balancer_id = oci_load_balancer_load_balancer.dotnet_lb.id
46+
default_backend_set_name = oci_load_balancer_backend_set.dotnet_bes.name
47+
name = "dotnet-${random_string.deploy_id.result}-80"
48+
port = local.http_port_number
49+
protocol = "HTTP"
50+
51+
connection_configuration {
52+
idle_timeout_in_seconds = "30"
53+
}
54+
}
55+
56+
resource "oci_load_balancer_listener" "dotnet_listener_443" {
57+
load_balancer_id = oci_load_balancer_load_balancer.dotnet_lb.id
58+
default_backend_set_name = oci_load_balancer_backend_set.dotnet_bes.name
59+
name = "dotnet-${random_string.deploy_id.result}-443"
60+
port = local.https_port_number
61+
protocol = "HTTP"
62+
63+
connection_configuration {
64+
idle_timeout_in_seconds = "30"
65+
}
66+
}

network.tf

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
#
4+
5+
resource "oci_core_virtual_network" "dotnet_main_vcn" {
6+
cidr_block = lookup(var.network_cidrs, "MAIN-VCN-CIDR")
7+
compartment_id = var.compartment_ocid
8+
display_name = "dotnet-main-${random_string.deploy_id.result}"
9+
dns_label = "dotnetmain${random_string.deploy_id.result}"
10+
freeform_tags = local.common_tags
11+
}
12+
13+
resource "oci_core_subnet" "dotnet_main_subnet" {
14+
cidr_block = lookup(var.network_cidrs, "MAIN-SUBNET-REGIONAL-CIDR")
15+
display_name = "dotnet-main-${random_string.deploy_id.result}"
16+
dns_label = "dotnetmain${random_string.deploy_id.result}"
17+
security_list_ids = [oci_core_security_list.dotnet_security_list.id]
18+
compartment_id = var.compartment_ocid
19+
vcn_id = oci_core_virtual_network.dotnet_main_vcn.id
20+
route_table_id = oci_core_route_table.dotnet_main_route_table.id
21+
dhcp_options_id = oci_core_virtual_network.dotnet_main_vcn.default_dhcp_options_id
22+
prohibit_public_ip_on_vnic = (var.instance_visibility == "Private") ? true : false
23+
freeform_tags = local.common_tags
24+
}
25+
26+
resource "oci_core_subnet" "dotnet_lb_subnet" {
27+
cidr_block = lookup(var.network_cidrs, ("MAIN-LB-SUBNET-REGIONAL-CIDR"))
28+
display_name = "dotnet-lb-${random_string.deploy_id.result}"
29+
dns_label = "dotnetlb${random_string.deploy_id.result}"
30+
security_list_ids = [oci_core_security_list.dotnet_lb_security_list.id]
31+
compartment_id = var.compartment_ocid
32+
vcn_id = oci_core_virtual_network.dotnet_main_vcn.id
33+
route_table_id = oci_core_route_table.dotnet_lb_route_table.id
34+
dhcp_options_id = oci_core_virtual_network.dotnet_main_vcn.default_dhcp_options_id
35+
prohibit_public_ip_on_vnic = false
36+
freeform_tags = local.common_tags
37+
}
38+
39+
resource "oci_core_route_table" "dotnet_main_route_table" {
40+
compartment_id = var.compartment_ocid
41+
vcn_id = oci_core_virtual_network.dotnet_main_vcn.id
42+
display_name = "dotnet-main-${random_string.deploy_id.result}"
43+
freeform_tags = local.common_tags
44+
45+
dynamic "route_rules" {
46+
for_each = (var.instance_visibility == "Private") ? [1] : []
47+
content {
48+
destination = lookup(data.oci_core_services.all_services.services[0], "cidr_block")
49+
destination_type = "SERVICE_CIDR_BLOCK"
50+
network_entity_id = oci_core_service_gateway.dotnet_service_gateway.id
51+
}
52+
}
53+
54+
dynamic "route_rules" {
55+
for_each = (var.instance_visibility == "Private") ? [] : [1]
56+
content {
57+
destination = lookup(var.network_cidrs, "ALL-CIDR")
58+
destination_type = "CIDR_BLOCK"
59+
network_entity_id = oci_core_internet_gateway.dotnet_internet_gateway.id
60+
}
61+
}
62+
63+
}
64+
65+
resource "oci_core_route_table" "dotnet_lb_route_table" {
66+
compartment_id = var.compartment_ocid
67+
vcn_id = oci_core_virtual_network.dotnet_main_vcn.id
68+
display_name = "dotnet-lb-${random_string.deploy_id.result}"
69+
freeform_tags = local.common_tags
70+
71+
route_rules {
72+
destination = lookup(var.network_cidrs, "ALL-CIDR")
73+
destination_type = "CIDR_BLOCK"
74+
network_entity_id = oci_core_internet_gateway.dotnet_internet_gateway.id
75+
}
76+
}
77+
78+
resource "oci_core_nat_gateway" "dotnet_nat_gateway" {
79+
block_traffic = "false"
80+
compartment_id = var.compartment_ocid
81+
display_name = "dotnet-nat-gateway-${random_string.deploy_id.result}"
82+
vcn_id = oci_core_virtual_network.dotnet_main_vcn.id
83+
freeform_tags = local.common_tags
84+
85+
count = var.use_only_always_free_elegible_resources ? 0 : ((var.instance_visibility == "Private") ? 0 : 0)
86+
}
87+
88+
resource "oci_core_internet_gateway" "dotnet_internet_gateway" {
89+
compartment_id = var.compartment_ocid
90+
display_name = "dotnet-internet-gateway-${random_string.deploy_id.result}"
91+
vcn_id = oci_core_virtual_network.dotnet_main_vcn.id
92+
freeform_tags = local.common_tags
93+
}
94+
95+
resource "oci_core_service_gateway" "dotnet_service_gateway" {
96+
compartment_id = var.compartment_ocid
97+
display_name = "dotnet-service-gateway-${random_string.deploy_id.result}"
98+
vcn_id = oci_core_virtual_network.dotnet_main_vcn.id
99+
services {
100+
service_id = lookup(data.oci_core_services.all_services.services[0], "id")
101+
}
102+
103+
count = var.use_only_always_free_elegible_resources ? 0 : 1
104+
}
105+
106+
107+
108+
109+
110+
111+

outputs.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
3+
#
4+
5+
output "app_public_url" {
6+
value = format("http://%s", lookup(oci_load_balancer_load_balancer.dotnet_lb.ip_address_details[0], "ip_address"))
7+
}
8+
9+
### Important Security Notice ###
10+
# The private key generated by this resource will be stored unencrypted in your Terraform state file.
11+
# Use of this resource for production deployments is not recommended.
12+
# Instead, generate a private key file outside of Terraform and distribute it securely to the system where Terraform will be run.
13+
output "generated_private_key_pem" {
14+
value = var.generate_public_ssh_key ? tls_private_key.compute_ssh_key.private_key_pem : "No Keys Auto Generated"
15+
}
16+
17+
output "dev" {
18+
value = "Made with \u2764 by Oracle Developers"
19+
}
20+
21+
output "comments" {
22+
value = "The application URL will be unavailable for a few minutes after provisioning, while the application is configured"
23+
}
24+
25+
output "deploy_id" {
26+
value = random_string.deploy_id.result
27+
}
28+
29+
output "deployed_to_region" {
30+
value = local.region_to_deploy
31+
}
32+

0 commit comments

Comments
 (0)