Skip to content

clouddrove/terraform-gcp-subnet

Terraform gcp subnet

Terraform module to create subnet resource on google.

Terraform Licence tfsec static-checks


We eat, drink, sleep and most importantly love DevOps. We are working towards strategies for standardizing architecture while ensuring security for the infrastructure. We are strong believer of the philosophy Bigger problems are always solved by breaking them into smaller manageable problems. Resonating with microservices architecture, it is considered best-practice to run database, cluster, storage in smaller connected yet manageable pieces within the infrastructure.

This module is basically combination of Terraform open source and includes automatation tests and examples. It also helps to create and improve your infrastructure with minimalistic code instead of maintaining the whole infrastructure code yourself.

We have fifty plus terraform modules. A few of them are comepleted and are available for open source usage while a few others are in progress.

Prerequisites

This module has a few dependencies:

Examples

IMPORTANT: Since the master branch used in source varies based on new modifications, we suggest that you use the release versions here.

Here are some examples of how you can use this module in your inventory structure:

Default subnet

module "subnet" {
  source = "clouddrove/subnet/google"

  name        = "dev"
  environment = var.environment
  label_order = var.label_order
  gcp_region  = "us-central1"

  google_compute_subnetwork_enabled  = true
  google_compute_firewall_enabled    = true
  google_compute_router_nat_enabled  = true
  module_enabled                     = true
  ipv6_access_type                   = "EXTERNAL"
  network                            = module.vpc.vpc_id
  project_id                         = "clouddrove"
  private_ip_google_access           = true
  allow                              = [{ "protocol" : "tcp", "ports" : ["1-65535"] }]
  source_ranges                      = ["10.10.0.0/16"]
  asn                                = 64514
  nat_ip_allocate_option             = "MANUAL_ONLY"
  source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
  filter                             = "ERRORS_ONLY"
  dest_range                         = "0.0.0.0/0"
  next_hop_gateway                   = "default-internet-gateway"
  priority                           = 1000
  secondary_ip_ranges = [
    {
      "range_name" : "services",
      "ip_cidr_range" : "10.1.0.0/16"
    },

    {
      "range_name" : "pods",
      "ip_cidr_range" : "10.3.0.0/16"
    }
  ]
}

Inputs

Name Description Type Default Required
allow (Optional) The list of ALLOW rules specified by this firewall. Each rule specifies a protocol and port-range tuple that describes a permitted connection. list(any) [] no
asn Local BGP Autonomous System Number (ASN). Must be an RFC6996 private ASN, either 16-bit or 32-bit. number 64514 no
dest_range The destination range of outgoing packets that this route applies to. Only IPv4 is supported. string "0.0.0.0/0" no
environment Environment (e.g. prod, dev, staging). string "" no
filter Specifies the desired filtering of logs on this NAT. string "" no
gcp_region Google Cloud region string "europe-west3" no
google_compute_address_enabled n/a bool true no
google_compute_firewall_enabled n/a bool true no
google_compute_route_enabled n/a bool true no
google_compute_router_nat_enabled n/a bool true no
google_compute_subnetwork_enabled n/a bool true no
ip_cidr_range (Required) The range of internal addresses that are owned by this subnetwork. Provide this property when you create the subnetwork. For example, 10.0.0.0/8 or 192.168.0.0/16. Ranges must be unique and non-overlapping within a network. Only IPv4 is supported. string "10.10.0.0/24" no
ipv6_access_type The access type of IPv6 address this subnet holds. string "" no
label_order Label order, e.g. sequence of application name and environment name,environment,'attribute' [webserver,qa,devops,public,] . list(any) [] no
log_config (Optional) Logging options for the subnetwork flow logs. Setting this value to 'null' will disable them. See https://www.terraform.io/docs/providers/google/r/compute_subnetwork.html for more information and examples. any true no
module_enabled n/a bool true no
module_timeouts (Optional) How long certain operations (per resource type) are allowed to take before being considered to have failed. any {} no
name n/a string "" no
nat_ip_allocate_option How external IPs should be allocated for this NAT. string "MANUAL_ONLY" no
network (Required) The VPC network the subnets belong to. Only networks that are in the distributed mode can have subnetworks. string "" no
next_hop_gateway URL to a gateway that should handle matching packets. string "" no
priority The priority of this route. number 1000 no
private_ip_google_access (Optional) When enabled, VMs in this subnetwork without external IP addresses can access Google APIs and services by using Private Google Access. bool true no
project_id (Optional) The ID of the project in which the resource belongs. If it is not set, the provider project is used. string "" no
secondary_ip_ranges An array of configurations for secondary IP ranges for VM instances contained in this subnetwork. The primary IP of such VM must belong to the primary ipCidrRange of the subnetwork. The alias IPs may belong to either primary or secondary ranges. any [] no
source_ranges (Optional) If source ranges are specified, the firewall will apply only to traffic that has source IP address in these ranges. any [] no
source_subnetwork_ip_ranges_to_nat How NAT should be configured per Subnetwork. string "" no

Outputs

Name Description
id GCS bucket id.
name GCS bucket name.

Testing

In this module testing is performed with terratest and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a GO environment in your system.

You need to run the following command in the testing folder:

  go test -run Test

Feedback

If you come accross a bug or have any feedback, please log it in our issue tracker, or feel free to drop us an email at hello@clouddrove.com.

If you have found it worth your time, go ahead and give us a ★ on our GitHub!

About us

At CloudDrove, we offer expert guidance, implementation support and services to help organisations accelerate their journey to the cloud. Our services include docker and container orchestration, cloud migration and adoption, infrastructure automation, application modernisation and remediation, and performance engineering.

We are The Cloud Experts!


We ❤️ Open Source and you can check out our other modules to get help with your new Cloud ideas.