terraform 配置github module source
terraform 支持多种module 的source 配置
以下是一个简单的使用github source的demo
测试项目
- 项目结构
├── init.tpl
├── main.tf
- 代码说明
main.tf 主要配置module block
module "users" {
source = "github.com/rongfengliang/terraform-module-demo/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
效果
terraform get
- module.users
- 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: bf52295095341d2d11821cecc698720af3a7d98f)
------------------------------------------------------------------------
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: "bf52295095341d2d11821cecc698720af3a7d98f" => <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\":\"ac61e848-238a-4646-c58e-e71560690deb\"\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\":\"67daebf6-21cb-56c6-4a29-fc829333b3e7\"\n}'" (forces new resource)
filename: "/Users/dalong/mylearning/mymodule-ttf/init.sh" => "/Users/dalong/mylearning/mymodule-ttf/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: bf52295095341d2d11821cecc698720af3a7d98f)
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: "bf52295095341d2d11821cecc698720af3a7d98f" => <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\":\"ac61e848-238a-4646-c58e-e71560690deb\"\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\":\"3a605320-88e5-47c4-e667-05f4a08b233a\"\n}'" (forces new resource)
filename: "/Users/dalong/mylearning/mymodule-ttf/init.sh" => "/Users/dalong/mylearning/mymodule-ttf/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: bf52295095341d2d11821cecc698720af3a7d98f)
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\":\"3a605320-88e5-47c4-e667-05f4a08b233a\"\n}'"
filename: "" => "/Users/dalong/mylearning/mymodule-ttf/init.sh"
local_file.foo: Creation complete after 0s (ID: f29538bda6dcd489041d6cc535c939287d801a70)
Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
- 生成的文件
init.sh
#!/bin/bash
curl -X POST \
http://"http://127.0.0.1:8500" \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"name":"dddddemo",
"age": "444",
"platform":"mobile",
"id":"3a605320-88e5-47c4-e667-05f4a08b233a"
}'
说明
terraform module source 对于多种类型的支持还是很不全的,对于我们进行代码的复用还是很方便的
参考资料
https://www.terraform.io/docs/modules/sources.html
https://github.com/rongfengliang/terraform-module-demo
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)