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

 
 
 
 

创建一个简单的terraform module的更多相关文章

  1. 用Eclipse 创建一个 简单的 Maven JavaWeb 项目

    使用Maven 创建一个简单的 javaWeb 项目: 本篇属于 创建 JavaWeb 项目的第三篇: 建议阅读本篇之前 阅读 用 Eclipse 创建一个简单的web项目  ;本篇是这这篇文章的基础 ...

  2. 用 Eclipse 创建一个简单的web项目

    Eclipse neon 汉化版 ; 1;右击新建 -->  选择 动态Web项目 2:  填写 项目名 项目位置 ; 选择 Dynamic web module version 和 tomca ...

  3. 一个先进的App框架:使用Ionic创建一个简单的APP

    原文  http://www.w3cplus.com/mobile/building-simple-app-using-ionic-advanced-html5-mobile-app-framewor ...

  4. 通过创建一个简单的骰子游戏来探究 Python

    在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏.这次,我将展示如何使用 Python 模块 Pygame 来创建一个图形化游戏.它将需要几篇文章才 ...

  5. 如何创建一个简单的Visual Studio Code扩展

    注:本文提到的代码示例下载地址>How to create a simple extension for VS Code VS Code 是微软推出的一款轻量级的代码编辑器,免费,开源,支持多种 ...

  6. 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型

    第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...

  7. 如何创建一个简单的C++同步锁框架(译)

    翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...

  8. Windows 8.1 应用再出发 (WinJS) - 创建一个简单项目

    前面几篇我们介绍了如何利用 C# + XAML 完成Windows Store App 功能的实现,接下来的几篇我们来看看如何利用 Html + WinJS 来完成这些功能. 本篇我们使用WinJS ...

  9. ADF_General JSF系列1_创建一个简单的JSF Application

    2015-02-17 Creatd By BaoXinjian

随机推荐

  1. (C/C++学习笔记) 十九. 模板

    十九. 模板 ● 模板的基本概念 模板(template) 函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 语法: template <<模 ...

  2. Linux 最小安装常用包

    Linux最小安装后很多常用的工具都没有,但直接通过yum install 文件名的方式可能无法找到,汇总于此,随时更新: CentOS7 1.如果要通过SecureCRT上直接上传下载文件,最简单的 ...

  3. mongoose的笔记

    本文地址转载自:http://www.cnblogs.com/egger/archive/2013/06/14/3135847.html  本文将介绍操作符的使用,配合操作符,我们可以执行更加复杂的操 ...

  4. SharePoint online Multilingual support - Development(1)

    博客地址:http://blog.csdn.net/FoxDave 上一节讲了SharePoint Online网站多语言的实现原理机制,本节主要从编程的角度来谈一下如何进行相关的设置. 下面列出 ...

  5. navicat premium 连接出现的问题

    1.listener does not currently know of service requested in connect descriptor 2.问题截图: 3.问题原因:服务名或者SI ...

  6. django面试三

    1.Django. Flask.Tornado框架的比较? Django: 对于django,大而全的框架它的内部组件比较多,内部提供:ORM.Admin.中间件.Form.ModelForm.Ses ...

  7. Day5作业及默写

    1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...

  8. vuejs 1.x与2.x差异

    1.x与2.x区别 v-for列表查询中 当前下标:$index --> index 1.x 用法 v-for="v in myData" =>$index获取下标 / ...

  9. WPA3在2018年为无线安全添砖加瓦

    Wi-Fi Alliance Announces WPA3, the Successor to Wi-Fi's WPA2 Security Protocol The Wi-Fi Alliance -- ...

  10. 重启uwsgi脚本备份

    NAME="identifyImg_uwsgi.init" if [ ! -n "$NAME" ];then echo "no arguments&q ...