创建一个简单的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
创建一个简单的terraform module的更多相关文章
- 用Eclipse 创建一个 简单的 Maven JavaWeb 项目
使用Maven 创建一个简单的 javaWeb 项目: 本篇属于 创建 JavaWeb 项目的第三篇: 建议阅读本篇之前 阅读 用 Eclipse 创建一个简单的web项目 ;本篇是这这篇文章的基础 ...
- 用 Eclipse 创建一个简单的web项目
Eclipse neon 汉化版 ; 1;右击新建 --> 选择 动态Web项目 2: 填写 项目名 项目位置 ; 选择 Dynamic web module version 和 tomca ...
- 一个先进的App框架:使用Ionic创建一个简单的APP
原文 http://www.w3cplus.com/mobile/building-simple-app-using-ionic-advanced-html5-mobile-app-framewor ...
- 通过创建一个简单的骰子游戏来探究 Python
在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏.这次,我将展示如何使用 Python 模块 Pygame 来创建一个图形化游戏.它将需要几篇文章才 ...
- 如何创建一个简单的Visual Studio Code扩展
注:本文提到的代码示例下载地址>How to create a simple extension for VS Code VS Code 是微软推出的一款轻量级的代码编辑器,免费,开源,支持多种 ...
- 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型
第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...
- 如何创建一个简单的C++同步锁框架(译)
翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...
- Windows 8.1 应用再出发 (WinJS) - 创建一个简单项目
前面几篇我们介绍了如何利用 C# + XAML 完成Windows Store App 功能的实现,接下来的几篇我们来看看如何利用 Html + WinJS 来完成这些功能. 本篇我们使用WinJS ...
- ADF_General JSF系列1_创建一个简单的JSF Application
2015-02-17 Creatd By BaoXinjian
随机推荐
- vue-router-4-编程式导航
想要导航到不同的 URL,用 router.push 方法,会向 history 栈添加一个新的记录 <router-link> <==>router.push // 字符串 ...
- Java 整体测试重点题 错题积累
重点题 错题积累 1: 解析: %d:用来设置输出日志的日期和时间 %m:用来输出代码中指定的消息 %n:用来输出一个回车换行符 %l:用来输出日志事件的发生位置 %p:用来输出优先级 %f:用 ...
- lxml简单用法 解析网页
import requests s=requests.Session() re=s.get(lgurl,headers=headers) #此处s可以直接换成requests the_page=re ...
- MyEclipse教程:使用UML创建模块库——第二部分(二)
MyEclipse 在线订购年终抄底促销!火爆开抢>> [MyEclipse最新版下载] UML2建模文件存储在建模存储库中,建模可用于生成Java代码,或者可以从代码生成模型. 本教程介 ...
- 插入排序算法 Java实现
插入排序算法是算法排序中的一种: 该算法是假设已有序列是有序序列,从首元素(首元素为单个元素,肯定是有序的...)开始分析,对其他元素的位置进行有序的确定: 以算法为例: public class I ...
- 循环神经网络-极其详细的推导BPTT
首先明确一下,本文需要对RNN有一定的了解,而且本文只针对标准的网络结构,旨在彻底搞清楚反向传播和BPTT. 反向传播形象描述 什么是反向传播?传播的是什么?传播的是误差,根据误差进行调整. 举个例子 ...
- redis 五大数据类型之hash篇
1.hset/hget/hmset/hmget/hgetall/hdel --hgetall 是以截图中 key-value 分别一一显示出来,k1对应v1 ,k2对应v2 2.hlen 3.hexi ...
- 2019-03-21-day016-正则表达式
昨日内容回顾 基本数据类型 编码 流程控制 文件操作 函数-内置函数 装饰器 常用模块: 序列化模块 随机数模块 os模块 sys模块 时间模块 hashlib collections re 1天半 ...
- 匿名函数lambda,过滤函数filter,映射类型map
匿名函数lambda, 作用是不用定义函数,用完之后会自动被删掉,在使用执行脚本的时候,使用lambda就可以省下定义函数的过程,简化代码的可读性. 格式是 例子g=lambda x,y:x+y g( ...
- SpringBoot 添加fastjson
1.先在项目中添加fastjson依赖: <dependency> <groupId>com.alibaba</groupId> <artifactId> ...