diff --git a/README.md b/README.md index 112fb54..365f973 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,23 @@ terraform_templates =============== -Terraform templates for various resources. \ No newline at end of file +Terraform templates for various resources. + +### Set up and running + +Place `main.tf` and `variables.tf` in a directory named `terraform` within the +project directory on the local system. + +Update variable names in `variables.tf` + +`gcloud auth application-default login` + +`terraform init` +`terraform apply` + +To destroy resources: +`terraform destroy` + +### vpc_with_subnets + +Creates a VPC named with one or more subnets (set count) in for . \ No newline at end of file diff --git a/vpc_with_subnetworks/main.tf b/vpc_with_subnetworks/main.tf new file mode 100644 index 0000000..e7fe7fa --- /dev/null +++ b/vpc_with_subnetworks/main.tf @@ -0,0 +1,35 @@ +# ref: +# https://www.easydeploy.io/blog/create-vpc-network-using-terraform-in-gcp/# + +provider "google" { + project = var.project_id + region = var.region +} + +data "google_compute_zones" "this" { + region = var.region + project = var.project_id +} + +locals { + type = ["public"] + zones = data.google_compute_zones.this.names +} + +# VPC +resource "google_compute_network" "this" { + name = "vpc-${var.name}" + delete_default_routes_on_create = false + auto_create_subnetworks = false + routing_mode = "REGIONAL" +} + +# Subnets +resource "google_compute_subnetwork" "this" { + count = 1 + name="${var.name}-${local.type[count.index]}-subnetwork" + ip_cidr_range= var.ip_cidr_range[count.index] + region=var.region + network=google_compute_network.this.id + private_ip_google_access=true +} \ No newline at end of file diff --git a/vpc_with_subnetworks/variables.tf b/vpc_with_subnetworks/variables.tf new file mode 100644 index 0000000..87b0999 --- /dev/null +++ b/vpc_with_subnetworks/variables.tf @@ -0,0 +1,24 @@ + +variable "project_id" { + type = string + description = "Project ID" + default = "" +} + +variable "region" { + type = string + description = "Region for this config" + default = "" +} + +variable "name" { + type = string + description = "VPC name" + default = "" +} + +variable "ip_cidr_range" { + type = list(string) + description="List of the range of internal address that are woned by this subnetwork" + default = [ "10.0.1.0/28" ] +}