gpt4 book ai didi

terraform - 如何在 `Terraform` 中引用 aws_cognito_user_pools id?

转载 作者:行者123 更新时间:2023-12-05 03:53:53 26 4
gpt4 key购买 nike

我有以下用于 cognito 客户端的 terraform 配置:

data "aws_cognito_user_pools" "re_user_pool" {
name = "${var.cognito_user_pool_name}"
}

resource "aws_cognito_user_pool_client" "app_client" {
name = "re-app-client"

user_pool_id = data.aws_cognito_user_pools.re_user_pool.id
depends_on = [data.aws_cognito_user_pools.re_user_pool]

explicit_auth_flows = ["USER_PASSWORD_AUTH"]
prevent_user_existence_errors = "ENABLED"
allowed_oauth_flows_user_pool_client = true
allowed_oauth_flows = ["code"]
allowed_oauth_scopes = ["phone", "openid", "email", "profile", "aws.cognito.signin.user.admin"]
supported_identity_providers = ["COGNITO", "Google"]
callback_urls = ["https://scnothzsf0.execute-api.ap-southeast-2.amazonaws.com/staging/signup"]
}

我引用了 AWS 上已经存在的 Cognito 用户池。当它使用 aws_cognito_user_pool_client 中的用户池 ID 时,错误发生在 user_pool_id = data.aws_cognito_user_pools.re_user_pool.id 行。我会得到错误

Error: Error creating Cognito User Pool Client: InvalidParameterException: 1 validation error detected: Value 're-user' at 'userPoolId' failed to satisfy constraint: Member must satisfy regular expression pattern: [\w-]+_[0-9a-zA-Z]+

on infra/cognito.tf line 5, in resource "aws_cognito_user_pool_client" "app_client":
5: resource "aws_cognito_user_pool_client" "app_client" {`

ID 的格式似乎不正确。我已阅读此文档 https://www.terraform.io/docs/providers/aws/d/cognito_user_pools.html 并且它具有引用属性 ids - The list of cognito user pool ids..我想知道为什么它会提供一个用户池 ID 列表。我如何引用此 ID?

我还尝试将其引用为 user_pool_id = data.aws_cognito_user_pools.re_user_pool.ids[0] 但出现错误:

Error: Invalid index

on infra/cognito.tf line 8, in resource "aws_cognito_user_pool_client" "app_client":
8: user_pool_id = data.aws_cognito_user_pools.re_user_pool.ids[0]

This value does not have any indices.

上面引用的re_user_pool定义在这里:

resource "aws_cognito_user_pool" "re_user_pool" {
name = "re-user"
}

最佳答案

我在解决同样的问题时遇到了你的问题。我看到这个问题已经有几个月了,但我仍然会为像我一样最终出现在这里的任何人添加一个答案。

首先,解决方案是通过tolistids值从set转换为list函数,然后像访问任何 terraform list 一样访问它。

警告:在我的例子中,我已确保我只有一个给定名称的用户池,但如果您没有遵循此约定,您可能会获得多个用户池。对于这种情况,此解决方案不是完整的解决方案,但也许它仍会指明正确的方向。

示例代码:

data "aws_cognito_user_pools" "test" {
name = "a_name"
}

output "test" {
value = "${tolist(data.aws_cognito_user_pools.test.ids)[0]"
}

其次,我是如何得出结论的:

我添加了一个 output block ,这样我就可以看到我正在使用什么,并且我在我的 terraform 文件中注释掉了有问题的行,这样我就可以成功地执行 terraform apply。接下来我运行 terraform apply 然后是 terraform output --json(注意:apply 必须成功才能 output以获得最新值)。

示例临时输出 block :

output "test" {
value = "${data.aws_cognito_user_pools.test}" // output top-level object for debugging
}

相关terraform应用输出:

test = {
"arns" = [
"<redacted>",
]
"id" = "a_name"
"ids" = [
"us-east-1_<redacted>",
]
"name" = "a_name"
}

相关terraform输出--json输出:

"test": {
"sensitive": false,
"type": [
"object",
{
"arns": [
"set",
"string"
],
"id": "string",
"ids": [
"set",
"string"
],
"name": "string"
}
],
"value": {
"arns": [
"<redacted>"
],
"id": "a_name",
"ids": [
"us-east-1_<redacted>"
],
"name": "a_name"
}
}

如您所见,ids 部分是 string 类型的 set。我决定尝试将 ids 转换为列表,看看我是否可以访问 0 索引并且它有效。我觉得这可能是一个 terraform 错误,但我还没有提交问题。

关于terraform - 如何在 `Terraform` 中引用 aws_cognito_user_pools id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61434636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com