Overview



Install Terraform Client

See https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

On Mac:

brew install terraform

Example

https://gist.github.com/ivaravko/501b4e8e5f1bc1bf5fecd149186b99b9


Sample: main.tf

mail.tf
# Configure Kubernetes provider and connect to the Kubernetes API server
provider "kubernetes" {
  config_path    = "~/.kube/config"
  config_context = "docker-desktop"
}

# Create an Nginx pod
resource "kubernetes_pod" "nginx" {
  metadata {
    name = "terraform-example"
    labels = {
      app = "nginx"
    }
  }

  spec {
    container {
      image = "nginx:1.23.2"
      name  = "example"
    }
  }
}

# Create an service
resource "kubernetes_service" "nginx" {
  metadata {
    name = "terraform-example"
  }
  spec {
    selector = {
      app = kubernetes_pod.nginx.metadata.0.labels.app
    }
    port {
      port        = 80
    }

    type = "NodePort"
  }

  depends_on = [
    kubernetes_pod.nginx
  ]
}


Initialize

terraform init


Run plan to check for errors

terraform plan


Apply changes

terraform apply


Destroy changes applied

terraform destroy

References

ReferenceURL
Terraform Homehttps://www.terraform.io
Terraform explained in 15 mins | Terraform Tutorial for Beginnershttps://www.youtube.com/watch?v=l5k1ai_GBDE
Complete Terraform Course - From BEGINNER to PRO! (Learn Infrastructure as Code)https://www.youtube.com/watch?v=7xngnjfIlK4
  • No labels