Цель Создайте два ресурса aws_athena_named_query
, используя терраформ. Конфигурация для каждого запроса определяется в отдельных словарях. Все словари запросов вложены в список (см. query_dict
переменная).
variable query_dict {
default = [
{
name = "query1"
workgroup = "bar"
query = "SELECT * FROM foo"
},
{
name = "query2"
workgroup = "bar"
query = "SELECT * FROM baz"
}
]
}
resource "aws_athena_named_query" "olap" {
for_each = toset([for query in var.query_dict: {
name = query.name
workgroup = query.workgroup
query = query.query}])
name = each.value.name
query = each.value.query
database = "test"
workgroup = each.value.workgroup
}
Что приводит к ошибке:
The given "for_each" argument value is unsuitable: "for_each" supports maps
and sets of strings, but you have provided a set containing type object.
Попытка 2:
resource "aws_athena_named_query" "olap" {
for_each = [for query in tomap(var.query_dict): {
name = query.name
workgroup = query.workgroup
query = query.query}]
name = each.value.name
query = each.value.query
database = "test"
workgroup = each.value.workgroup
}
Результат:
Invalid value for "v" parameter: cannot convert tuple to map of any single
type.
Попытка 3:
resource "aws_athena_named_query" "olap" {
count = length(var.query_dict)
name = query_dict[count.index]["name"]
query = query_dict[count.index]["query"]
database = "test"
workgroup = query_dict[count.index]["workgroup"]
}
Что выводит ошибку:
on main.tf line 57, in resource "aws_athena_named_query" "olap":
57: workgroup = query_dict[count.index]["workgroup"]
A reference to a resource type must be followed by at least one attribute
access, specifying the resource name.
Ожидаемый результат:
# aws_athena_named_query.olap["query1"] will be created
+ resource "aws_athena_named_query" "olap" {
+ database = "db"
+ id = (known after apply)
+ name = "query1"
+ query = "SELECT * FROM foo"
+ workgroup = "bar"
}
# aws_athena_named_query.olap["query2"] will be created
+ resource "aws_athena_named_query" "olap" {
+ database = "db"
+ id = (known after apply)
+ name = "query2"
+ query = "SELECT * FROM baz"
+ workgroup = "bar"
}