-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmain.tf
84 lines (75 loc) · 2.48 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Summary: Creates a GKE (Google Kubernetes Engine) cluster, connects the Terraform Kubernetes Provider to it, and creates a k8s deployment.
# Documentation: https://www.terraform.io/docs/language/settings/index.html
terraform {
required_version = ">= 1.0.0"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 3.0"
}
}
}
# Documentation: https://www.terraform.io/docs/language/values/variables.html
variable "project_id" {
type = string
}
# Documentation: https://www.terraform.io/docs/language/providers/requirements.html
provider "google" {
project = var.project_id
region = "us-central1"
zone = "us-central1-a"
}
# GKE
# Documentation: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/container_cluster
resource "google_container_cluster" "changeme_cluster_and_deployment_cluster" {
name = "changeme-cluster-and-deployment-cluster"
location = "us-central1-a"
initial_node_count = 1
node_config {
preemptible = true
machine_type = "n1-standard-1"
}
}
# Kubernetes
# Documentation: https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/using_gke_with_terraform
# # Explanation: Retrieve an access token as the Terraform runner
data "google_client_config" "changeme_cluster_and_deployment_provider" {}
# Documentation: https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs
provider "kubernetes" {
host = "https://${google_container_cluster.changeme_cluster_and_deployment_cluster.endpoint}"
token = data.google_client_config.changeme_cluster_and_deployment_provider.access_token
cluster_ca_certificate = base64decode(
google_container_cluster.changeme_cluster_and_deployment_cluster.master_auth[0].cluster_ca_certificate,
)
}
# Deployment
# Documentation: https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment
resource "kubernetes_deployment" "changeme_cluster_and_deployment_deployment" {
metadata {
name = "changeme-cluster-and-deployment-deployment"
labels = {
app = "changeme-cluster-and-deployment-app"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "changeme-cluster-and-deployment-app"
}
}
template {
metadata {
labels = {
app = "changeme-cluster-and-deployment-app"
}
}
spec {
container {
image = "nginx"
name = "nginx"
}
}
}
}
}