From 22b6dbbf6a55ef75f1bade28ae8837200ee044d2 Mon Sep 17 00:00:00 2001 From: Aarushi <50577581+aarushik93@users.noreply.github.com> Date: Thu, 25 Jul 2024 09:45:36 +0100 Subject: [PATCH] feat(rnd,infra) Set up terraform (#7565) * add terraform * gitignore update * linting * formatting and linting in ci * store state in backend bucket --- .github/workflows/autogpt-infra-ci.yml | 33 +++++++++ rnd/infra/terraform/.gitignore | 4 ++ rnd/infra/terraform/.terraform.lock.hcl | 22 ++++++ rnd/infra/terraform/environments/dev.tfvars | 11 +++ rnd/infra/terraform/main.tf | 43 ++++++++++++ .../terraform/modules/gke_cluster/main.tf | 21 ++++++ .../terraform/modules/gke_cluster/outputs.tf | 14 ++++ .../modules/gke_cluster/variables.tf | 41 +++++++++++ .../terraform/modules/networking/main.tf | 12 ++++ .../terraform/modules/networking/ouputs.tf | 19 +++++ .../terraform/modules/networking/variables.tf | 21 ++++++ rnd/infra/terraform/variables.tf | 70 +++++++++++++++++++ 12 files changed, 311 insertions(+) create mode 100644 .github/workflows/autogpt-infra-ci.yml create mode 100644 rnd/infra/terraform/.gitignore create mode 100644 rnd/infra/terraform/.terraform.lock.hcl create mode 100644 rnd/infra/terraform/environments/dev.tfvars create mode 100644 rnd/infra/terraform/main.tf create mode 100644 rnd/infra/terraform/modules/gke_cluster/main.tf create mode 100644 rnd/infra/terraform/modules/gke_cluster/outputs.tf create mode 100644 rnd/infra/terraform/modules/gke_cluster/variables.tf create mode 100644 rnd/infra/terraform/modules/networking/main.tf create mode 100644 rnd/infra/terraform/modules/networking/ouputs.tf create mode 100644 rnd/infra/terraform/modules/networking/variables.tf create mode 100644 rnd/infra/terraform/variables.tf diff --git a/.github/workflows/autogpt-infra-ci.yml b/.github/workflows/autogpt-infra-ci.yml new file mode 100644 index 0000000000..2605cd02de --- /dev/null +++ b/.github/workflows/autogpt-infra-ci.yml @@ -0,0 +1,33 @@ +name: AutoGPT Builder Infra + +on: + push: + branches: [ master ] + paths: + - '.github/workflows/autogpt-infra-ci.yml' + - 'rnd/infra/**' + pull_request: + paths: + - '.github/workflows/autogpt-infra-ci.yml' + - 'rnd/infra/**' + +defaults: + run: + shell: bash + working-directory: rnd/infra + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: TFLint + uses: pauloconnor/tflint-action@v0.0.2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tflint_path: terraform/ + tflint_recurse: true + tflint_changed_only: false diff --git a/rnd/infra/terraform/.gitignore b/rnd/infra/terraform/.gitignore new file mode 100644 index 0000000000..ad9439c82b --- /dev/null +++ b/rnd/infra/terraform/.gitignore @@ -0,0 +1,4 @@ +*.tfstate +*.tfstate.backup +tfplan +.terraform/ \ No newline at end of file diff --git a/rnd/infra/terraform/.terraform.lock.hcl b/rnd/infra/terraform/.terraform.lock.hcl new file mode 100644 index 0000000000..5e14d96b4b --- /dev/null +++ b/rnd/infra/terraform/.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 = "4.85.0" + constraints = "~> 4.0" + hashes = [ + "h1:ZVDZuhYSIWhCkSuDkwFeSIJjn0/DcCxak2W/cHW4OQQ=", + "zh:17d60a6a6c1741cf1e09ac6731433a30950285eac88236e623ab4cbf23832ca3", + "zh:1c70254c016439dbb75cab646b4beace6ceeff117c75d81f2cc27d41c312f752", + "zh:35e2aa2cc7ac84ce55e05bb4de7b461b169d3582e56d3262e249ff09d64fe008", + "zh:417afb08d7b2744429f6b76806f4134d62b0354acf98e8a6c00de3c24f2bb6ad", + "zh:622165d09d21d9a922c86f1fc7177a400507f2a8c4a4513114407ae04da2dd29", + "zh:7cdb8e39a8ea0939558d87d2cb6caceded9e21f21003d9e9f9ce648d5db0bc3a", + "zh:851e737dc551d6004a860a8907fda65118fc2c7ede9fa828f7be704a2a39e68f", + "zh:a331ad289a02a2c4473572a573dc389be0a604cdd9e03dd8dbc10297fb14f14d", + "zh:b67fd531251380decd8dd1f849460d60f329f89df3d15f5815849a1dd001f430", + "zh:be8785957acca4f97aa3e800b313b57d1fca07788761c8867c9bc701fbe0bdb5", + "zh:cb6579a259fe020e1f88217d8f6937b2d5ace15b6406370977a1966eb31b1ca5", + "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", + ] +} diff --git a/rnd/infra/terraform/environments/dev.tfvars b/rnd/infra/terraform/environments/dev.tfvars new file mode 100644 index 0000000000..0082681b18 --- /dev/null +++ b/rnd/infra/terraform/environments/dev.tfvars @@ -0,0 +1,11 @@ +project_id = "agpt-dev" +region = "us-central1" +zone = "us-central1-a" +network_name = "dev-gke-network" +subnet_name = "dev-gke-subnet" +subnet_cidr = "10.0.0.0/24" +cluster_name = "dev-gke-cluster" +node_count = 2 +node_pool_name = "dev-main-pool" +machine_type = "e2-medium" +disk_size_gb = 100 diff --git a/rnd/infra/terraform/main.tf b/rnd/infra/terraform/main.tf new file mode 100644 index 0000000000..474a76c205 --- /dev/null +++ b/rnd/infra/terraform/main.tf @@ -0,0 +1,43 @@ +terraform { + required_version = ">= 1.9.0" + required_providers { + google = { + source = "hashicorp/google" + version = "~> 4.0" + } + } + backend "gcs" { + bucket = "agpt-dev-terraform" + prefix = "terraform/state" + } +} + +provider "google" { + project = var.project_id + zone = var.zone +} + +module "networking" { + source = "./modules/networking" + + project_id = var.project_id + region = var.region + network_name = var.network_name + subnet_name = var.subnet_name + subnet_cidr = var.subnet_cidr +} + +module "gke_cluster" { + source = "./modules/gke_cluster" + + project_id = var.project_id + zone = var.zone + cluster_name = var.cluster_name + node_pool_name = var.node_pool_name + node_count = var.node_count + machine_type = var.machine_type + disk_size_gb = var.disk_size_gb + network = module.networking.network_self_link + subnetwork = module.networking.subnet_self_link + enable_autopilot = var.enable_autopilot +} diff --git a/rnd/infra/terraform/modules/gke_cluster/main.tf b/rnd/infra/terraform/modules/gke_cluster/main.tf new file mode 100644 index 0000000000..c646a14030 --- /dev/null +++ b/rnd/infra/terraform/modules/gke_cluster/main.tf @@ -0,0 +1,21 @@ +resource "google_container_cluster" "primary" { + name = var.cluster_name + location = var.zone + + dynamic "node_pool" { + for_each = var.enable_autopilot ? [] : [1] + content { + name = var.node_pool_name + node_count = var.node_count + + node_config { + machine_type = var.machine_type + disk_size_gb = var.disk_size_gb + } + } + } + + network = var.network + subnetwork = var.subnetwork +} + diff --git a/rnd/infra/terraform/modules/gke_cluster/outputs.tf b/rnd/infra/terraform/modules/gke_cluster/outputs.tf new file mode 100644 index 0000000000..6a2c822f78 --- /dev/null +++ b/rnd/infra/terraform/modules/gke_cluster/outputs.tf @@ -0,0 +1,14 @@ +output "cluster_name" { + description = "The name of the cluster" + value = google_container_cluster.primary.name +} + +output "cluster_endpoint" { + description = "The endpoint for the cluster" + value = google_container_cluster.primary.endpoint +} + +output "node_pool_name" { + description = "The name of the node pool" + value = var.enable_autopilot ? null : google_container_cluster.primary.node_pool[0].name +} diff --git a/rnd/infra/terraform/modules/gke_cluster/variables.tf b/rnd/infra/terraform/modules/gke_cluster/variables.tf new file mode 100644 index 0000000000..80184e7ded --- /dev/null +++ b/rnd/infra/terraform/modules/gke_cluster/variables.tf @@ -0,0 +1,41 @@ +variable "project_id" { + description = "The project ID to host the cluster in" +} + +variable "zone" { + description = "The zone to host the cluster in" +} + +variable "cluster_name" { + description = "The name for the GKE cluster" +} + +variable "node_count" { + description = "Number of nodes in the cluster" +} + +variable "node_pool_name" { + description = "Name of the node pool in the cluster" +} + +variable "machine_type" { + description = "Type of machine to use for nodes" +} + +variable "disk_size_gb" { + description = "Size of the disk attached to each node, specified in GB" + default = 100 +} + +variable "network" { + description = "The VPC network to host the cluster in" +} + +variable "subnetwork" { + description = "The subnetwork to host the cluster in" +} + +variable "enable_autopilot" { + description = "Enable Autopilot for this cluster" + type = bool +} \ No newline at end of file diff --git a/rnd/infra/terraform/modules/networking/main.tf b/rnd/infra/terraform/modules/networking/main.tf new file mode 100644 index 0000000000..64aac3a77d --- /dev/null +++ b/rnd/infra/terraform/modules/networking/main.tf @@ -0,0 +1,12 @@ +resource "google_compute_network" "vpc_network" { + name = var.network_name + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "subnet" { + name = var.subnet_name + ip_cidr_range = var.subnet_cidr + region = var.region + network = google_compute_network.vpc_network.self_link +} + diff --git a/rnd/infra/terraform/modules/networking/ouputs.tf b/rnd/infra/terraform/modules/networking/ouputs.tf new file mode 100644 index 0000000000..24db7330e9 --- /dev/null +++ b/rnd/infra/terraform/modules/networking/ouputs.tf @@ -0,0 +1,19 @@ +output "network_name" { + description = "The name of the VPC network" + value = google_compute_network.vpc_network.name +} + +output "network_self_link" { + description = "The self-link of the VPC network" + value = google_compute_network.vpc_network.self_link +} + +output "subnet_name" { + description = "The name of the subnet" + value = google_compute_subnetwork.subnet.name +} + +output "subnet_self_link" { + description = "The self-link of the subnet" + value = google_compute_subnetwork.subnet.self_link +} \ No newline at end of file diff --git a/rnd/infra/terraform/modules/networking/variables.tf b/rnd/infra/terraform/modules/networking/variables.tf new file mode 100644 index 0000000000..3989bddff0 --- /dev/null +++ b/rnd/infra/terraform/modules/networking/variables.tf @@ -0,0 +1,21 @@ +variable "project_id" { + description = "The project ID to host the network in" +} + +variable "region" { + description = "The region to host the network in" +} + +variable "network_name" { + description = "The name of the VPC network" +} + +variable "subnet_name" { + description = "The name of the subnet" +} + +variable "subnet_cidr" { + description = "The CIDR range for the subnet" +} + + diff --git a/rnd/infra/terraform/variables.tf b/rnd/infra/terraform/variables.tf new file mode 100644 index 0000000000..23c48e9086 --- /dev/null +++ b/rnd/infra/terraform/variables.tf @@ -0,0 +1,70 @@ +variable "project_id" { + description = "The project ID to host the cluster in" + type = string +} + +variable "region" { + description = "Project region" + type = string + default = "us-central1" +} + +variable "zone" { + description = "The zone to host the cluster in" + type = string + default = "us-central1-a" +} + +variable "network_name" { + description = "The name of the VPC network" + type = string + default = "gke-network" +} + +variable "subnet_name" { + description = "The name of the subnet" + type = string + default = "gke-subnet" +} + +variable "subnet_cidr" { + description = "The CIDR range for the subnet" + type = string + default = "10.0.0.0/24" +} + +variable "cluster_name" { + description = "The name for the GKE cluster" + type = string + default = "gke-cluster" +} + +variable "node_count" { + description = "Number of nodes in the cluster" + type = number + default = 3 +} + +variable "node_pool_name" { + description = "The name for the node pool" + type = string + default = "default-pool" +} + +variable "machine_type" { + description = "Type of machine to use for nodes" + type = string + default = "e2-medium" +} + +variable "disk_size_gb" { + description = "Size of the disk attached to each node, specified in GB" + type = number + default = 100 +} + +variable "enable_autopilot" { + description = "Enable Autopilot for this cluster" + type = bool + default = false +}