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. 每天CSS学习之text-decoration

    text-decoration是CSS的一个属性,其作用是给文本装饰上划线.中间线.下划线或不装饰.其值如下所示: 1.none:不装饰任何线.该值是默认值.如下所示: p{ text-decorat ...

  2. json解析写入mysql

    import json,requests,pymysql from pprint import pprint from datetime import datetime dt=datetime.now ...

  3. Excel 数据读入到DataSet

    using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.I ...

  4. sass嵌套风格

    1.嵌套输出方式 nested Sass 提供了一种嵌套显示 CSS 文件的方式.例如 nav { ul { margin:; padding:; list-style: none; } li { d ...

  5. SQL-22 统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum

    题目描述 统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no.部门名称dept_name以及次数sumCREATE TABLE `departments` (`dept_no` char( ...

  6. jdk,jre和jvm

    JDK(Java Development Kit)是针对Java开发员的产品,是整个Java的核心,包括了Java运行环境JRE.Java工具和Java基础类库 JRE是Java Runtime En ...

  7. poj3080(kmp+枚举)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20163   Accepted: 8948 Descr ...

  8. ksort 函数

    foreach ($modules AS $key => $value){ ksort($modules[$key]);}ksort($modules); strpos(','.$_SESSIO ...

  9. mysql解决数据库高并发

    分表分库 数据库索引 redis缓存数据库 读写分离 负载均衡: 将大量的并发请求分担到多个处理节点,由于单个处理节点的故障不影响服务,负载均衡集群同事也实现了高可用性.

  10. JetBrains Goland 2018.2.1最新版破解

    JetBrains GoLand 2018.2.1破解版是JetBrains的新商业IDE,旨在为Go开发提供符合人体工程学的环境.新的IDE通过特定于Go语言的编码辅助和工具集成扩展了Intelli ...