1

我有 Terraform 模块来预置 AWS 资源。之后terraform plan,我看到以下错误:

错误:main.tf 第 113 行缺少必需的参数,在模块“ecs-task-execution-role”中:113:模块“ecs-task-execution-role”{ 需要参数“iam_role_name”,但未找到定义.

这是我在中定义的资源../terraform/modules/iam/ecs_iam.tf

resource "aws_iam_role" "iam--task-execution-role" {
  name               = var.iam_role_name
  assume_role_policy = data.aws_iam_policy_document.ecs-task-assume-role.json
}

data "aws_iam_policy_document" "ecs-task-assume-role" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ecs-tasks.amazonaws.com"]
    }
  }
}

data "aws_iam_policy" "ecs-task-execution-role" {
  arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}

# Attach the above policy to the execution role.

resource "aws_iam_role_policy_attachment" "ecs-task-execution-role" {
  role       = aws_iam_role.iam-task-execution-role.name
  policy_arn = data.aws_iam_policy.ecs-task-execution-role.arn
}

变量定义在../terraform/modules/iam/variables.tf

variable "iam_role_name" {
  type = string
}

该模块位于../terraform/production/main.tf

module "iam-jo-task-execution-role" {

  source = "../modules/iam"

  iam_role_name = "iam-jo-task-execution-role"

}

# Attach the above policy to the execution role.

module "ecs-task-execution-role" {

  source = "../modules/iam"

}

任何帮助表示赞赏。

4

2 回答 2

1

在您的示例中:

module "iam-jo-task-execution-role" {

  source = "../modules/iam"

  iam_role_name = "iam-jo-task-execution-role"

}

# Attach the above policy to the execution role.

module "ecs-task-execution-role" {

  source = "../modules/iam"

}

您没有将iam_role_name变量的值传递给ecs-task-execution-role模块。它上面的iam-jo-task-execution-role模块显示了如何将变量传递给模块。

于 2020-10-15T01:40:12.697 回答
0

事实证明我误解了 Terraform 中的资源和模块。

../terraform/production/main.tf声明使用 source 中的资源的文件中../modules/iam,我需要以下语法:

module "iam-jo-task-execution-role" {

  source = "../modules/iam"

  iam_role_name = "iam-jo-task-execution-role"

}

无需为在 中声明的每个资源定义一个模块../terraform/modules/iam/ecs_iam.tf。这只是几个资源的分组(打包)。

于 2020-10-21T14:48:22.913 回答