使用 cloudformation 创建 aws IAM 角色不会创建 RolePolicies

creating aws IAM Role using cloudformation does not create RolePolicies

我正在创建一个 ec2 实例,其角色提供对运动流和 Dynamodb 偏移表的访问。我为此使用 aws cloudformation

我在创建 Streaming Access IAM Role 本身时遇到了问题。

所以,我将有以下结构,

                        has
StreamingAccessRole ----------> RolePolicy1(kinesis:*), RolePolicy2(dynamodb:*)

使用两个策略创建 AWS IAM 角色的模板,一个用于 kinesis,另一个用于 dynamodb:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "teamIdentifier": {
      "Type": "String",
      "Default": "a28",
      "Description": "Identifier for the team"
    }
  },
  "Resources": {
    "StreamingAccessRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "ec2.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/a28/",
        "Policies": [
          {
            "PolicyName": "Stream-ConsumerOffset-RW-AccessPolicy",
            "PolicyDocument": {
              "Version": "2012-10-17",
              "Statement": [
                {
                  "Effect": "Allow",
                  "Action": "kinesis:*",
                  "Resource": "arn:aws:kinesis:us-west-2:*:stream/a28-*"
                },
                {
                  "Effect": "Allow",
                  "Action": "dynamodb:*",
                  "Resource": "arn:aws:dynamodb:us-west-2:*:table/a28-*"
                }
              ]
            }
          }
        ]
      }
    }
  }
}

它创建访问角色但没有角色策略。

$ aws iam get-role --role-name a28-streaming-access-role-st-StreamingAccessRole-14QHMTIOIRN5X --region us-west-2 --profile aws-federated
{
    "Role": {
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17", 
            "Statement": [
                {
                    "Action": "sts:AssumeRole", 
                    "Effect": "Allow", 
                    "Principal": {
                        "Service": "ec2.amazonaws.com"
                    }
                }
            ]
        }, 
        "RoleId": "AROAIFD6X2CJXTKLVQNLE", 
        "CreateDate": "2017-04-07T18:54:59Z", 
        "RoleName": "a28-streaming-access-role-st-StreamingAccessRole-14QHMTIOIRN5X", 
        "Path": "/a28/", 
        "Arn": "arn:aws:iam::500238854089:role/a28/a28-streaming-access-role-st-StreamingAccessRole-14QHMTIOIRN5X"
    }
}

列出角色策略

$ aws iam list-role-policies --role-name a28-streaming-access-role-st-StreamingAccessRole-14QHMTIOIRN5X --region us-west-2 --profile aws-federated
{
    "PolicyNames": []
}

这意味着它甚至没有创建任何策略,

aws iam list-policies --region us-west-2 --profile aws-federated | grep Stream-ConsumerOffset-RW-AccessPolicy

但是如果我在上面的例子中只提供了kinesis:*语句,它会创建一个策略,但不会单独使用dynamodb:*

所以,我的问题是 我应该如何使用一个 cloudformation AWS::IAM::Role 模板 提供多个 RolePolicies,或者这是否特定于 dynamodb?

你的模板非常适合我。

我 运行 您的模板,然后:

$ aws iam get-role --role-name stack1-StreamingAccessRole-1KDUTVG1OLLQM
{
    "Role": {
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17", 
            "Statement": [
                {
                    "Action": "sts:AssumeRole", 
                    "Effect": "Allow", 
                    "Principal": {
                        "Service": "ec2.amazonaws.com"
                    }
                }
            ]
        }, 
        "RoleId": "AROAJADV75HTIM6C62YXQ", 
        "CreateDate": "2017-04-08T22:22:21Z", 
        "RoleName": "stack1-StreamingAccessRole-1KDUTVG1OLLQM", 
        "Path": "/a28/", 
        "Arn": "arn:aws:iam::123456789012:role/a28/stack1-StreamingAccessRole-1KDUTVG1OLLQM"
    }
}

列出角色策略:

$ aws iam list-role-policies --role-name stack1-StreamingAccessRole-1KDUTVG1OLLQM
{
    "PolicyNames": [
        "Stream-ConsumerOffset-RW-AccessPolicy"
    ]
}

该政策作为 内联政策 附加,因此不会出现在 list-policies 中。而是使用get-role-policy查看:

$ aws iam get-role-policy --role-name stack1-StreamingAccessRole-1KDUTVG1OLLQM --policy-name Stream-ConsumerOffset-RW-AccessPolicy
{
    "RoleName": "stack1-StreamingAccessRole-1KDUTVG1OLLQM", 
    "PolicyDocument": {
        "Version": "2012-10-17", 
        "Statement": [
            {
                "Action": "kinesis:*", 
                "Resource": "arn:aws:kinesis:us-west-2:*:stream/a28-*", 
                "Effect": "Allow"
            }, 
            {
                "Action": "dynamodb:*", 
                "Resource": "arn:aws:dynamodb:us-west-2:*:table/a28-*", 
                "Effect": "Allow"
            }
        ]
    }, 
    "PolicyName": "Stream-ConsumerOffset-RW-AccessPolicy"
}

在角色中创建策略时出现间歇性竞争条件。使用 AWS::IAM::Policy 单独创建策略并将角色 属性 设置为角色。问题将消失。

原因可能是竞态条件,正如 by Tim Bassett, I simply wanted to add final solution that worked, and how to add AWS::IAM::Policy 中对 cloudformation 的回答。

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Some Streaming api devops",
  "Parameters": {
    "environment": {
      "Type": "String",
      "Default": "staging",
      "Description": "environment"
    }
  },
  "Resources": {
    "StreamingAccessRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "RoleName": "StreamingAccessRole",
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "ec2.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "Path": "/a28/"
      }
    },
    "StreamConsumerOffsetRWAccessPolicy": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "cloudwatch:*"
              ],
              "Resource": [
                "*"
              ]
            },
            {
              "Effect": "Allow",
              "Action": "kinesis:*",
              "Resource": "arn:aws:kinesis:us-west-2:051620159240:stream/a28-*"
            },
            {
              "Effect": "Allow",
              "Action": [
                "dynamodb:BatchGetItem",
                "dynamodb:BatchWriteItem",
                "dynamodb:CreateTable",
                "dynamodb:DeleteItem",
                "dynamodb:DeleteTable",
                "dynamodb:DescribeLimits",
                "dynamodb:DescribeReservedCapacity",
                "dynamodb:DescribeReservedCapacityOfferings",
                "dynamodb:DescribeStream",
                "dynamodb:DescribeTable",
                "dynamodb:GetItem",
                "dynamodb:GetRecords",
                "dynamodb:GetShardIterator",
                "dynamodb:ListStreams",
                "dynamodb:ListTables",
                "dynamodb:PutItem",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:UpdateItem",
                "dynamodb:UpdateTable"
              ],
              "Resource": "arn:aws:dynamodb:us-west-2:051620159240:table/a28-*"
            },
            {
              "Action": [
                "sns:*Permission",
                "sns:Create*",
                "sns:Delete*",
                "sns:Publish",
                "sns:ReceiveMessage",
                "sns:Set*"
              ],
              "Resource": [
                "arn:aws:sns:us-west-2:051620159240:a28-*"
              ],
              "Effect": "Allow"
            }
          ]
        },
        "PolicyName": "StreamConsumerOffsetRWAccessPolicy",
        "Roles": [
          {
            "Ref": "StreamingAccessRole"
          }
        ]
      }
    }
  }
}