【问题标题】:Creating IAM user via terraform and upload the secret key and access key in S3 bucket通过 terraform 创建 IAM 用户并在 S3 存储桶中上传密钥和访问密钥
【发布时间】:2022-01-12 07:31:25
【问题描述】:

我编写了一个 terraform 代码来创建 IAM 用户,我的要求是将访问密钥和秘密密钥存储在 S3 存储桶中。我尝试通过 s3 cli 命令实现相同的功能,但没有太大帮助。任何建议将不胜感激

【问题讨论】:

  • 到目前为止你尝试过什么?在不知道的情况下,我们只能猜测您还没有尝试过什么。
  • 我尝试通过 s3 copy cli 命令,但没有太大帮助
  • 您可以尝试两种方法,一种使用“local_exec”,另一种使用“aws_s3_bucket_object”资源。确保您了解其中的区别
  • 更正我错了,密钥和访问密钥需要存储在文件中,然后使用本地 exec 和 aws s3 cp 命令上传或通过 terraform 路由并通过 aws_s3_bucket_object 上传

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


【解决方案1】:

我想指出,如果配置不正确,将令牌存储在 s3 中可能会很危险。

确保您了解 AWS 中的策略和 s3 中的访问控制的工作原理!https://docs.aws.amazon.com/IAM/latest/UserGuide/access.html

除此之外,这就是我想出的:

# The user to which we will grant access to s3
resource "aws_iam_user" "user" {
  name          = "s3-user"
  path          = "/"
}

# Create the access key
resource "aws_iam_access_key" "key" {
  user = aws_iam_user.user.name
}

# Create the bucket for storing tokens
resource "aws_s3_bucket" "token" {
  bucket = "my_token_bucket"
  acl    = "private"
}

# Create the object inside the token bucket
resource "aws_s3_bucket_object" "tokens" {
  bucket                 = aws_s3_bucket.token.id
  key                    = "keys.txt"
  server_side_encryption = "AES256"
  content_type = "text/plain"
  content = <<EOF
access_id: ${aws_iam_access_key.key.id}
access_secret: ${aws_iam_access_key.key.secret}
EOF
}

我还没有测试过。

【讨论】:

  • 我已经使用这种方式来实现我的要求,并稍作改动。这绝对能更好地满足我的要求。
【解决方案2】:

你可以使用 loca-exec 来执行命令:

resource "null_resource" "s3_copy" {
  provisioner "local-exec" {
    command = "aws s3 cp keys.txt s3://bucket/keys "
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多