diff --git a/Terraform/.terraform.lock.hcl b/Terraform/.terraform.lock.hcl new file mode 100644 index 0000000..f3e1a86 --- /dev/null +++ b/Terraform/.terraform.lock.hcl @@ -0,0 +1,40 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/google" { + version = "4.55.0" + hashes = [ + "h1:ZXZfn21OrSyT7NkVSstMH+oEnv3iE8atQqWt6LiXPxU=", + "zh:0a82a76dc4bbe05418075f88830f73ad3ca9d56d83a172faaf3306b016219d52", + "zh:367e3c0ce96ab8f9ec3e1fab5a4f9a48b3b5b336622b36b828f75bf6fb663001", + "zh:51fd41c7508c4c39830e5c2885bc053e90d5d24fc90462235b69394185b7fa1d", + "zh:7ebe62261c522631d22ab06951d0d6a1bf629b98aea5d9fe2e2e50ca256cf395", + "zh:9dd119eca735471d61fe9e4cc45e8c257275e2e9f4da30fba7296fc7ae8de99e", + "zh:a4426a0d24dcf8b3899e17530fabb3fb5791ff7db65404c26e66b031a8422bd2", + "zh:c1e93a786b6d014610c3f83fda12b3044009947f729b2042635fa66d9f387c47", + "zh:ea0703ee2f5e3732077e946cfa5cdd85119ef4ecc898a2affdeef9de9f92fe4e", + "zh:ecada51dd406f46e9fce7dafb0b8ef3a671b8d572dbc1d39d9fdc137029f5275", + "zh:effb91791080a86ff130b517bce5253aed1372ad2c6f9cfb252375a196b9f730", + "zh:f1885b811a31e37d53bd780d2485c19754ee2db0a66affeb5e788aa9b1950b8c", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} + +provider "registry.terraform.io/hashicorp/random" { + version = "3.4.3" + hashes = [ + "h1:xZGZf18JjMS06pFa4NErzANI98qi59SEcBsOcS2P2yQ=", + "zh:41c53ba47085d8261590990f8633c8906696fa0a3c4b384ff6a7ecbf84339752", + "zh:59d98081c4475f2ad77d881c4412c5129c56214892f490adf11c7e7a5a47de9b", + "zh:686ad1ee40b812b9e016317e7f34c0d63ef837e084dea4a1f578f64a6314ad53", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:84103eae7251384c0d995f5a257c72b0096605048f757b749b7b62107a5dccb3", + "zh:8ee974b110adb78c7cd18aae82b2729e5124d8f115d484215fd5199451053de5", + "zh:9dd4561e3c847e45de603f17fa0c01ae14cae8c4b7b4e6423c9ef3904b308dda", + "zh:bb07bb3c2c0296beba0beec629ebc6474c70732387477a65966483b5efabdbc6", + "zh:e891339e96c9e5a888727b45b2e1bb3fcbdfe0fd7c5b4396e4695459b38c8cb1", + "zh:ea4739860c24dfeaac6c100b2a2e357106a89d18751f7693f3c31ecf6a996f8d", + "zh:f0c76ac303fd0ab59146c39bc121c5d7d86f878e9a69294e29444d4c653786f8", + "zh:f143a9a5af42b38fed328a161279906759ff39ac428ebcfe55606e05e1518b93", + ] +} diff --git a/Terraform/.terraform/providers/registry.terraform.io/hashicorp/google/4.55.0/linux_amd64/terraform-provider-google_v4.55.0_x5 b/Terraform/.terraform/providers/registry.terraform.io/hashicorp/google/4.55.0/linux_amd64/terraform-provider-google_v4.55.0_x5 new file mode 100755 index 0000000..aad030c Binary files /dev/null and b/Terraform/.terraform/providers/registry.terraform.io/hashicorp/google/4.55.0/linux_amd64/terraform-provider-google_v4.55.0_x5 differ diff --git a/Terraform/.terraform/providers/registry.terraform.io/hashicorp/random/3.4.3/linux_amd64/terraform-provider-random_v3.4.3_x5 b/Terraform/.terraform/providers/registry.terraform.io/hashicorp/random/3.4.3/linux_amd64/terraform-provider-random_v3.4.3_x5 new file mode 100755 index 0000000..65143dd Binary files /dev/null and b/Terraform/.terraform/providers/registry.terraform.io/hashicorp/random/3.4.3/linux_amd64/terraform-provider-random_v3.4.3_x5 differ diff --git a/Terraform/main.tf b/Terraform/main.tf new file mode 100644 index 0000000..14f265b --- /dev/null +++ b/Terraform/main.tf @@ -0,0 +1,152 @@ +provider "google" { + project = var.PROJECT_ID + region = var.REGION +} + +# random ID +resource "random_id" "bucket_prefix" { + byte_length = 8 +} + +#resource "google_storage_bucket" "default" { +# name = "tfstate-${random_id.bucket_prefix.hex}" +# force_destroy = false +# uniform_bucket_level_access = true +# location = "US" +# storage_class = "STANDARD" +# versioning { +# enabled = false +# } +#} + +#terraform { +# backend "gcs" { +# bucket = "tfstate-${random_id.bucket_prefix.hex}" +# prefix = "terraform/state" +# } +#} + +# vpc network +resource "google_compute_network" "vpc_network" { + name = var.VPC_NAME + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "vpc_subnet" { + name = "${var.VPC_NAME}-subnet" + region = var.REGION + network = google_compute_network.vpc_network.self_link + ip_cidr_range = "10.1.0.0/16" +} + +resource "google_compute_firewall" "allow_http" { + name = "allow-http-${var.COMMON_NAME}" + network = google_compute_network.vpc_network.self_link + source_ranges = ["0.0.0.0/0"] + destination_ranges = ["0.0.0.0/0"] + allow { + protocol = "tcp" + ports = ["80", "443"] + } +} + +resource "google_compute_firewall" "allow_redis" { + name = "allow-redis-${var.COMMON_NAME}" + network = google_compute_network.vpc_network.self_link + source_ranges = ["0.0.0.0/0"] + destination_ranges = ["0.0.0.0/0"] + allow { + protocol = "tcp" + ports = ["6379", "6380"] + } +} + +# pub/sub +resource "google_pubsub_topic" "spanner_topic" { + name = "${var.SPANNER_TOPIC}" +} + +resource "google_pubsub_subscription" "spanner_topic_sub" { + name = "${var.SPANNER_TOPIC}-sub" + topic = google_pubsub_topic.spanner_topic.name +} + +resource "google_pubsub_topic" "simulator_topic" { + name = "${var.SIMULATOR_TOPIC}" +} + +resource "google_pubsub_subscription" "simulator_topic_sub" { + name = "${var.SIMULATOR_TOPIC}-sub" + topic = google_pubsub_topic.simulator_topic.name +} + +resource "google_pubsub_topic" "redis_topic" { + name = "${var.REDIS_TOPIC}" +} + +resource "google_pubsub_subscription" "redis_topic_sub" { + name = "${var.REDIS_TOPIC}-sub" + topic = google_pubsub_topic.redis_topic.name +} + +resource "google_pubsub_topic" "score_topic" { + name = "${var.SCORE_TOPIC}" +} + +resource "google_pubsub_subscription" "score_topic_sub" { + name = "${var.SCORE_TOPIC}-sub" + topic = google_pubsub_topic.score_topic.name +} + +# memory store - redis +resource "google_redis_instance" "redis_instance" { + name = var.REDIS_INST_NAME + tier = "BASIC" + memory_size_gb = 5 + authorized_network = google_compute_network.vpc_network.self_link +} + +# Spanner +resource "google_spanner_instance" "spanner_instance" { + name = var.SPANNER_INST_NAME + config = "regional-${var.REGION}" + display_name = var.SPANNER_INST_NAME + processing_units = 200 +} + +resource "google_spanner_database" "spanner_database" { + instance = google_spanner_instance.spanner_instance.name + name = var.SPANNER_DB_NAME + deletion_protection = false +} + +# GKE +resource "google_container_cluster" "gke_cluster" { + name = var.GKE_CLUSTER_NAME + location = var.REGION + network = google_compute_network.vpc_network.name + subnetwork = google_compute_subnetwork.vpc_subnet.name + remove_default_node_pool = true + initial_node_count = 1 + enable_ip_masq = true +} + +resource "google_container_node_pool" "gke_node_pool" { + name = "${var.GKE_CLUSTER_NAME}-default" + location = var.REGION + cluster = google_container_cluster.gke_cluster.name + node_count = 1 + + node_config { + preemptible = true + machine_type = "e2-medium" + oauth_scopes = [ + "https://www.googleapis.com/auth/cloud-platform", + "https://www.googleapis.com/auth/compute", + "https://www.googleapis.com/auth/devstorage.read_only", + "https://www.googleapis.com/auth/logging.write", + "https://www.googleapis.com/auth/monitoring", + ] + } +} + diff --git a/Terraform/terraform.tfvars b/Terraform/terraform.tfvars new file mode 100644 index 0000000..aeaed6a --- /dev/null +++ b/Terraform/terraform.tfvars @@ -0,0 +1 @@ +PROJECT_ID = \ No newline at end of file diff --git a/Terraform/variables.tf b/Terraform/variables.tf new file mode 100644 index 0000000..0b51f96 --- /dev/null +++ b/Terraform/variables.tf @@ -0,0 +1,59 @@ +variable "PROJECT_ID" { + type = string + description = "GCP project ID" +} + +variable "COMMON_NAME" { + type = string + default = "game-demo" +} + +variable "REGION" { + type = string + default = "us-west1" +} + +variable "VPC_NAME" { + type = string + default = "vpc-game-demo" +} + +variable "SPANNER_TOPIC" { + type = string + default = "pushToSpannerTest" +} + +variable "SIMULATOR_TOPIC" { + type = string + default = "simulatorTest" +} + +variable "REDIS_TOPIC" { + type = string + default = "updateRedisTest" +} + +variable "SCORE_TOPIC" { + type = string + default = "updateScoreTest" +} + +variable "REDIS_INST_NAME" { + type = string + default = "redis-test" +} + +variable "SPANNER_INST_NAME" { + type = string + default = "game-demo-inst" +} + +variable "SPANNER_DB_NAME" { + type = string + default = "game-demo-db" +} + +variable "GKE_CLUSTER_NAME" { + type = string + default = "game-demo-cluster" +} \ No newline at end of file