pkslow.com 南瓜慢说

  • AllArticles
  • Container
  • Spring
  • Life
  • Cloud
  • Collections
  • About
  • GitHub

  • Search
Terraform101 English Terraform Middleware config Go Private Kubernetes pkslow Test HTTPS Redis Docker Mac Plan Stream MongoDB Spring DevOps JVM String Map Set List Performance Email Springboot JavaCollections ArrayList Java

Terraform Module - extract and reuse

Created on: 2021-06-06 | Category: Cloud | 0 | View: 338

1 Why we need module

Terraform Module helps us manage the resources in the right way, just like the Method in Java. Generally, the module contains the files:

  • main.tf: just like the main function;
  • README.md: description about the module;
  • variables.tf: define the variables used in module;
  • outputs.tf: output the value from the module;
  • examples: show how to use;

2 create your own module

2.1 create the module

I will create a simple module to deploy the nginx to Kubernetes. It helps to deploy the Deployment and Service on k8s for us. There are two files in the simple module:

(1) main.tf: defines the resources(kubernetes Deployment and Service)

(2) variables.tf: defines the variables for the resources

Let's put the two files in the folder nginx-kubernetes:

main.tf:

resource "kubernetes_deployment" "test" {
  metadata {
    name      = var.applicationName
    namespace = var.namespace
  }
  spec {
    replicas = var.replicas
    selector {
      match_labels = {
        app = var.applicationName
      }
    }
    template {
      metadata {
        labels = {
          app = var.applicationName
        }
      }
      spec {
        container {
          image = var.image
          name  = "nginx-container"
          port {
            container_port = 80
          }
        }
      }
    }
  }
}

resource "kubernetes_service" "test" {
  metadata {
    name      = var.applicationName
    namespace = var.namespace
  }
  spec {
    selector = {
      app = var.applicationName
    }
    type = "NodePort"
    port {
      node_port   = var.nodePort
      port        = 80
      target_port = 80
    }
  }

  depends_on = [kubernetes_deployment.test]
}

As code shows, the varibles pattern is var.xxx. The Terraform will try to replace the var with the input varibles.

variables.tf:

variable "namespace" {
    description = "k8s namespace"
}

variable "applicationName" {
    description = "applicationName"
}

variable "image" {
    description = "image"
}

variable "replicas" {
    description = "deployment replicas"
}

variable "nodePort" {
    description = "nodePort"
}

All the var.xxx in main.tf need to be pre-defined here.

2.2 How to use the module

It's easy to use the module, just like calling a method in Java:

provider "kubernetes" {
  config_path = "~/.kube/config"
}

module "pkslow-nginx" {
    source = "./nginx-kubernetes"
    namespace = "pkslow"
    applicationName = "pkslow-nginx"
    image = "nginx:1.19.5"
    replicas = 3
    nodePort = 30301
}
  • source: where is the module from;
  • namespace: input varible for Kubernetes;
  • applicationName: input varible for Kubernetes;
  • image: input varible for Kubernetes;
  • replicas: input varible for Kubernetes;
  • nodePort: input varible for Kubernetes;

3 external module

Terraform supports many kinds of Module sources: local/Terraform registry/GitHub/http etc.

Local:

module "pkslow" {
  source = "./pkslow"
}

Terraform Registry:

module "consul" {
  source = "hashicorp/consul/aws"
  version = "0.1.0"
}

GitHub:

module "pkslow" {
  source = "github.com/larrydpk/pkslow"
}

GitHub SSH:

module "pkslow" {
  source = "git@github.com:larrydpk/pkslow.git"
}

Zip file:

module "vpc" {
  source = "https://pkslow.com/vpc-module?archive=zip"
}

4 the end

Please check the code on: https://github.com/LarryDpk/pkslow-samples


References:

Terraform Module

Module Sources


Code for all: GitHub

欢迎关注微信公众号<南瓜慢说>,将持续为你更新...

file

Recommendations:
Cloud Native
Terraform
Container: Docker/Kubernetes
Spring Boot / Spring Cloud
Https
如何制定切实可行的计划并好好执行

  • Author 作者: LarryDpk 南瓜慢说
  • Link 链接: https://www.pkslow.com/archives/terraform-module-en
  • 版权声明: 本博客所有文章除特别声明外,不可转载!
# Terraform101 # English # Terraform # Middleware # config # Go # Private # Kubernetes # pkslow # Test # HTTPS # Redis # Docker # Mac # Plan # Stream # MongoDB # Spring # DevOps # JVM # String # Map # Set # List # Performance # Email # Springboot # JavaCollections # ArrayList # Java
Terraform101 English Terraform Middleware config Go Private Kubernetes pkslow Test HTTPS Redis Docker Mac Plan Stream MongoDB Spring DevOps JVM String Map Set List Performance Email Springboot JavaCollections ArrayList Java
How to initiate the GCP project and use gcloud to access
《Terraform 101 从入门到实践》 第二章 Providers插件管理
  • Contents
  • Site Overview
南瓜慢说

南瓜慢说

多年Java开发,主要专注后端技术:Java/Spring/Springboot/微服务/大数据等。

多读书,多分享;多写作,多整理。

241 Posts
9 Categories
30 Tags
RSS
0%
© 2020 — 2022 南瓜慢说 pkslow The WebSite keeping alive:   粤ICP备20036375号