创建一个简单的terraform module

 

terraform module可以实现代码的复用,同时方便分享,下面创建一个简单的基于localfile && template provider 的module

module 项目

一个简单基于模板生成curl 配置module

  • 项目结构
├── README.md
├── init.tpl
├── main.tf
├── outputs.tf
└── variables.tf
  • 代码说明
main.tf:
使用template provider 生成文件
data "template_file" "init" {
  template = "${file("init.tpl")}"
  vars = {
    name = "${var.username}"
    age = 444
    consul_host = "${var.consul_host}"
    platform ="mobile"
  }
}
variables.tf:
变量定义
variable "username" {
    type = "string"
    description = "this is user default name"
    default = "dalong"
}

variable "consul_host" {
    type = "string"
    description = "consul host "
    default = "http://localhost:8500"
}
outputs.tf:
输出定义
output "exec_shell" {
    value = "${data.template_file.init.rendered}"
}
tpl 模板格式:
#!/bin/bash
curl -X POST \
   ${consul_host} \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
  "name":"${name}",
  "age": "${age}",
  "platform":"${platform}",
  "id":"${uuid()}"
}'

引用module

为了简单使用本地source,和module 在一个目录中

  • 项目结构
├── README.md
├── init.sh
├── init.tpl
├── main.tf
├── modules
│ └── users
│ ├── README.md
│ ├── init.tpl
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
  • 代码说明
 main.tf :
引用module, 以及使用module 的输出变量
module "users" {
  source = "./modules/users"
  username = "dddddemo"
  consul_host ="http://127.0.0.1:8500"
}
resource "local_file" "foo" {
    content = "${module.users.exec_shell}" 
    filename = "${path.module}/init.sh"
}
init.tpl: 模板内容
#!/bin/bash
curl -X POST \
   ${consul_host} \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
  "name":"${name}",
  "age": "${age}",
  "platform":"${platform}",
  "id":"${uuid()}"
}'

使用

  • get module
terraform get 
  • init 下载plugin
terraform  init

效果

Initializing modules...
- module.users

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.local: version = "~> 1.2"
* provider.template: version = "~> 2.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
  • 查看plan
terraform plan

效果

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.template_file.init: Refreshing state...
local_file.foo: Refreshing state... (ID: 785957ddadd6fe919ea5644ff2096066536e186b)

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

-/+ local_file.foo (new resource required)
      id: "785957ddadd6fe919ea5644ff2096066536e186b" => <computed> (forces new resource)
      content: "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"d288df44-c939-1ca3-0bb6-b8998b9305ca\"\n}'" => "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"204da291-b163-9f37-b719-e304c351ccbd\"\n}'" (forces new resource)
      filename: "/Users/dalong/mylearning/t-mode/init.sh" => "/Users/dalong/mylearning/t-mode/init.sh"


Plan: 1 to add, 0 to change, 1 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
  • apply
terraform apply

效果

data.template_file.init: Refreshing state...
local_file.foo: Refreshing state... (ID: 785957ddadd6fe919ea5644ff2096066536e186b)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

-/+ local_file.foo (new resource required)
      id: "785957ddadd6fe919ea5644ff2096066536e186b" => <computed> (forces new resource)
      content: "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"d288df44-c939-1ca3-0bb6-b8998b9305ca\"\n}'" => "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"a2f79788-011b-0bd8-ad21-51ce9604a42a\"\n}'" (forces new resource)
      filename: "/Users/dalong/mylearning/t-mode/init.sh" => "/Users/dalong/mylearning/t-mode/init.sh"


Plan: 1 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

local_file.foo: Destroying... (ID: 785957ddadd6fe919ea5644ff2096066536e186b)
local_file.foo: Destruction complete after 0s
local_file.foo: Creating...
  content: "" => "#!/bin/bash\ncurl -X POST \\\n http://\"http://127.0.0.1:8500\" \\\n -H 'Content-Type: application/json' \\\n -H 'cache-control: no-cache' \\\n -d '{\n \"name\":\"dddddemo\",\n \"age\": \"444\",\n \"platform\":\"mobile\",\n \"id\":\"a2f79788-011b-0bd8-ad21-51ce9604a42a\"\n}'"
  filename: "" => "/Users/dalong/mylearning/t-mode/init.sh"
local_file.foo: Creation complete after 0s (ID: 18917f440b6efa1ab11ae1c4ad1bde51bba37b85)

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
  • 生成的curl 命令
#!/bin/bash
curl -X POST \
   http://127.0.0.1:8500 \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
  "name":"dddddemo",
  "age": "444",
  "platform":"mobile",
  "id":"a2f79788-011b-0bd8-ad21-51ce9604a42a"
}'

说明

创建的module 很简单,但是包好了基本的创建以及使用流程,同时最好的实践是通过源代码管理进行module 的管理(git)

参考资料

https://github.com/rongfengliang/terraform-module-demo
https://www.terraform.io/docs/modules/index.html
https://www.terraform.io/docs/configuration/modules.html