英文:
terraform module sources github url. which format?
问题
我看到一个 Terraform 代码,写成了:
source = "github.com/brikis98/terraform-up-and-running-code//code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster?ref=v0.1.0"
但是,当我在 Chrome 中访问这个 URL 时,无法访问其资源。
所以,我想知道这个 URL 遵循什么格式。是否有任何规则?
双斜杠(//)让我感到怀疑...。
我已经查阅了 Terraform 模块部分的文档。但它没有描述这个。
英文:
I saw a terraform code which wrote as
> source = "github.com/brikis98/terraform-up-and-running-code//code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster?ref=v0.1.0"
But, when I access this URL in chrome, It can't reach its resource.
So. I wonder what this URL follows the format. Is there any rule on it?
double slash (//) is suspicious.. to me.
I already reviewed document in terraform module section. But It doesn't describe about it.
答案1
得分: 1
关于模块source
参数中的有效内容的完整文档请参阅模块源。
你在问题中展示的示例包含两个不同部分,对应于该页面上的两个不同部分:
-
github.com/brikis98/terraform-up-and-running-code
是GitHub下描述的源地址方案之一。由于Terraform是一个常用的版本控制主机,Terraform对其有特殊支持,并知道任何GitHub存储库都应使用Git克隆,因此Terraform可以在内部自动将该简写重写为以下规范形式:
git::https://github.com/brikis98/terraform-up-and-running-code
较长的版本包括
git::
前缀,告诉Terraform它应将以下内容视为Git URL,Terraform还会自动添加https://
前缀,使其成为HTTPS URL。实际上,这个
git::
前缀是通用Git存储库的语法,因此您可以看到Terraform在内部将此GitHub特定URL转换为通用Git URL。?ref=v0.1.0
后缀也是通用Git源地址方案的一部分,如选择修订版中所述。当你运行
terraform init
时,Terraform会在报告中报告源地址的规范版本,作为安装此模块包的一部分。 -
//code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster
是可选的子目录规范。"模块包"是Terraform可以下载的包含一个或多个模块的文件包的通用词汇。对于Terraform来说,Git存储库是模块包的一个示例。
默认情况下,Terraform期望在模块包的根目录中找到模块源代码,但如果源地址包括由
//
标记引入的一部分,那么Terraform将其视为包内应找到您试图调用的特定模块的子目录。这个特定的GitHub存储库包含书籍不同部分的许多不同的Terraform模块,因此对该存储库的引用将始终具有子目录部分。
综上所述,Terraform通过以下方式理解此源地址字符串:
-
将其拆分为“包”和“子目录”部分:
- 包:
github.com/brikis98/terraform-up-and-running-code?ref=v0.1.0
- 子目录:
code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster
- 包:
-
注意到包部分使用了
github.com
缩写,因此将其重写为规范形式:- 包:
git::https://github.com/brikis98/terraform-up-and-running-code?ref=v0.1.0
(这是
terraform init
在安装时将报告的包地址。) - 包:
-
由于包含了安装方法前缀
git::
,Terraform知道它应该通过运行类似以下命令来获取模块包:git clone https://github.com/brikis98/terraform-up-and-running-code
git checkout v0.1.0
Terraform始终将外部模块包下载到本地系统上的
.terraform
目录下的隐藏缓存目录中。该目录的确切结构是一个可能在不同版本的Terraform之间发生变化的实现细节,但在当前版本的Terraform中,子目录以您module
块中的标签命名。 -
如果下载成功,Terraform现在在一个类似
.terraform/modules/example
的目录中拥有包的副本。然后将子目录部分连接起来,以找到模块在本地缓存中的最终位置:
.terraform/modules/example/code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster
最终目录必须包含至少一个.tf
文件,描述模块的内容,Terraform将类似于根模块的方式加载和解码它。
英文:
The full documentation on what is valid inside a module source
argument is Module Sources.
The example you've shown in your question includes parts which correspond with two different sections on that page:
-
github.com/brikis98/terraform-up-and-running-code
is one of the source address schemes described under GitHub.Because Terraform is a commonly-used version control host, Terraform has special support for it and knows that any GitHub repository is to be cloned using Git, so Terraform can automatically rewrite that shorthand into the following canonical form internally:
git::https://github.com/brikis98/terraform-up-and-running-code
That longer version includes the
git::
prefix which tells Terraform that it should treat the following as a Git URL, and Terraform also automatically adds thehttps://
prefix to make this an HTTPS URL.This
git::
prefix is actually the syntax for Generic Git Repositories, so you can see that Terraform is internally transforming this GitHub-specific URL into a generic Git URL. The?ref=v0.1.0
suffix is also part of the Generic Git source address scheme, as described in Selecting a Revision.When you run
terraform init
Terraform will report the canonical version of the source address as part of its report that it's installing this module package. -
//code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster
is the optional subdirectory specification."Module package" is a generic word for a package of files that Terraform can download which contains one or more modules. A Git repository is one example of a module package as far as Terraform is concerned.
By default Terraform expects to find the module source code in the root of the module package, but if the source address includes a portion introduced by the
//
marker then Terraform takes that as a subdirectory within the package where it should find the specific module you were trying to call. This particular GitHub repository contains many different Terraform modules belonging to different parts of the book it's supporting, so references to this repository will always have a subdirectory portion.
Overall then Terraform understands this source address string by:
-
Splitting it into "package" and "subdirectory" parts:
- Package:
github.com/brikis98/terraform-up-and-running-code?ref=v0.1.0
- Subdirectory:
code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster
- Package:
-
Noticing that the package part is using the
github.com
shorthand and so rewriting it into the canonical form:- Package
git::https://github.com/brikis98/terraform-up-and-running-code?ref=v0.1.0
(This is the package address that
terraform init
will report when it's installing.) - Package
-
Now that the package includes the installation method prefix
git::
, Terraform knows that it should obtain the module package by running a command something like the following:git clone https://github.com/brikis98/terraform-up-and-running-code
git checkout v0.1.0
Terraform always downloads external module packages into a hidden cache directory under the
.terraform
directory on your local system. The exact structure of that directory is an implementation detail that might change between different versions of Terraform, but in current Terraform the subdirectory is named after the label in yourmodule
block. -
If the download succeeded then Terraform now has a copy of the package in a directory something like
.terraform/modules/example
.It will then join the subdirectory portion to find the final location of the module in the local cache:
.terraform/modules/example/code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster
That final directory must contain at least one .tf
file describing the contents of the module, which Terraform will then load and decode in a similar way to how it decodes the root module.
答案2
得分: 0
source = "git::https://github.com/brikis98/terraform-up-and-running-code.git//code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster?ref=v0.1.0"
英文:
Try with this
source = "git::https://github.com/brikis98/terraform-up-and-running-code.git//code/terraform/04-terraform-module/module-example/modules/services/webserver-cluster?ref=v0.1.0"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论