1

在 RDS AWS 创建的密码部分下,我试图传递aws_secretsmanager_secret_version价值。我得到以下错误。

resource "aws_db_instance" "airflow" {
  allocated_storage                   = "${var.rds_allocated_storage}"
  storage_type                        = "${var.rds_storage_type}"
  storage_encrypted                   = "true"
  engine                              = "mysql"
  engine_version                      = "${var.rds_engine_version}"
  instance_class                      = "${var.rds_instance_class}"
  name                                = "airflow"
  identifier                          = "airflow"
  username                            = "${var.rds_username}"
  password                            = "${jsondecode(aws_secretsmanager_secret_version.secret.secret_string)["rds_password"]}"
  parameter_group_name                = "-airflow-mysql"
  vpc_security_group_ids              = ["${aws_security_group_airflow_sg.id}"]
  db_subnet_group_name                = "${aws_db_subnet_group.airflow_rds.id}"
  kms_key_id                          = "${data.aws_kms_key.rds.arn}"
  license_model                       = "general-public-license"

  depends_on = [
    aws_db_parameter_group.airflow_mysql
  ]

  tags = merge(
    var.common_tags,
    map("Classification", "private"),
    map("Name", "-airflow-rds")
  )
}

秘密管理器.tf

resource "aws_secretsmanager_secret" "secret" {
  description         = "airflow"
  kms_key_id          = "${data.aws_kms_key.sm.arn}"
  name                = "airflow"
}
resource "random_string" "rds_password" {
  length = 16
  special = true
  override_special = "/@\" "
}


resource "aws_secretsmanager_secret_version" "secret" {
  secret_id     = "${aws_secretsmanager_secret.secret.id}"
  secret_string = <<EOF
{
  "rds_password": "${random_string.rds_password.result}"
  }
EOF
}

以下是错误日志:-

错误:函数调用错误

在 ../../modules/airflow/outputs.tf 第 27 行,在输出 "rds_password": 27: value = jsondecode(aws_secretsmanager_secret_version.secret.secret_string)["rds_password"] |------ -------- | aws_secretsmanager_secret_version.secret.secret_string 是 "{\n \"rds_password\": \"9Y\"@xu3jy@sNGXt/\"\n }\n"

调用函数“jsondecode”失败:对象键:值对后的无效字符“@”。

错误:函数调用错误

在 ../../modules/airflow/rds.tf 第 12 行,在资源 "aws_db_instance" "airflow": 12: password = "${jsondecode(aws_secretsmanager_secret_version.secret.secret_string)["rds_password"]}" |- --------------- | aws_secretsmanager_secret_version.secret.secret_string 是 "{\n \"rds_password\": \"9Y\"@xu3jy@sNGXt/\"\n }\n"

调用函数“jsondecode”失败:对象键:值对后的无效字符“@”。

4

2 回答 2

1

我认为您没有正确索引地图。问题在secret_string)["rds_password"]

代替

password = "${jsondecode(aws_secretsmanager_secret_version.secret.secret_string)["rds_password"]}"

password = "${jsondecode(aws_secretsmanager_secret_version.secret.secret_string["rds_password"])}"
于 2020-05-21T13:30:22.463 回答
0

正如您在Terraform 文档中看到的,secret_string 中的 key-val 对象应该使用 jsonencode() 注入。

看下面的例子(改编自文档页面):

# The map here can come from other supported configurations
# like locals, resource attribute, map() built-in, etc.
variable "example" {
  default = {
    #HERE YOU DEFINE YOUR MAP
    rds_password= "${random_string.rds_password.result}"

  }

  type = "map"
}

resource "aws_secretsmanager_secret_version" "example" {
  secret_id     = "${aws_secretsmanager_secret.example.id}"
  # HERE YOU INJECT THE KEY/VAL
  secret_string = "${jsonencode(var.example)}"
}
于 2019-08-08T14:58:24.170 回答