【问题标题】:aws autscaling api access policy via terraform通过 terraform 的 aws autscaling api 访问策略
【发布时间】:2020-08-06 14:57:43
【问题描述】:

使用由服务器控制的 aws 自动缩放组,预测即将到来的负载并根据需要向上/向下扩展。服务器需要具有最少所需权限的自动缩放 api 权限。

我的问题是限制服务器仅使用在资源字段上定义的特定自动缩放组。到目前为止,我发现的所有策略示例都仅在资源字段中使用“*”,如果我没记错的话,这应该意味着它可以访问所有自动缩放组。

data "aws_iam_policy_document" "default" {
  statement {
    sid    = "S3PolicyStmtNodeAutoscalingApiCalls"
    effect = "Allow"

    actions   = [
      "autoscaling:DescribeAutoScalingGroups",
      "autoscaling:SetDesiredCapacity",
      "autoscaling:TerminateInstanceInAutoScalingGroup"
    ]

    resources = [ var.autoscaling_group_arn ]
  }
}

通过 terraform 实现,这将转化为以下 json 策略(自动缩放组 arn 混淆):

resource "aws_iam_policy" "aws_api_access" {
  arn    = "arn:aws:iam::123456789123:policy/aws-api-access"
  id     = "arn:aws:iam::123456789123:policy/aws-api-access"
  name   = "aws-api-access"
  path   = "/"
  policy = jsonencode({
    Statement = [
      {
        Action   = [
          "autoscaling:TerminateInstanceInAutoScalingGroup",
          "autoscaling:SetDesiredCapacity",
          "autoscaling:DescribeAutoScalingGroups",
        ]
        Effect   = "Allow"
        Resource = "arn:aws:autoscaling:region:acountid:autoScalingGroup:id:autoScalingGroupName/name"
        Sid      = "S3PolicyStmtAutoscalingApiCalls"
      }
    ]
    Version   = "2012-10-17"
  })
}

错误是 AccessDenied:用户:arn:aws:sts::id:assumed-role/role_name/i-instance-id 无权执行:autoscaling:DescribeAutoScalingGroups

到目前为止,我只使用资源属性中的通配符运行它,感谢任何提示。

【问题讨论】:

  • 使用受限资源时是否出现错误?如果可以,可以发一下吗?
  • 查看docs.aws.amazon.com/IAM/latest/UserGuide/… 看起来autoscaling:DescribeAutoScalingGroups 不能受资源或条件键的限制,但其他两个权限应该没问题。我会在 IAM 政策中将它们分成单独的语句,然后我认为这应该可行。
  • 呃,不错的提示,我试试看!错误是 AccessDenied:用户:arn:aws:sts::id:assumed-role/role_name/i-instance-id 无权执行:autoscaling:DescribeAutoScalingGroups
  • 是的,该操作需要 * 资源,但它是只读资源,并且该调用的输出中不应该有任何特别机密的内容,因此我认为您应该可以拆分语句.

标签: amazon-web-services terraform aws-auto-scaling aws-policies


【解决方案1】:

解决方案在 cmets 中,将自动缩放:DescribeAutoScalingGroups 与其余部分分开,解决无法在资源字段中指定自动缩放组的问题。

data "aws_iam_policy_document" "default" {
  statement {
    sid    = "S3PolicyStmtNodeAutoscalingApiCalls"
    effect = "Allow"

    actions   = [
      "autoscaling:SetDesiredCapacity",
      "autoscaling:TerminateInstanceInAutoScalingGroup"
    ]

    resources = [ var.autoscaling_group_arn ]
  }

  statement {
    sid    = "S3PolicyStmtNodeAutoscalingDescribe"
    effect = "Allow"

    actions   = [
      "autoscaling:DescribeAutoScalingGroups"
    ]

   resources = [ "*" ]
  }
}

【讨论】:

  • 您可以编辑答案以使其更充分地工作吗?评论不是一流的,因此不会像答案一样长寿。如果您可以在答案中展示您所做的事情,那么对于可能遇到与您相同的问题的其他人来说,这将更加有用。
猜你喜欢
  • 2019-02-06
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
  • 2021-06-12
  • 1970-01-01
相关资源
最近更新 更多