如何在 Terraform 中按特定键对地图/对象列表进行排序?
例子:
[
{"name":"b", "value":"some value"},
{"name":"c", "value":"some value"},
{"name":"a", "value":"some value"},
{"name":"d", "value":"some value"}
]
Run Code Online (Sandbox Code Playgroud)
所需输出
[
{"name":"a", "value":"some value"},
{"name":"b", "value":"some value"},
{"name":"c", "value":"some value"},
{"name":"d", "value":"some value"}
]
Run Code Online (Sandbox Code Playgroud) 当我尝试运行“Terraform init”时出现错误,我在这里缺少什么?
\n\xe2\x94\x82 Error: Variables not allowed\n\xe2\x94\x82 \n\xe2\x94\x82 on ../../resources/s3-bucket/main.tf line 12, in resource "aws_s3_bucket" "s3-bucket":\n\xe2\x94\x82 12: prevent_destroy = var.prevent_destroy\n\xe2\x94\x82 \n\xe2\x94\x82 Variables may not be used here.\n\xe2\x95\xb5\n\n\xe2\x95\xb7\n\xe2\x94\x82 Error: Unsuitable value type\n\xe2\x94\x82 \n\xe2\x94\x82 on ../../resources/s3-bucket/main.tf line 12, in resource "aws_s3_bucket" "s3-bucket":\n\xe2\x94\x82 12: prevent_destroy = var.prevent_destroy\n\xe2\x94\x82 \n\xe2\x94\x82 Unsuitable value: value must be known\nRun Code Online (Sandbox Code Playgroud)\n资源:
\n resource "aws_s3_bucket" "s3-bucket" {\n bucket = var.bucket_name\n tags = {\n Name = var.tags_name\n Environment = var.tags_environment\n }\n versioning { enabled = var.versioning }\n lifecycle {\n …Run Code Online (Sandbox Code Playgroud) amazon-s3 amazon-web-services terraform terraform-provider-aws
我知道 CI/CD 变量可以在 HCL 中使用,因为在环境中使用 TF_VAR_ 前缀声明它们将使我能够将它们作为输入变量查找,然后在我所在的 .tf 文件中使用它们。需要他们。
\n我做了:
\nTF_VAR_ibm_api_key,然后屏蔽它。main.tfmain.tfvariables.tf,结果相同这是我的main.tf文件:
variable ibm_api_key {\n}\n\nterraform {\n required_version = ">= 0.13"\nrequired_providers {\n ibm = {\n source = "IBM-Cloud/ibm"\n }\n }\n}\n\nprovider "ibm" {\n ibmcloud_api_key = var.ibm_api_key\n}\nRun Code Online (Sandbox Code Playgroud)\n预期行为:变量从 CI/CD 传递并添加到 HCL 代码中。
\n当前行为:在 \xc2\xb4plan\xc2\xb4 期间,作业失败并出现错误代码 1
\n$ terraform plan\nvar.ibm_api_key\n …Run Code Online (Sandbox Code Playgroud) 我使用 Terraform 将文件上传到 S3 存储桶中。如何获取其 URL,如 S3://bucket/object?
我要使用
output "my_bucket_file_version" {
value = aws_s3_bucket_object.object.version_id
}
Run Code Online (Sandbox Code Playgroud)
但我想这不会起作用。
当尝试在 Terraform 中创建 FIFO SQS 队列时:
resource "aws_sqs_queue" "my_queue" {
name = "my_queue"
visibility_timeout_seconds = 10
message_retention_seconds = 172800
fifo_queue = true
tags = merge(local.base_tags, { Name = "my_queue" })
}
Run Code Online (Sandbox Code Playgroud)
抛出以下错误:
Error: invalid queue name: my_queue
Run Code Online (Sandbox Code Playgroud) 当我试图让我的 terraform 代码更加动态时,我遇到了问题。请重点关注“vm2”配置。
\n1. 工作场景
\n在我的 locals.tf 中我有:
\n vms_configurations = {\n "vm1" = {\n size = "Standard_E4ds_v5"\n vm_backup_policy_frequency = "Weekly"\n vm_backup_weekly = {\n count = 4\n weekdays = ["Sunday"]\n }\n }\n "vm2" = {\n size = "Standard_DS12_v2"\n vm_backup_policy_frequency = "Daily"\n vm_backup_daily_policy_retention = 7\n vm_backup_weekly = {\n }\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n在我对备份模块的调用中,我有:
\n...\n vm_backup_weekly = each.value["vm_backup_weekly"]\n vm_backup_daily_policy_retention = each.value["vm_backup_policy_frequency"] == "Daily" ? each.value["vm_backup_daily_policy_retention"] : null\n...\nRun Code Online (Sandbox Code Playgroud)\n一切都很好。
\n2. 失败场景
\n但是,如果我尝试从 locals.tf 中的“vm2”配置中删除“vm_backup_weekly”,如下所示:
\n vms_configurations = …Run Code Online (Sandbox Code Playgroud) 地形版本:“1.2.9”
\nlist(object({}))当具有类型并标记为的输入变量sensitive = true传递给dynamic块时,Terraform 失败并出现无效值错误for_each。\n当输入变量标记为不敏感时,不会出现该错误。
输入变量如下所示:
\nvariable "sample_variable" {\n type = list(object({\n name = string\n description = optional(string)\n secure = optional(bool)\n type = string\n use_default = optional(bool)\n value = string\n }))\n sensitive = true\n description = "A list of objects with sensitive values."\n default = []\n}\nRun Code Online (Sandbox Code Playgroud)\ndynamic并在资源块中消耗,for_each如下所示:
resource "ibm_cloud_sample_resource" "my_resource" {\n name = var.name\n description = var.description\n template_env_settings = local.env_values\n tags = var.tags\n dynamic "template_inputs" …Run Code Online (Sandbox Code Playgroud) 我有一个现有的 VPC,它有 4 个 CIDR,我需要检索这些 CIDR 并将它们作为入站规则添加到安全性中。有没有办法在 Terraform 中做到这一点?我没有找到一种方法来做到这一点 Terraform
data "aws_vpc" "example_vpc" {
id = "vpc-xxxxx" # Replace with the ID of your VPC
}
output "vpc_cidr_block" {
value = "${data.aws_vpc.example_vpc.cidr_block}"
}
Run Code Online (Sandbox Code Playgroud)
我已尝试上面的代码片段,但输出值仅给出主要 CIDR。
Terraform 版本:v0.14.5
我尝试使用 terraform 将后端和前端部署为 azure 中的容器应用程序。
前端需要将后端的 URL 作为环境变量。
这可能一次性完成吗?
例如,使用 docker-compose 时,容器的内部主机名是预先确定的,或者是 kubernetes 中的服务。
我现在唯一能想到的就是应用该计划,等待容器应用程序创建并接收主机名,然后更改我的.tf文件并再次应用。但必须有更好的解决方案。
azure terraform terraform-provider-azure azure-container-apps
我见过这个问题和另一个类似的问题,但无法让它工作使用 Terraform 的 helm_release,如何设置数组或列表值?
\n我的列表来自 json 文件,它是允许的组列表
\nlocals {\n app_groups = jsondecode(file("../../work/resources/gke/params/access_auths.json")).accessgroups[var.project][0].app_group\n}\noutput "app_groups" {\n value = local.app_groups\n}\nRun Code Online (Sandbox Code Playgroud)\n给出 -
\napp_groups = [\n "bigquery-team",\n "devops-team",\n]\nRun Code Online (Sandbox Code Playgroud)\n主.tf-
\nresource "helm_release" "oauth2proxy" {\n name = "oauth2proxy"\n chart = "../charts/oauth2proxy"\n namespace = kubernetes_namespace.oauth2proxy.metadata.0.name\n set {\n name = "app_groups[0]"\n value = "${local.app_groups}"\n }\n values = [\n templatefile("../charts/oauth2proxy/oauth2proxy_values.yaml", {\n projectID = var.project\n clusterName = var.clusterName\n })\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n在 helm deployment.yml 中,启动容器时需要进入 -args -
\n {{ range $v := .app_groups …Run Code Online (Sandbox Code Playgroud)