我需要部署 GCP 计算实例列表。我如何通过像这样的对象列表中的“vms”循环 for_each:
"gcp_zone": "us-central1-a",
"image_name": "centos-cloud/centos-7",
"vms": [
{
"hostname": "test1-srfe",
"cpu": 1,
"ram": 4,
"hdd": 15,
"log_drive": 300,
"template": "Template-New",
"service_types": [
"sql",
"db01",
"db02"
]
},
{
"hostname": "test1-second",
"cpu": 1,
"ram": 4,
"hdd": 15,
"template": "APPs-Template",
"service_types": [
"configs"
]
}
]
}
Run Code Online (Sandbox Code Playgroud) 我正在重构一些 Terraform 模块并得到:
Error: Provider configuration not present
To work with
module.my_module.some_resource.resource_name its
original provider configuration at
module.my_module.provider.some_provider.provider_name is required, but it
has been removed. This occurs when a provider configuration is removed while
objects created by that provider still exist in the state. Re-add the provider
configuration to destroy
module.my_module.some_resource.resource_name, after
which you can remove the provider configuration again.
Run Code Online (Sandbox Code Playgroud)
似乎我需要从 tfstate 文件中删除该资源,然后使用新的 tf 配置重新添加它。
当我重构一些整体代码时,有数百条这样的Error: Provider configuration not present
消息。
删除和重新添加的任何快捷方式?
我一直在使用Terraform来构建我的AWS栈并且一直很喜欢它.如果要在商业环境中使用,则需要针对不同环境(例如QA,STAGING,PROD)重用配置.
我怎么能做到这一点?我是否需要创建一个包装器脚本来调用terraform的cli,同时按照下面的环境传入不同的状态文件?我想知道Terraform是否提供了更原生的解决方案.
terraform apply -state=qa.tfstate
Run Code Online (Sandbox Code Playgroud) 我使用 CI 系统编译 terraform 提供程序并将它们捆绑到图像中,但每次运行 terraform init 时,都会收到以下错误/失败。
\n\xe2\x94\x82 Error: Failed to install provider\n\xe2\x94\x82 \n\xe2\x94\x82 Error while installing rancher/rancher2 v1.13.0: the current package for\n\xe2\x94\x82 registry.terraform.io/rancher/rancher2 1.13.0 doesn't match any of the\n\xe2\x94\x82 checksums previously recorded in the dependency lock file\n
Run Code Online (Sandbox Code Playgroud)\n对于我的提供程序文件中列出的所有提供程序,都会重复此消息,如下所示:
\nterraform {\n required_version = ">= 0.13"\n\n required_providers {\n azurerm = {\n source = "hashicorp/azurerm"\n version = "2.55.0"\n }\n github = {\n source = "integrations/github"\n version = "4.8.0"\n }\n }\n...snip...\n}\n
Run Code Online (Sandbox Code Playgroud)\nterraform hcl 锁定文件存储在存储库中,只有当存储库中存在锁定文件时,才会出现这些错误并且 terraform init 失败。可能是什么原因?
\n我刚刚开始使用terraform,我希望能够使用AWS S3作为我的后端来存储我的项目状态.
terraform {
backend "s3" {
bucket = "tfstate"
key = "app-state"
region = "us-east-1"
}
}
Run Code Online (Sandbox Code Playgroud)
我觉得使用terraform为后端存储基础架构设置我的S3存储桶,IAM组和策略是明智的.
如果我在应用初始terraform基础设施之前设置后端状态,则会合理地抱怨后端存储桶尚未创建.所以,我的问题是,如何设置我的terraform后端与terraform,同时保持我的状态为terraform追踪的后端.看起来像一个嵌套的玩偶问题.
我对如何编写脚本有一些想法,例如,检查存储桶是否存在或是否已设置某个状态,然后引导terraform,最后在第一次运行后将terraform tfstate从本地文件系统复制到s3.但在走下这条艰难的道路之前,我想我会确保自己没有遗漏一些明显的东西.
我正在尝试使用 terraform v0.14.3 部署一个 bitbucket 管道以在谷歌云中创建资源。运行 terraform 命令后,管道失败并显示以下错误:
Error: Invalid legacy provider address
This configuration or its associated state refers to the unqualified provider
"google".
You must complete the Terraform 0.13 upgrade process before upgrading to later
versions.
Run Code Online (Sandbox Code Playgroud)
我们将 terraform 的本地版本更新为 v.0.13.0,然后运行:terraform 0.13upgrade
如本指南中所述:https ://www.terraform.io/upgrade-guides/0-13.html 。生成的 versions.tf 文件需要 terraform 版本 >=0.13,我们所需的提供程序块现在如下所示:
terraform {
backend "gcs" {
bucket = "some-bucket"
prefix = "terraform/state"
credentials = "key.json" #this is just a bitbucket pipeline variable
}
required_providers {
google = { …
Run Code Online (Sandbox Code Playgroud) 我需要为不同的用户启动一堆EC2盒子.每个用户都应该从所有其他用户沙箱中进行,因此每个EC2框都需要自己的SSH密钥.
在terraform中实现这一目标的最佳方法是什么?
我发现的几乎所有说明都要我手动创建SSH密钥并将其粘贴到terraform脚本中.
(坏)示例:
由于我需要以编程方式为许多用户生成唯一键,因此这是不切实际的.
这似乎不是一个困难的用例,但我无法在任何地方找到它的文档.
在紧要关头,我可以使用bash生成terraform脚本并动态注入SSH密钥.但这似乎就像terraform首先应该做的那样.
我是 terraform 的新手,正在尝试构建具有两个子网和 VPC 的基础设施。我创建了两个模块
VPC 模块将创建一个 VPC 并返回 vpc_id 作为输出,与我尝试在子网模块中使用的返回 vpc_id 相同,但当我运行 terraform 计划时,它会要求我输入 vpc_id 输入。
我想要 VPC 模块的输出值中的 vpc_id,任何人都可以帮助我吗?
下面是代码,
根 tf 文件,
provider "aws" {
shared_credentials_file = var.shared_cred
profile = "default"
region = var.aws_region
}
module "vpc" {
source = "./vpc"
name = "terraformVPC"
cidr = "10.50.40.0/27"
}
module "private_subnet" {
source = "./subnet"
subnet_name = "private_subnet"
subnet_cidr = "10.50.40.16/28"
#VPC_id = aws_vpc.moduleVPC.id
VPCid = module.vpc.outvpc_id # this is the issue
}
module "public_subnet" { …
Run Code Online (Sandbox Code Playgroud) 我有一个情况,如果用户不提供 vpc id,我必须创建 aws_vpc 资源。之后,我应该使用该 VPC 创建资源。
现在,我在创建资源时应用条件aws_vpc
。例如,仅当existing_vpc
为 false 时才创建 VPC:
count = "${var.existing_vpc ? 0 : 1}"
Run Code Online (Sandbox Code Playgroud)
接下来,例如,我必须在 VPC 中创建节点。如果existing_vpc
为 true,则使用var.vpc_id
,否则使用从资源计算出的 VPC ID aws_vpc
。
但是,问题是,如果existing_vpc
为 true,aws_vpc
则不会创建新资源,并且三元条件无论如何都会尝试检查资源是否aws_vpc
正在创建。如果没有创建,terraform 就会出错。
在 上使用条件运算符时出现错误的示例aws_subnet
:
Resource 'aws_subnet.xyz-subnet' not found for variable 'aws_subnet.xyz-subnet.id'
Run Code Online (Sandbox Code Playgroud)
导致错误的代码是:
subnet_id = "${var.existing_vpc ? var.subnet_id : aws_subnet.xyz-subnet.id}"
Run Code Online (Sandbox Code Playgroud)
如果两者相互依赖,我们如何创建条件资源并根据它们为其他配置分配值?
google_logging_metric
我正在Terraform 中声明资源(使用 version 0.11.14
)
我有以下声明
resource "google_logging_metric" "my_metric" {
description = "Check for logs of some cron job\t"
name = "mycj-logs"
filter = "resource.type=\"k8s_container\" AND resource.labels.cluster_name=\"${local.k8s_name}\" AND resource.labels.namespace_name=\"workable\" AND resource.labels.container_name=\"mycontainer-cronjob\" \nresource.labels.pod_name:\"my-pod\""
project = "${data.terraform_remote_state.gke_k8s_env.project_id}"
metric_descriptor {
metric_kind = "DELTA"
value_type = "INT64"
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使filter
字段多行?
local
变量的存在"${local.k8s_name}
使得它有点具有挑战性。