【问题标题】:How can I create the first IAM role to assume with Terraform?如何创建第一个 IAM 角色以使用 Terraform 代入?
【发布时间】:2020-03-26 18:18:47
【问题描述】:

我正在尝试使用 Terraform 设置 AWS,但对 aws_iam_role 感到困惑。

所以要创建第一个角色(比如说超级用户),aws_iam_role 需要 assume_role_policy,这是一个要承担的角色。 (但目前还没有具有 poweruser 策略的 poweruser 角色。)

感觉就像是先有鸡还是先有蛋 - 要创建一个角色,我需要另一个角色来承担,但我无法创建,因为我还没有要承担的角色。

如何使用 Terraform 做到这一点?我误解了什么吗?还是我需要先手动设置一些初始角色/用户?

【问题讨论】:

    标签: amazon-web-services terraform amazon-iam terraform-provider-aws terraform-aws-modules


    【解决方案1】:

    如果这是您第一次运行 Terraform 脚本,我建议使用 Programmatic Access 在控制台中添加用户,附加 AWS 托管策略(如 AdministratorAccess)并确保保存 Access key IDSecret access key

    然后,在你的HOME目录cd ~,创建一个文件夹.aws

    在 .aws 目录中创建一个名为 credentials 的文件。

    [default]
    aws_access_key_id = paste
    aws_secret_access_key = paste
    

    在您的 terraform 文件中:

    provider "aws" {
        profile = "default" #pertains to the default profile included in credentials file
        region = "us-east-2"
        version = "~> 2.26.0"
    }
    

    【讨论】:

    • 谢谢。所以你的意思是我需要一个手动步骤来访问 Terraform,对吧?
    • Tp 在您的机器上设置 Terraform,是的,您需要手动步骤才能执行诸如 terraform planterraform apply 之类的 terraform 命令。当您能够执行 terraform 命令时,您就可以继续创建资源,例如角色、策略等。
    【解决方案2】:

    您必须创建第一个 IAM 用户 manually 才能获得用于 Terraform 的 access keysecret_key

    更准确地说,例如,您刚刚创建了一个 aws 帐户并且没有创建任何 IAM 用户,这意味着根本不存在访问密钥和秘密密钥。但是,Terraform 必须需要 access keysecret_key 才能执行。

    # AWS Provider
    provider "aws" {
      region = "ap-northeast-1"
      access_key = "AIKAWP3TMGZNG34RUWRS"
      secret_key = "RNA6yaOwlOw4AEuFaBH2qJzSh/aE1zhFL5cbvgbb"
    }
    

    因此,您必须为 only the first IAM user 创建一个 IAM 用户 manually 以获取 access keysecret_key 以执行 Terraform。在创建第一个 IAM 用户并将 access keysecret_key 提供给 Terraform 后,您可以使用 Terraform 来创建其他 IAM 用户,而无需手动方式。

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 2020-04-25
      • 1970-01-01
      • 2019-04-22
      • 2018-09-09
      • 2020-01-06
      • 2018-01-15
      • 2018-01-11
      • 2019-09-08
      相关资源
      最近更新 更多