diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b87277c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +terraform.tfstate +terraform.tfstate.backup +.terraform diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/gcp/README.md b/gcp/README.md new file mode 100644 index 0000000..e69de29 diff --git a/gcp/computer_network/README.md b/gcp/computer_network/README.md new file mode 100644 index 0000000..3d2d0fc --- /dev/null +++ b/gcp/computer_network/README.md @@ -0,0 +1,107 @@ +# GCP VPC Terraform module + +Terraform module which creates VPC resources on GCP. + + +## Usage + +```hcl +module "network" { + source = "../.." + region = "us-east1" + name = "quark-labs" + project = "quarks-labs" + auto_create_subnetworks = false + + subnetworks = { + subenet1 = { + + name = "default-us-east1" + region = "us-east1" + ip_cidr_range = "172.28.0.0/27" + private_ip_google_access = false + + nat = { + nat_ip_allocate_option = "MANUAL_ONLY" + source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" + } + + secondary_ip_ranges = { + + primary = { + range_name = "primary" + ip_cidr_range = "172.1.16.0/20" + } + + secondary = { + range_name = "secondary" + ip_cidr_range = "172.1.32.0/20" + } + + } + } + } +} + +``` + + +## Contributing + +Report issues/questions/feature requests on in the [issues](https://github.com/terraform-gcp-modules/.../issues/new) section. + +Full contributing [guidelines are covered here](.github/contributing.md). + + + + + + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 0.14 | +| [google](#requirement\_google) | 5.24.0 | + +## Providers + +| Name | Version | +|------|---------| +| [google](#provider\_google) | 5.24.0 | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [google_compute_address.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_address) | resource | +| [google_compute_network.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_network) | resource | +| [google_compute_router.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_router) | resource | +| [google_compute_router_nat.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_router_nat) | resource | +| [google_compute_subnetwork.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_subnetwork) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [auto\_create\_subnetworks](#input\_auto\_create\_subnetworks) | If auto\_crea\_subnetworks is true, subnet is created automality | `bool` | n/a | yes | +| [name](#input\_name) | Name of GCP Network | `string` | n/a | yes | +| [project](#input\_project) | Name of GCP Network | `string` | n/a | yes | +| [region](#input\_region) | GCP Region | `string` | n/a | yes | +| [subnetworks](#input\_subnetworks) | Name of GCP Network | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [google\_compute\_network\_auto\_create\_subnetworks](#output\_google\_compute\_network\_auto\_create\_subnetworks) | auto\_create\_subnetworks of network | +| [google\_compute\_network\_description](#output\_google\_compute\_network\_description) | Description of network | +| [google\_compute\_network\_gateway\_ipv4](#output\_google\_compute\_network\_gateway\_ipv4) | gateway\_ipv4 of network | +| [google\_compute\_network\_id](#output\_google\_compute\_network\_id) | id of network | +| [google\_compute\_network\_internal\_ipv6\_range](#output\_google\_compute\_network\_internal\_ipv6\_range) | internal\_ipv6\_range of network | +| [google\_compute\_network\_name](#output\_google\_compute\_network\_name) | Name of network | + \ No newline at end of file diff --git a/gcp/computer_network/USAGE.md b/gcp/computer_network/USAGE.md new file mode 100644 index 0000000..c11bf25 --- /dev/null +++ b/gcp/computer_network/USAGE.md @@ -0,0 +1,49 @@ + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 0.14 | +| [google](#requirement\_google) | 5.24.0 | + +## Providers + +| Name | Version | +|------|---------| +| [google](#provider\_google) | 5.24.0 | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [google_compute_address.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_address) | resource | +| [google_compute_network.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_network) | resource | +| [google_compute_router.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_router) | resource | +| [google_compute_router_nat.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_router_nat) | resource | +| [google_compute_subnetwork.this](https://registry.terraform.io/providers/hashicorp/google/5.24.0/docs/resources/compute_subnetwork) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [auto\_create\_subnetworks](#input\_auto\_create\_subnetworks) | If auto\_crea\_subnetworks is true, subnet is created automality | `bool` | n/a | yes | +| [name](#input\_name) | Name of GCP Network | `string` | n/a | yes | +| [project](#input\_project) | Name of GCP Network | `string` | n/a | yes | +| [region](#input\_region) | GCP Region | `string` | n/a | yes | +| [subnetworks](#input\_subnetworks) | Name of GCP Network | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [google\_compute\_network\_auto\_create\_subnetworks](#output\_google\_compute\_network\_auto\_create\_subnetworks) | auto\_create\_subnetworks of network | +| [google\_compute\_network\_description](#output\_google\_compute\_network\_description) | Description of network | +| [google\_compute\_network\_gateway\_ipv4](#output\_google\_compute\_network\_gateway\_ipv4) | gateway\_ipv4 of network | +| [google\_compute\_network\_id](#output\_google\_compute\_network\_id) | id of network | +| [google\_compute\_network\_internal\_ipv6\_range](#output\_google\_compute\_network\_internal\_ipv6\_range) | internal\_ipv6\_range of network | +| [google\_compute\_network\_name](#output\_google\_compute\_network\_name) | Name of network | + \ No newline at end of file diff --git a/gcp/computer_network/docs/.gitkeep b/gcp/computer_network/docs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/gcp/computer_network/examples/simple-subnet/.terraform.lock.hcl b/gcp/computer_network/examples/simple-subnet/.terraform.lock.hcl new file mode 100644 index 0000000..872e09c --- /dev/null +++ b/gcp/computer_network/examples/simple-subnet/.terraform.lock.hcl @@ -0,0 +1,22 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/google" { + version = "5.24.0" + constraints = "5.24.0" + hashes = [ + "h1:RAuSwFWUzTeGWmPlFA1S6pIULW/zdd5Cjl7Yf2U03zg=", + "zh:108d318bd6b5aa6cbfd4f5ace415c92129e37a78dd8ec2a1fe863a07690ac6ee", + "zh:12f2dad5c506934c07b655ae6de083374231acfff9b648de4fb8de2564d1c83c", + "zh:24bc487ea381a34c7416726f96307c9c30332b49a9937ea55ffb3092783f03b2", + "zh:2c0ead7c00f428da762ab0f848d19d6dc4cf1d1da8b8ed9eca560cb0abc0ff6b", + "zh:34b0a6f3ec8d1b1d6ae22f1ed00209a96c757f7f63fe9aebaab7a185bf79b66d", + "zh:4470e00308b80e071023771d1dc5b02ae249874e1a0fa29e2e701118126791c2", + "zh:581ebed948ef9b85d2683be8682cd2f2a2a3c8d0a260c2412197b6d1e9e3f2a7", + "zh:974c091fb381dc5a39afb447666221177cae75ba89c2884f02f12a1586a8b5b9", + "zh:a9d7a728c94144410f54360c3f8d92e8913b2564779f238cd420196ebf4014cc", + "zh:ba20867988cd386709639a2669fec7f3e086d075dc189555b389b7024efb779a", + "zh:e79d978f6ece3bb9d35bcabdc163803ff6c9fdffff8d733d431c9edbfe90e016", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} diff --git a/gcp/computer_network/examples/simple-subnet/main.tf b/gcp/computer_network/examples/simple-subnet/main.tf new file mode 100644 index 0000000..a152530 --- /dev/null +++ b/gcp/computer_network/examples/simple-subnet/main.tf @@ -0,0 +1,39 @@ +provider "google" {} + +locals { + name = "quark-labs" + region = "us-east1" + project = "quarks-labs" + auto_create_subnetworks = false +} + +module "network" { + source = "../.." + region = local.region + name = local.name + auto_create_subnetworks = local.auto_create_subnetworks + project = local.project + + subnetworks = { + subenet1 = { + name = "default-us-east1" + region = "us-east1" + ip_cidr_range = "172.28.0.0/27" + private_ip_google_access = false + nat = { + nat_ip_allocate_option = "MANUAL_ONLY" + source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" + } + secondary_ip_ranges = { + primary = { + range_name = "primary" + ip_cidr_range = "172.1.16.0/20" + } + secondary = { + range_name = "secondary" + ip_cidr_range = "172.1.32.0/20" + } + } + } + } +} \ No newline at end of file diff --git a/gcp/computer_network/examples/simple-subnet/tf-backend.tf b/gcp/computer_network/examples/simple-subnet/tf-backend.tf new file mode 100644 index 0000000..3d0c056 --- /dev/null +++ b/gcp/computer_network/examples/simple-subnet/tf-backend.tf @@ -0,0 +1,5 @@ +terraform { + backend "local" { + path = "terraform.tfstate" + } +} \ No newline at end of file diff --git a/gcp/computer_network/main.tf b/gcp/computer_network/main.tf new file mode 100644 index 0000000..20b56ad --- /dev/null +++ b/gcp/computer_network/main.tf @@ -0,0 +1,98 @@ +################################################################################ +# NETWORK +################################################################################ + +resource "google_compute_network" "this" { + name = lower(var.name) + auto_create_subnetworks = var.auto_create_subnetworks + project = var.project +} + +################################################################################ +# SUBNET +################################################################################ + +resource "google_compute_subnetwork" "this" { + + for_each = var.subnetworks + + name = lower(each.value.name) + project = google_compute_network.this.project + network = google_compute_network.this.name + ip_cidr_range = each.value.ip_cidr_range + region = each.value.region + private_ip_google_access = try(each.value.private_ip_google_access, true) + + dynamic "secondary_ip_range" { + + for_each = each.value.secondary_ip_ranges + + content { + range_name = secondary_ip_range.value["range_name"] + ip_cidr_range = secondary_ip_range.value["ip_cidr_range"] + } + } + + depends_on = [google_compute_network.this] +} + +################################################################################ +# ADDRESS +################################################################################ + +resource "google_compute_address" "this" { + for_each = var.subnetworks + + name = format("%s-nat-ip", each.key) + project = google_compute_network.this.project + region = each.value.region + depends_on = [ + google_compute_subnetwork.this + ] +} + + +################################################################################ +# ROUTER +################################################################################ + +resource "google_compute_router" "this" { + for_each = var.subnetworks + + name = format("%s-cloud-router", google_compute_network.this.name) + project = google_compute_network.this.project + network = google_compute_network.this.name + region = each.value.region + + depends_on = [google_compute_network.this] +} + + +################################################################################ +# ROUTER NAT +################################################################################ + +resource "google_compute_router_nat" "this" { + + for_each = var.subnetworks + + name = format("%s-cloud-nat", google_compute_network.this.name) + project = google_compute_network.this.project + router = google_compute_router.this[each.key].name + region = each.value.region + nat_ips = toset([for address in google_compute_address.this : address.self_link ]) + nat_ip_allocate_option = try(each.value.nat.nat_ip_allocate_option, "MANUAL_ONLY") + source_subnetwork_ip_ranges_to_nat = try(each.value.nat.source_subnetwork_ip_ranges_to_nat, "LIST_OF_SUBNETWORKS") + + + subnetwork { + name = google_compute_subnetwork.this[each.key].self_link + source_ip_ranges_to_nat = try(each.value.source_ip_ranges_to_nat, ["PRIMARY_IP_RANGE", "LIST_OF_SECONDARY_IP_RANGES"]) + secondary_ip_range_names = toset([for range in each.value.secondary_ip_ranges : range.range_name ]) + } + + + depends_on = [ + google_compute_network.this + ] +} diff --git a/gcp/computer_network/tf-outputs.tf b/gcp/computer_network/tf-outputs.tf new file mode 100644 index 0000000..3f21f6b --- /dev/null +++ b/gcp/computer_network/tf-outputs.tf @@ -0,0 +1,33 @@ +output "google_compute_network_name" { + value = google_compute_network.this.name + description = "Name of network" +} + +output "google_compute_network_description" { + value = google_compute_network.this.description + description = "Description of network" +} + +output "google_compute_network_id" { + value = google_compute_network.this.id + description = "id of network" +} + +output "google_compute_network_internal_ipv6_range" { + value = google_compute_network.this.internal_ipv6_range + description = "internal_ipv6_range of network" +} + +output "google_compute_network_gateway_ipv4" { + value = google_compute_network.this.gateway_ipv4 + description = "gateway_ipv4 of network" +} + +output "google_compute_network_auto_create_subnetworks" { + value = google_compute_network.this.auto_create_subnetworks + description = "auto_create_subnetworks of network" +} + + + + diff --git a/gcp/computer_network/tf-providers.tf b/gcp/computer_network/tf-providers.tf new file mode 100644 index 0000000..e69de29 diff --git a/gcp/computer_network/tf-variables.tf b/gcp/computer_network/tf-variables.tf new file mode 100644 index 0000000..87e9c9f --- /dev/null +++ b/gcp/computer_network/tf-variables.tf @@ -0,0 +1,23 @@ +variable "region" { + type = string + description = "GCP Region" +} + +variable "name" { + type = string + description = "Name of GCP Network" +} +variable "project" { + type = string + description = "Name of GCP Network" +} +variable "subnetworks" { + type = any + description = "List of GCP Subnetworks" +} +variable "auto_create_subnetworks" { + type = bool + description = "If auto_crea_subnetworks is true, subnet is created automality" +} + + \ No newline at end of file diff --git a/gcp/computer_network/tf-versions.tf b/gcp/computer_network/tf-versions.tf new file mode 100644 index 0000000..b4bda62 --- /dev/null +++ b/gcp/computer_network/tf-versions.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + google = { + source = "hashicorp/google" + version = "5.24.0" + } + } + required_version = ">= 0.14" +} \ No newline at end of file