Terraform Module management, aggregation resource extraction and reuse

1 Introduction

Terraform has been used in recent work vb.net tutorial Study and record, hoping to help others.

Modules are for management and reuse, just like functions. A module may have the following files:

  • main.tf: like a function entry;
  • README.md: such as function declaration;
  • variables.tf: variable description, like the input parameter of a function;
  • outputs.tf: output, such as the return value of a function;
  • Examples: use examples;

In addition, modules can also be nested, that is, there are sub modules in the module, but it is not recommended to nest too deeply.

2 create and use custom modules

2.1 creating modules

Let's take a simple example c # tutorial Explain how to create a module yourself. The function of this module is to deploy an nginx on Kubernetes, that is, to create a Deployment and a Service.

This module has two files, one is main TF is used to define resources, namely Deployment and Service. The other file is variables TF, used to define the input variables required by this module. Both files are placed in the nginx kubernetes folder of the current directory. The catalog results are as follows:

main.tf files are as follows:

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]
}

It is the resource definition, and then change some python basic tutorial The quantity is replaced by var.xxx, so that the corresponding variable will be assigned when Terraform is parsed.

variables.tf files are as follows:

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

variable "applicationName" {
    description = "applicationName"
}

variable "image" {
    description = "image"
}

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

variable "nodePort" {
    description = "nodePort"
}

In main The variables used in the TF file are defined here.

2.2 using modules

Now we have created it java basic course Module, and then reference it. We can just reference it in the current directory. The code is as follows:

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: the address of the module source;
  • namespace: command space, input parameters defined by the module;
  • applicationName: application name, input parameter of module definition;
  • Image: image, the input parameter defined by the module;
  • replicas: number of pods, input parameters defined by the module;
  • nodePort: port, the input parameter defined by the module;

Referring to a module is still very simple, which is similar to calling a function, that is, telling others the name and input parameters.

3 use external modules

The source of the module supports multiple sql tutorial Type, such as local path, Terraform official warehouse, GitHub, etc.

Local path:

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

Terraform warehouse:

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

GitHub address:

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

If SSH is used, it is as follows:

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

Compressed package:

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

4 Summary

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

Added by OriginalBoy on Sun, 23 Jan 2022 09:31:47 +0200