【问题标题】:How to create multiple AWS IAM roles at once using terraform如何使用 terraform 一次创建多个 AWS IAM 角色
【发布时间】:2022-01-28 06:58:40
【问题描述】:

所以我知道我可以通过一遍又一遍地执行资源块来做到这一点,直到我把它们全部写出来,但这似乎有点多余。现在我只是想在将策略附加到角色之前创建角色。我在这里和整个网络上看了看,我找不到任何似乎可以做到的东西。我查看了 aws_iam_role 模块源代码,但没有看到他们如何为多个角色做到这一点。任何帮助将不胜感激。

我现在所做的示例代码。

resource "aws_iam_role" "test1" {
    name = "role1"
    assume_role_policy = file("${path.module}/json_files/assume_role.json")
}

resource "aws_iam_role" "test2" {
    name = "role2"
    assume_role_policy = file("${path.module}/json_files/assume_role.json")
}

resource "aws_iam_role" "test3" {
    name = "role3"
    assume_role_policy = file("${path.module}/json_files/assume_role.json")
}

【问题讨论】:

    标签: amazon-web-services terraform amazon-iam


    【解决方案1】:

    您可以使用countfor_each

    有计数

    resource "aws_iam_role" "test" {
        count = 3
        name = "role${each.index}"
        assume_role_policy = file("${path.module}/json_files/assume_role.json")
    }
    

    与 for_each

    resource "aws_iam_role" "test" {
        for_each = toset(["1", "2", "3"])
        name = "role${each.key}"
        assume_role_policy = file("${path.module}/json_files/assume_role.json")
    }
    

    【讨论】:

    • 这似乎比我做的要干净得多。我还用 type = map(map(list(string))) 对其进行了模块化
    猜你喜欢
    • 2020-03-26
    • 2020-04-25
    • 2018-11-10
    • 2018-07-30
    • 2019-12-08
    • 2021-02-03
    • 2018-09-09
    • 2020-05-16
    • 2018-01-11
    相关资源
    最近更新 更多