diff --git a/README.md b/README.md index 9f4d6e4a64..2d7303239f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ -# Configuring your **dev** environment +# Managing infrastructure as code with Terraform, Cloud Build, and GitOps + +This is the repo for the [Managing infrastructure as code with Terraform, Cloud Build, and GitOps](https://cloud.google.com/solutions/managing-infrastructure-as-code) tutorial. This tutorial explains how to manage infrastructure as code with Terraform and Cloud Build using the popular GitOps methodology. + +## Configuring your **dev** environment... Just for demostration, this step will: 1. Configure an apache2 http server on network '**dev**' and subnet '**dev**-subnet-01' @@ -12,7 +16,7 @@ terraform apply terraform destroy ``` -# Promoting your environment to **production** +## Promoting your environment to **production** Once you have tested your app (in this example an apache2 http server), you can promote your configuration to prodution. This step will: 1. Configure an apache2 http server on network '**prod**' and subnet '**prod**-subnet-01' diff --git a/cloudbuild.yaml b/cloudbuild.yaml index 84f5365cec..a2e241289d 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -25,7 +25,7 @@ steps: echo "***********************" - id: 'tf init' - name: 'hashicorp/terraform:0.11.14' + name: 'hashicorp/terraform:1.0.0' entrypoint: 'sh' args: - '-c' @@ -50,7 +50,7 @@ steps: # [START tf-plan] - id: 'tf plan' - name: 'hashicorp/terraform:0.11.14' + name: 'hashicorp/terraform:1.0.0' entrypoint: 'sh' args: - '-c' @@ -76,7 +76,7 @@ steps: # [START tf-apply] - id: 'tf apply' - name: 'hashicorp/terraform:0.11.14' + name: 'hashicorp/terraform:1.0.0' entrypoint: 'sh' args: - '-c' diff --git a/environments/dev/backend.tf b/environments/dev/backend.tf index 33a807a336..26c454deb5 100644 --- a/environments/dev/backend.tf +++ b/environments/dev/backend.tf @@ -15,7 +15,7 @@ terraform { backend "gcs" { - bucket = "PROJECT_ID-tfstate" + bucket = "yash-innovation-tfstate" prefix = "env/dev" } } diff --git a/environments/dev/main.tf b/environments/dev/main.tf index a77cb88e91..bec3d95395 100644 --- a/environments/dev/main.tf +++ b/environments/dev/main.tf @@ -14,27 +14,35 @@ locals { - "env" = "dev" + env = "dev" } provider "google" { project = "${var.project}" } -module "vpc" { - source = "../../modules/vpc" - project = "${var.project}" - env = "${local.env}" -} -module "http_server" { - source = "../../modules/http_server" - project = "${var.project}" - subnet = "${module.vpc.subnet}" -} -module "firewall" { - source = "../../modules/firewall" - project = "${var.project}" - subnet = "${module.vpc.subnet}" +module "kubernetes_engine" { + source = "../../modules/vpc" + count = var.kubernetes_engine-create ? var.kubernetes_engine-count : 0 + k8s_cluster_name = var.k8s_cluster_name + k8s_cluster_location = var.k8s_cluster_location + k8s_remove_default_node_pool = var.k8s_remove_default_node_pool + k8s_initial_node_count = var.k8s_initial_node_count + #k8s_username = var.k8s_username + #k8s_password = var.k8s_password + k8s_issue_client_certificate = var.k8s_issue_client_certificate + k8s_pool_name = var.k8s_pool_name + k8s_pool_location = var.k8s_pool_location + k8s_pool_node_count = var.k8s_pool_node_count + k8s_pool_preemptible = var.k8s_pool_preemptible + k8s_pool_machine_type = var.k8s_pool_machine_type + k8s_pool_disable-legacy-endpoints = var.k8s_pool_disable-legacy-endpoints + k8s_pool_oauth_scopes = var.k8s_pool_oauth_scopes + k8s_min_node_count = var.k8s_min_node_count + k8s_max_node_count = var.k8s_max_node_count + project = "${var.project}" + env = "${local.env}" } + diff --git a/environments/dev/outputs.tf b/environments/dev/outputs.tf index 0ae139e4f7..6f350a511d 100644 --- a/environments/dev/outputs.tf +++ b/environments/dev/outputs.tf @@ -13,22 +13,18 @@ # limitations under the License. -output "network" { - value = "${module.vpc.network}" +output "cluster_id" { + value = module.kubernetes_engine[*].cluster_id } -output "subnet" { - value = "${module.vpc.subnet}" +output "cluster_endpoint" { + value = module.kubernetes_engine[*].cluster_endpoint } -output "firewall_rule" { - value = "${module.firewall.firewall_rule}" +output "pool_id" { + value = module.kubernetes_engine[*].pool_id } -output "instance_name" { - value = "${module.http_server.instance_name}" -} - -output "external_ip" { - value = "${module.http_server.external_ip}" +output "pool_instance_group_urls" { + value = module.kubernetes_engine[*].pool_instance_group_urls } diff --git a/environments/dev/terraform.tfvars b/environments/dev/terraform.tfvars index cb8a5a1bbc..1ca5b5bcbe 100644 --- a/environments/dev/terraform.tfvars +++ b/environments/dev/terraform.tfvars @@ -1 +1,38 @@ -project="PROJECT_ID" \ No newline at end of file +project="yash-innovation" +kubernetes_engine-create=true + + +kubernetes_engine-count=1 + + +k8s_cluster_name="tf-gke-cluster1" + +k8s_cluster_location="us-central1-a" + + +k8s_remove_default_node_pool=true + +k8s_initial_node_count=1 + +k8s_issue_client_certificate=false + +k8s_pool_name="tf-node-pool" + + +k8s_pool_location="us-central1-a" + + +k8s_pool_node_count=2 + +k8s_pool_preemptible=true + +k8s_pool_machine_type="e2-small" + +k8s_min_node_count=1 +k8s_max_node_count=3 + +k8s_pool_disable-legacy-endpoints=true +k8s_pool_oauth_scopes= [ + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/monitoring" + ] diff --git a/environments/dev/variables.tf b/environments/dev/variables.tf index 2b0a363f94..4834687a6f 100644 --- a/environments/dev/variables.tf +++ b/environments/dev/variables.tf @@ -14,3 +14,85 @@ variable "project" {} +variable "kubernetes_engine-create" { + type = bool + default = true +} + +variable "kubernetes_engine-count" { + type = number + default = 1 +} + +variable "k8s_cluster_name" { + type = string + default = "tf-gke-cluster1" +} + +variable "k8s_cluster_location" { + type = string + default = "us-central1-a" +} + +variable "k8s_remove_default_node_pool" { + type = bool + default = true +} + +variable "k8s_initial_node_count" { + type = number + default = 1 +} + + +variable "k8s_issue_client_certificate" { + type = bool + default = false +} + +variable "k8s_pool_name" { + type = string + default = "tf-node-pool" +} + +variable "k8s_pool_location" { + type = string + default = "us-central1-a" +} + +variable "k8s_pool_node_count" { + type = number + default = 2 +} + +variable "k8s_pool_preemptible" { + type = bool + default = true +} + +variable "k8s_pool_machine_type" { + type = string + default = "e2-small" +} + +variable "k8s_pool_disable-legacy-endpoints" { + type = bool + default = true +} +variable "k8s_min_node_count" { + type = number + default = 1 +} + +variable "k8s_max_node_count" { + type = number + default = 3 +} + +variable "k8s_pool_oauth_scopes" { + type = list(string) + default = [ + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/monitoring", + ] +} diff --git a/environments/dev/versions.tf b/environments/dev/versions.tf index aecd2473e3..4cc81b29fa 100644 --- a/environments/dev/versions.tf +++ b/environments/dev/versions.tf @@ -14,5 +14,5 @@ terraform { - required_version = "~> 0.11.0" + required_version = "~> 1.0.0" } diff --git a/environments/prod/backend.tf b/environments/prod/backend.tf index 7ed343b1a2..ea29eca5c4 100644 --- a/environments/prod/backend.tf +++ b/environments/prod/backend.tf @@ -15,7 +15,7 @@ terraform { backend "gcs" { - bucket = "PROJECT_ID-tfstate" + bucket = "yash-innovation-tfstate" prefix = "env/prod" } } diff --git a/environments/prod/main.tf b/environments/prod/main.tf index 642fe67ba2..a3b8bfe852 100644 --- a/environments/prod/main.tf +++ b/environments/prod/main.tf @@ -14,27 +14,33 @@ locals { - "env" = "prod" + env = "dev" } provider "google" { project = "${var.project}" } -module "vpc" { - source = "../../modules/vpc" - project = "${var.project}" - env = "${local.env}" -} -module "http_server" { - source = "../../modules/http_server" - project = "${var.project}" - subnet = "${module.vpc.subnet}" -} -module "firewall" { - source = "../../modules/firewall" - project = "${var.project}" - subnet = "${module.vpc.subnet}" +module "kubernetes_engine" { + source = "../../modules/vpc" + count = var.kubernetes_engine-create ? var.kubernetes_engine-count : 0 + k8s_cluster_name = var.k8s_cluster_name + k8s_cluster_location = var.k8s_cluster_location + k8s_remove_default_node_pool = var.k8s_remove_default_node_pool + k8s_initial_node_count = var.k8s_initial_node_count + #k8s_username = var.k8s_username + #k8s_password = var.k8s_password + k8s_issue_client_certificate = var.k8s_issue_client_certificate + k8s_pool_name = var.k8s_pool_name + k8s_pool_location = var.k8s_pool_location + k8s_pool_node_count = var.k8s_pool_node_count + k8s_pool_preemptible = var.k8s_pool_preemptible + k8s_pool_machine_type = var.k8s_pool_machine_type + k8s_pool_disable-legacy-endpoints = var.k8s_pool_disable-legacy-endpoints + k8s_pool_oauth_scopes = var.k8s_pool_oauth_scopes + project = "${var.project}" + env = "${local.env}" } + diff --git a/environments/prod/outputs.tf b/environments/prod/outputs.tf index 0ae139e4f7..6f350a511d 100644 --- a/environments/prod/outputs.tf +++ b/environments/prod/outputs.tf @@ -13,22 +13,18 @@ # limitations under the License. -output "network" { - value = "${module.vpc.network}" +output "cluster_id" { + value = module.kubernetes_engine[*].cluster_id } -output "subnet" { - value = "${module.vpc.subnet}" +output "cluster_endpoint" { + value = module.kubernetes_engine[*].cluster_endpoint } -output "firewall_rule" { - value = "${module.firewall.firewall_rule}" +output "pool_id" { + value = module.kubernetes_engine[*].pool_id } -output "instance_name" { - value = "${module.http_server.instance_name}" -} - -output "external_ip" { - value = "${module.http_server.external_ip}" +output "pool_instance_group_urls" { + value = module.kubernetes_engine[*].pool_instance_group_urls } diff --git a/environments/prod/terraform.tfvars b/environments/prod/terraform.tfvars index cb8a5a1bbc..237517c662 100644 --- a/environments/prod/terraform.tfvars +++ b/environments/prod/terraform.tfvars @@ -1 +1,36 @@ -project="PROJECT_ID" \ No newline at end of file +project="yash-innovation" +kubernetes_engine-create=true + + +kubernetes_engine-count=1 + + +k8s_cluster_name="tf-gke-cluster1" + +k8s_cluster_location="us-central1-a" + + +k8s_remove_default_node_pool=true + +k8s_initial_node_count=1 + +k8s_issue_client_certificate=false + +k8s_pool_name="tf-node-pool" + + +k8s_pool_location="us-central1-a" + + +k8s_pool_node_count=1 + +k8s_pool_preemptible=true + +k8s_pool_machine_type="e2-micro" + + +k8s_pool_disable-legacy-endpoints=true +k8s_pool_oauth_scopes= [ + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/monitoring" + ] diff --git a/environments/prod/variables.tf b/environments/prod/variables.tf index 2b0a363f94..fcae41b22c 100644 --- a/environments/prod/variables.tf +++ b/environments/prod/variables.tf @@ -14,3 +14,76 @@ variable "project" {} +variable "kubernetes_engine-create" { + type = bool + default = true +} + +variable "kubernetes_engine-count" { + type = number + default = 1 +} + +variable "k8s_cluster_name" { + type = string + default = "tf-gke-cluster1" +} + +variable "k8s_cluster_location" { + type = string + default = "us-central1-a" +} + +variable "k8s_remove_default_node_pool" { + type = bool + default = true +} + +variable "k8s_initial_node_count" { + type = number + default = 1 +} + + +variable "k8s_issue_client_certificate" { + type = bool + default = false +} + +variable "k8s_pool_name" { + type = string + default = "tf-node-pool" +} + +variable "k8s_pool_location" { + type = string + default = "us-central1-a" +} + +variable "k8s_pool_node_count" { + type = number + default = 1 +} + +variable "k8s_pool_preemptible" { + type = bool + default = true +} + +variable "k8s_pool_machine_type" { + type = string + default = "e2-micro" +} + +variable "k8s_pool_disable-legacy-endpoints" { + type = bool + default = true +} + +variable "k8s_pool_oauth_scopes" { + type = list(string) + default = [ + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/monitoring", + ] +} diff --git a/environments/prod/versions.tf b/environments/prod/versions.tf index aecd2473e3..4cc81b29fa 100644 --- a/environments/prod/versions.tf +++ b/environments/prod/versions.tf @@ -14,5 +14,5 @@ terraform { - required_version = "~> 0.11.0" + required_version = "~> 1.0.0" } diff --git a/modules/firewall/main.tf b/modules/firewall/main.tf deleted file mode 100644 index 5e40f7089f..0000000000 --- a/modules/firewall/main.tf +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -locals { - network = "${element(split("-", var.subnet), 0)}" -} - -resource "google_compute_firewall" "allow-http" { - name = "${local.network}-allow-http" - network = "${local.network}" - project = "${var.project}" - - allow { - protocol = "tcp" - ports = ["80"] - } - - target_tags = ["http-server2"] - source_ranges = ["0.0.0.0/0"] -} diff --git a/modules/firewall/outputs.tf b/modules/firewall/outputs.tf deleted file mode 100644 index 6eee8e9bcf..0000000000 --- a/modules/firewall/outputs.tf +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -output "firewall_rule" { - value = "${google_compute_firewall.allow-http.name}" -} diff --git a/modules/firewall/variables.tf b/modules/firewall/variables.tf deleted file mode 100644 index 2301355111..0000000000 --- a/modules/firewall/variables.tf +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -variable "project" {} -variable "subnet" {} diff --git a/modules/firewall/versions.tf b/modules/firewall/versions.tf deleted file mode 100644 index aecd2473e3..0000000000 --- a/modules/firewall/versions.tf +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -terraform { - required_version = "~> 0.11.0" -} diff --git a/modules/http_server/main.tf b/modules/http_server/main.tf deleted file mode 100644 index 6f05187f34..0000000000 --- a/modules/http_server/main.tf +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -locals { - network = "${element(split("-", var.subnet), 0)}" -} - -resource "google_compute_instance" "http_server" { - project = "${var.project}" - zone = "us-west1-a" - name = "${local.network}-apache2-instance" - machine_type = "f1-micro" - - metadata_startup_script = "sudo apt-get update && sudo apt-get install apache2 -y && echo '