如何获得谷歌云所有可用的地形资源类型的列表?



我最近开始学习Terraform。对于googlegoogle-beta提供商,我想检查/列出所有可用的资源类型,但我没有收到任何有价值的信息。Hashicorp页面- Command: terraform providers schema似乎没有在这里提供更多信息。

Google Cloud Shell

$ cd /tmp
$ mkdir tf-temp
$ cd tf-temp
$ touch main.tf
$ terraform init
$ terraform providers schema
The `terraform providers schema` command requires the `-json` flag.

Usage: terraform [global options] providers schema -json
Prints out a json representation of the schemas for all providers used
in the current configuration.
$
$ terraform providers schema -json
{"format_version":"1.0"}

我认为文档中需要注意的关键是开头句子末尾的短语(强调我的):

terraform providers schema命令用于打印当前配置中使用的提供程序的详细模式.

在当前配置中使用提供商有几种不同的方式,但最直接的方式是在根模块中显式声明提供商需求。如果您想获得hashicorp/googlehashicorp/google-beta提供程序的模式,那么您可以通过以下方式声明这些需求:

terraform {
required_providers {
google = {
source  = "hashicorp/google"
version = "4.27.0"
}
google-beta = {
source  = "hashicorp/google-beta"
version = "4.27.0"
}
}
}

你并不一定需要像我上面展示的那样精确的版本约束——如果你只是想获得最新的版本,你可以使用一个不那么具体的版本约束或者根本不使用约束——但是我包含它只是为了展示你如何引导Terraform选择你想要的模式的提供者的版本。

您可以运行terraform init来安装提供程序,并在依赖锁文件中记录选择的版本,然后运行terraform providers schema -json来查看从这些提供程序提取的模式。

$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/google-beta versions matching "4.27.0"...
- Finding hashicorp/google versions matching "4.27.0"...
- Installing hashicorp/google-beta v4.27.0...
- Installed hashicorp/google-beta v4.27.0 (signed by HashiCorp)
- Installing hashicorp/google v4.27.0...
- Installed hashicorp/google v4.27.0 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
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.
$ terraform providers schema -json | jq
{
"format_version": "1.0",
"provider_schemas": {
"registry.terraform.io/hashicorp/google": {
"provider": {
"version": 0,
"block": {
"attributes": {
"access_approval_custom_endpoint": {
"type": "string",

(…因为对于一个合理的Stack Overflow答案来说太长而被截断)

这可能对想要快速回答的人有用:

terraform init
terraform providers schema -json | jq  -rM '.provider_schemas[].resource_schemas|keys[]'

最新更新