我正在尝试在 terraform 中使用嵌套循环。我有两个列表变量list_of_allowed_accountsand list_of_images,并希望遍历 list list_of_images,然后遍历 list list_of_allowed_accounts。
这是我的 terraform 代码。
variable "list_of_allowed_accounts" {
type = "list"
default = ["111111111", "2222222"]
}
variable "list_of_images" {
type = "list"
default = ["alpine", "java", "jenkins"]
}
data "template_file" "ecr_policy_allowed_accounts" {
template = "${file("${path.module}/ecr_policy.tpl")}"
vars {
count = "${length(var.list_of_allowed_accounts)}"
account_id = "${element(var.list_of_allowed_accounts, count.index)}"
}
}
resource "aws_ecr_repository_policy" "repo_policy_allowed_accounts" {
count = "${length(var.list_of_images)}"
repository = "${element(aws_ecr_repository.images.*.id, count.index)}"
count = "${length(var.list_of_allowed_accounts)}"
policy = "${data.template_file.ecr_policy_allowed_accounts.rendered}"
}
Run Code Online (Sandbox Code Playgroud)
这相当于我正在尝试做的 bash。
for image in …Run Code Online (Sandbox Code Playgroud) 由于某种奇怪的原因,Terraform 文档没有解释“错误:循环”的含义。我到处找,但官方文档中没有提到它。
我找不到报告特定 terraform 提供商(在本例中为 AWS 提供商)版本的规范方法。我可以找到一个似乎用版本信息命名的二进制文件:
$ ls .terraform/plugins/darwin_amd64/
lock.json
terraform-provider-aws_v1.0.0_x4
Run Code Online (Sandbox Code Playgroud)
但没有其他参考v1.0.0_x4,或任何其他版本。
我正在想象一些像这样的命令terraform providers --version,它目前只会打印出 Terraform 版本。
说到这里,我正在使用 Terraform v0.10.7。
干杯,詹姆斯
我在 Terraform 中有现有的基础设施,并且已经使用了一段时间。最近,我交换了本地笔记本电脑的 AWS 凭证(存储在 中的凭证~/.aws/credentials),它停止工作,直到我重新设置这些凭证。
问题是我在 Terraform 源本身中声明了信用,但它似乎根本没有使用它们。
terraform {
backend "s3" {
bucket = "example_tf_states"
key = "global/vpc/us_east_1/example_state.tfstate"
encrypt = true
region = "us-east-1"
}
}
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
}
variable "access_key" {
default = "<hidden_for_stack_exchange_post>"
}
variable "secret_key" {
default = "<hidden_for_stack_exchange_post>"
}
variable "region" {
default = "us-east-1"
}
Run Code Online (Sandbox Code Playgroud)
访问 ID 权限 100% 良好。我使用相同的帐户 ID 和密钥进行aws configure设置,~/.aws/credentials就像我在上面的 Terraform 变量声明中一样。
只要~/.aws/credentials …
我有一个 100% 使用 Terraform 构建的 AWS 环境。我没碰过它了几天,但今天我去的时候做什么我认为是一个相当琐碎的变化,Terraform已经决定,它希望为我建造整个新的AWS基础设施。退出更改没有任何区别,无论我做什么,Terraform 现在想要构建新的基础设施。
它并没有想撕开旧的基础架构下,它想建立这个新的基础设施并排侧与它。
它基本上就像我所有预先存在的基础设施都不存在一样。
我怀疑这是因为一切都与 VPC 相关联,并且因为它认为它需要创建一个新的 VPC,所以 VPC 下游的一切(即一切)也会重新创建,以便 ID 匹配。
我对 Terraform 很陌生,所以有什么办法可以“强制”Terraform 回到旧状态?
我通过存储tfstate创建 S3 存储桶和 DynamoDB 锁表并将其存储在 git 中来引导我的 terraform 远程状态。我的组织使用扫描库 Yelp的/检测,秘密和标记包含行private的Base64 High Entropy String。
# excerpt from `tfstate` file
{
"mode": "managed",
"type": "aws_dynamodb_table",
"name": "state-lock",
"provider": "provider.aws",
"instances": [
{
"schema_version": 1,
"attributes": {
"arn": "arn:aws:dynamodb:eu-west-1:111:table/terraform-state-lock",
...
"write_capacity": 1
},
"private": "<long string>"
}
]
}
Run Code Online (Sandbox Code Playgroud)
https://www.terraform.io/docs/providers/aws/r/dynamodb_table.html不会将该字段作为属性导出,而且我找不到有关该字段含义的文档。
它包含什么?
运行 terraform 命令时,我开始得到以下信息:
$ terraform refresh
Error refreshing state: 1 error(s) occurred:
* SignatureDoesNotMatch: Signature expired: 20170226T035111Z is now earlier than 20170227T013047Z (20170227T014547Z - 15 min.)
status code: 403, request id: 7626f995-fc8e-11e6-9b73-7990b2a1af41
Run Code Online (Sandbox Code Playgroud)
这是什么意思?如何解决?
假设我有一个模块,它会生成一些 ids: module.tf:
resource "random_id" "etcdapiserver-id" {
byte_length = 4
count = "${var.etcd_apiserver_count}"
}
Run Code Online (Sandbox Code Playgroud)
模块_输出.tf:
output "etcdapiserver_hostname_list" {
value = ["${random_id.etcdapiserver-id.*.hex}"]
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作正常,列表成功地在输出中结束:
terraform output --module=module
etcdapiserver_hostname_list = [
751adf6a,
9e573ee7,
edb94de3
]
Run Code Online (Sandbox Code Playgroud)
现在我想在主 terraform 配置中使用此列表中的元素。假设我正在 openstack 上创建多个计算实例: main.tf:
resource "openstack_compute_instance_v2" "etcdapiserver" {
count = "3"
name = "etcdapi-node-${element(module.ignition.etcdapiserver_hostname_list.*, count.index)}"
Run Code Online (Sandbox Code Playgroud)
但它会失败
错误:资源“openstack_compute_instance_v2.etcdapiserver”配置:“etcdapiserver_hostname_list.*”不是模块“ignition”的有效输出
有什么办法可以做到吗?谢谢!
当我尝试在底部运行代码时,我收到以下错误(无论我命名存储帐户是什么),当前其名为“functions”,但我可以称其为“bannanas”,它会输出相同的错误?
我遇到的错误:(
“发生了 1 个错误:
azurerm_storage_account.test:发生 1 个错误:
azurerm_storage_account.test:创建 Azure 存储帐户“函数”时出错:storage.AccountsClient#Create:发送请求失败:StatusCode=0 -- 原始错误:autorest/azure:服务返回错误。状态= Code="StorageAccountAlreadyTaken" Message="名为functions 的存储帐户已被占用。
我的代码:
resource "azurerm_resource_group" "test" {
name = "azure-functions-cptest-rg"
location = "westus2"
}
resource "azurerm_storage_account" "test" {
name = "functionsapptestsa"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_app_service_plan" "test" {
name = "azure-functions-test-service-plan"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
kind = "FunctionApp"
sku {
tier = "Dynamic"
size = "Y1"
}
}
resource "azurerm_function_app" "test" {
name = …Run Code Online (Sandbox Code Playgroud) 要求:
我想旋转 AWS ALB - 为此,我需要在两个不同的可用区中至少有两个子网。
(Terraform 显示了一个非常明确的错误,以防我们忘记 - 请参阅下面的错误 #1)。
我想检查相关 VPC 中当前拥有的公有子网数量,并确保其至少为 2。
我的尝试:
为此我将计算:
number_of_public_subnets_to_create = "${2 - length(data.aws_subnet_ids.customer_a_public_subnets.ids)}"
Run Code Online (Sandbox Code Playgroud)
对于上面的计算 - Terraform 有 2 种数据源类型: aws_subnet和aws_subnet_ids。
如果子网被标记 - 我们可以使用aws_subnet_ids数据源并添加一个简单的过滤器,如下所示:
data "aws_subnet_ids" "customer_a_public_subnets" {
vpc_id = "${data.aws_vpc.my-customer_a-vpc.id}"
tags {
Tier = "Public"
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
但是如果子网不包含“公共/私有”标签怎么办?
有没有简单的解决方案(也许是aws_subnet数据源上的通用过滤器)?
错误#1:
创建应用程序负载均衡器时出错:ValidationError:必须指定两个不同可用区中的至少两个子网。